Skip to content

Commit

Permalink
Merge pull request lynaghk#2 from jwhitlark/master
Browse files Browse the repository at this point in the history
Added "auto" subtask.
  • Loading branch information
lynaghk committed Aug 9, 2012
2 parents 083ab82 + 35e2666 commit 53cf1d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Expand Up @@ -16,7 +16,7 @@ Cljx is a Lein plugin that emits Clojure and ClojureScript code from a single me
To use it, add it to your `project.clj`:

```clojure
:plugins [com.keminglabs/cljx "0.1.0"]
:plugins [com.keminglabs/cljx "0.1.4"]
:cljx {:builds [{:source-paths ["src/cljx"]
:output-path ".generated/clj"
:rules cljx.rules/clj-rules}
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions project.clj
@@ -1,11 +1,12 @@
(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"
:license {:name "BSD"
: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)
48 changes: 36 additions & 12 deletions 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
Expand All @@ -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))))

0 comments on commit 53cf1d1

Please sign in to comment.