2014-12-05 15:56:40 +00:00
|
|
|
(ns cryogen-core.watcher
|
2014-12-27 07:38:25 +00:00
|
|
|
(:require [clojure.java.io :refer [file]]
|
2015-01-02 05:05:59 +00:00
|
|
|
[cryogen-core.io :refer [ignore]]
|
2015-09-21 13:21:05 +00:00
|
|
|
[pandect.algo.md5 :refer [md5]]
|
2015-01-02 05:05:59 +00:00
|
|
|
[clojure.set :as set]))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2015-01-02 05:05:59 +00:00
|
|
|
(defn get-assets [path ignored-files]
|
|
|
|
(->> path
|
2014-12-27 07:38:25 +00:00
|
|
|
file
|
|
|
|
file-seq
|
2015-09-14 10:06:35 +00:00
|
|
|
(filter #(not (.isDirectory ^java.io.File %)))
|
2014-12-27 07:38:25 +00:00
|
|
|
(filter (ignore ignored-files))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2015-01-02 05:05:59 +00:00
|
|
|
(defn checksums [path ignored-files]
|
|
|
|
(let [files (get-assets path ignored-files)]
|
|
|
|
(zipmap (map md5 files) files)))
|
|
|
|
|
|
|
|
(defn find-changes [old-sums new-sums]
|
|
|
|
(let [old-sum-set (-> old-sums keys set)
|
|
|
|
new-sum-set (-> new-sums keys set)]
|
|
|
|
(when-some [changes (set/difference new-sum-set old-sum-set)]
|
|
|
|
(vals (select-keys new-sums changes)))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-27 07:38:25 +00:00
|
|
|
(defn watch-assets [root ignored-files action]
|
2015-01-02 05:05:59 +00:00
|
|
|
(loop [sums (checksums root ignored-files)]
|
2014-12-04 16:38:48 +00:00
|
|
|
(Thread/sleep 300)
|
2015-01-02 05:05:59 +00:00
|
|
|
(let [new-sums (checksums root ignored-files)]
|
|
|
|
(when (find-changes sums new-sums)
|
2014-12-04 16:38:48 +00:00
|
|
|
(action))
|
2015-01-02 05:05:59 +00:00
|
|
|
(recur new-sums))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-27 07:38:25 +00:00
|
|
|
(defn start-watcher! [root ignored-files action]
|
|
|
|
(doto (Thread. #(watch-assets root ignored-files action))
|
2014-12-04 16:38:48 +00:00
|
|
|
(.setDaemon true)
|
|
|
|
(.start)))
|