From 3279245005b83d0b1446a042f2470d42c1bebf64 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 1 Dec 2016 17:05:12 -0400 Subject: - lux-lein can now watch source-code directories to automatically build and test when changes occur. --- lux-lein/src/leiningen/lux.clj | 13 ++++++++++-- lux-lein/src/leiningen/lux/utils.clj | 11 ++++------ lux-lein/src/leiningen/lux/watch.clj | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 lux-lein/src/leiningen/lux/watch.clj (limited to 'lux-lein') 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)))) + )) -- cgit v1.2.3