diff --git a/README.markdown b/README.markdown index 7257027..70ccbaf 100644 --- a/README.markdown +++ b/README.markdown @@ -28,6 +28,8 @@ To use it, add it to your `project.clj`: :rules cljx.rules/cljs-rules}]} ``` +Can be run "once" or "auto", in which case it will watch all source-paths for changes to .cljx files. Defaults to "once". + Add ```clojure diff --git a/project.clj b/project.clj index 7f1ed18..064b48b 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject com.keminglabs/cljx "0.1.3" +(defproject com.keminglabs/cljx "0.1.4" :description "Static Clojure code rewriting" :url "http://github.com/lynaghk/cljx" @@ -6,6 +6,7 @@ :url "http://www.opensource.org/licenses/BSD-3-Clause"} :dependencies [[org.clojure/core.logic "0.7.0"] - [jonase/kibit "0.0.3"]] + [jonase/kibit "0.0.3"] + [watchtower "0.1.1"]] :eval-in-leiningen true) diff --git a/src/leiningen/cljx.clj b/src/leiningen/cljx.clj index ce1d17c..7065a47 100644 --- a/src/leiningen/cljx.clj +++ b/src/leiningen/cljx.clj @@ -1,5 +1,6 @@ (ns leiningen.cljx - (:use [cljx.core :only [generate]])) + (:use [cljx.core :only [generate]] + [watchtower.core :only [watcher* watch rate file-filter on-change extensions]])) (def no-opts-warning "You need a :cljx entry in your project.clj! It should look something like:\n @@ -8,18 +9,41 @@ :cljs-output-path \".generated/cljs\"} ") +(defn- cljx-compile [builds] + "The actual static transform, separated out so it can be called repeatedly." + (doseq [{:keys [source-paths output-path extension rules include-meta] + :or {extension "clj" include-meta false}} builds] + (let [rules (eval rules)] + (doseq [p source-paths] + (binding [*print-meta* include-meta] + (generate p output-path extension rules)))))) + +(defn- once + "Transform .cljx files once and then exit." + [builds] + (cljx-compile builds)) + +(defn- auto + "Watch .cljx files and transform them after any changes." + [builds] + (let [dirs (set (flatten (map :source-paths builds)))] + (println "Watching" (vec dirs) "for changes.") + (-> (watcher* dirs) + (file-filter (extensions :cljx)) + (rate 1000) + (on-change (fn [_] (cljx-compile builds))) + (watch)))) + (defn cljx "Statically transform .cljx files into Clojure and ClojureScript sources." - [project] - - (if-let [opts (:cljx project)] - (if-let [{builds :builds} opts] - (doseq [{:keys [source-paths output-path extension rules include-meta] - :or {extension "clj" include-meta false}} builds] + {:subtasks [#'once #'auto]} + ([project] (cljx project "once")) + ([project subtask] - (let [rules (eval rules)] - (doseq [p source-paths] - (binding [*print-meta* include-meta] - (generate p output-path extension rules)))))) + (if-let [opts (:cljx project)] + (if-let [{builds :builds} opts] + (case subtask + "once" (once builds) + "auto" (auto builds))) - (println no-opts-warning))) + (println no-opts-warning))))