Skip to content
Browse files

initial import

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
0 parents commit 2966202b5ddba4899fa57a242592b07398333d05 @ibdknox committed Nov 10, 2011
Showing with 114 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +13 −0 README
  3. +3 −0 project.clj
  4. +86 −0 src/watchtower/core.clj
  5. +6 −0 test/watchtower/test/core.clj
6 .gitignore
@@ -0,0 +1,6 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-failures
+.lein-deps-sum
13 README
@@ -0,0 +1,13 @@
+# watchtower
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2011 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
3 project.clj
@@ -0,0 +1,3 @@
+(defproject watchtower "0.1.0"
+ :description "A library for directory watchers"
+ :dependencies [[org.clojure/clojure "[1.2.1],[1.3.0]"]])
86 src/watchtower/core.clj
@@ -0,0 +1,86 @@
+(ns watchtower.core
+ (:require [clojure.java.io :as io]))
+
+(def ^{:dynamic true} *last-pass* nil)
+
+;;*****************************************************
+;; Watcher map creation
+;;*****************************************************
+
+(defn watcher* [dirs]
+ (let [dirs (if (string? dirs)
+ [dirs]
+ dirs)]
+ {:dirs dirs
+ :filters []}))
+
+(defn file-filter [w filt]
+ (update-in w [:filters] conj filt))
+
+(defn rate [w r]
+ (assoc w :rate r))
+
+(defn on-change [w func]
+ (update-in w [:on-change] conj func))
+
+;;*****************************************************
+;; Watcher execution
+;;*****************************************************
+
+(defn default-filter [f]
+ (.isFile f))
+
+(defn modified? [f]
+ (> (.lastModified f) @*last-pass*))
+
+(defn get-files [dirs filters]
+ (let [dir-files (mapcat #(-> % io/file file-seq) dirs)]
+ (filter filters dir-files)))
+
+(defn updated?-fn [dirs filters]
+ (let [filters (conj filters default-filter)
+ final-filter #(every? (fn [func] (func %)) filters)]
+ (fn []
+ (let [files (get-files dirs final-filter)
+ results (seq (doall (filter modified? files)))]
+ (when results
+ (reset! *last-pass* (System/currentTimeMillis)))
+ results))))
+
+(defn changed-fn [funcs]
+ (fn [files]
+ (doseq [f funcs]
+ (f files))))
+
+(defn compile-watcher [{:keys [filters rate dirs on-change]}]
+ {:rate rate
+ :updated? (updated?-fn dirs filters)
+ :changed (changed-fn on-change)})
+
+(defn watch [w]
+ (let [{:keys [updated? rate changed]} (compile-watcher w)]
+ (binding [*last-pass* (atom 0)]
+ (while true
+ (Thread/sleep rate)
+ (when-let [changes (updated?)]
+ (changed changes))))))
+
+(defmacro watcher [dirs & body]
+ `(let [w# (-> ~dirs
+ (watcher*)
+ ~@body)]
+ (future (watch w#))))
+
+;;*****************************************************
+;; file filters
+;;*****************************************************
+
+(defn ignore-dotfiles [f]
+ (not= \. (first (.getName f))))
+
+(defn extensions [& exts]
+ (let [exts-set (set (map name exts))]
+ (fn [f]
+ (let [fname (.getName f)
+ cur (subs fname (inc (.lastIndexOf fname ".")))]
+ (exts-set cur)))))
6 test/watchtower/test/core.clj
@@ -0,0 +1,6 @@
+(ns watchtower.test.core
+ (:use [watchtower.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit 2966202

Please sign in to comment.
Something went wrong with that request. Please try again.