aboutsummaryrefslogtreecommitdiff
path: root/lux-lein/src
diff options
context:
space:
mode:
authorEduardo Julian2016-12-01 17:05:12 -0400
committerEduardo Julian2016-12-01 17:05:12 -0400
commit3279245005b83d0b1446a042f2470d42c1bebf64 (patch)
tree363731eec945b76d4b46276930174b1449451d16 /lux-lein/src
parent5ac6c8480c5d5866d55b18a5674c96acc3e3f427 (diff)
- lux-lein can now watch source-code directories to automatically build and test when changes occur.
Diffstat (limited to 'lux-lein/src')
-rw-r--r--lux-lein/src/leiningen/lux.clj13
-rw-r--r--lux-lein/src/leiningen/lux/utils.clj11
-rw-r--r--lux-lein/src/leiningen/lux/watch.clj41
3 files changed, 56 insertions, 9 deletions
diff --git a/lux-lein/src/leiningen/lux.clj b/lux-lein/src/leiningen/lux.clj
index 6b2268d70..4902d048f 100644
--- a/lux-lein/src/leiningen/lux.clj
+++ b/lux-lein/src/leiningen/lux.clj
@@ -8,7 +8,8 @@
[leiningen.core.classpath :as classpath]
(leiningen.lux [builder :as &builder]
[test :as &test]
- [repl :as &repl])))
+ [repl :as &repl]
+ [watch :as &watch])))
;; [Exports]
(defn lux [project & args]
@@ -22,6 +23,14 @@
"repl"
(&repl/repl project)
+ "watch"
+ (case (second args)
+ "build"
+ (&watch/watch #(&builder/build project) project)
+
+ "test"
+ (&watch/watch #(&test/test project) project))
+
;; default...
- (println "Commands available: build, test, repl"))
+ (println "Commands available: (watch) build, (watch) test, repl"))
)
diff --git a/lux-lein/src/leiningen/lux/utils.clj b/lux-lein/src/leiningen/lux/utils.clj
index 70d203865..6419e9de8 100644
--- a/lux-lein/src/leiningen/lux/utils.clj
+++ b/lux-lein/src/leiningen/lux/utils.clj
@@ -86,11 +86,8 @@
(when line
(println line)
(recur (.readLine std-out))))
- (loop [had-error? false
- line (.readLine std-err)]
- (if line
- (do (println line)
- (recur true (.readLine std-err)))
- (when had-error?
- (System/exit 1))))
+ (loop [line (.readLine std-err)]
+ (when line
+ (println line)
+ (recur (.readLine std-err))))
(println post))))
diff --git a/lux-lein/src/leiningen/lux/watch.clj b/lux-lein/src/leiningen/lux/watch.clj
new file mode 100644
index 000000000..d9cae9075
--- /dev/null
+++ b/lux-lein/src/leiningen/lux/watch.clj
@@ -0,0 +1,41 @@
+;; Copyright (c) Eduardo Julian. All rights reserved.
+;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+;; If a copy of the MPL was not distributed with this file,
+;; You can obtain one at http://mozilla.org/MPL/2.0/.
+
+(ns leiningen.lux.watch
+ (:require [leiningen.core.classpath :as classpath])
+ (:import (java.io File)
+ (java.nio.file FileSystems
+ Path
+ WatchEvent$Kind
+ StandardWatchEventKinds
+ WatchService
+ WatchKey)))
+
+(defn ^:private file-tree [path]
+ (let [dir (new File path)]
+ (if (and (.exists dir)
+ (.isDirectory dir))
+ (->> (.listFiles dir) (mapcat (comp file-tree #(.getAbsolutePath ^File %))) (cons path))
+ (list))))
+
+(defn watch [action project]
+ (let [fs (FileSystems/getDefault)
+ ^WatchService watcher (.newWatchService fs)
+ dirs-to-watch (->> (concat (get project :test-paths (list))
+ (get project :source-paths (list)))
+ (mapcat file-tree)
+ (map #(.getPath fs % (into-array String []))))
+ _ (doseq [^Path dir dirs-to-watch]
+ (.register dir watcher (into-array WatchEvent$Kind [StandardWatchEventKinds/ENTRY_MODIFY])))]
+ (do (action)
+ (loop []
+ (do (when-let [^WatchKey key (.poll watcher)]
+ (when (.isValid key)
+ (.pollEvents key)
+ (.reset key)
+ (action)))
+ (Thread/sleep 1000)
+ (recur))))
+ ))