Use checksums instead of last modified times.
Also add pandect dependency.
This commit is contained in:
parent
6f7cb79a65
commit
4eb0036ef5
2 changed files with 35 additions and 25 deletions
|
@ -1,4 +1,4 @@
|
||||||
(defproject cryogen-core "0.1.10"
|
(defproject cryogen-core "0.1.11-SNAPSHOT"
|
||||||
:description "Cryogen's compiler"
|
:description "Cryogen's compiler"
|
||||||
:url "https://github.com/lacarmen/cryogen-core"
|
:url "https://github.com/lacarmen/cryogen-core"
|
||||||
:license {:name "Eclipse Public License"
|
:license {:name "Eclipse Public License"
|
||||||
|
@ -13,4 +13,5 @@
|
||||||
[hiccup "1.0.5"]
|
[hiccup "1.0.5"]
|
||||||
[selmer "0.7.8"]
|
[selmer "0.7.8"]
|
||||||
[markdown-clj "0.9.61"
|
[markdown-clj "0.9.61"
|
||||||
:exclusions [com.keminglabs/cljx]]])
|
:exclusions [com.keminglabs/cljx]]
|
||||||
|
[pandect "0.4.1"]])
|
||||||
|
|
|
@ -1,24 +1,33 @@
|
||||||
(ns cryogen-core.watcher
|
(ns cryogen-core.watcher
|
||||||
(:require [clojure.java.io :refer [file]]
|
(:require [clojure.java.io :refer [file]]
|
||||||
[cryogen-core.io :refer [ignore]]))
|
[cryogen-core.io :refer [ignore]]
|
||||||
|
[pandect.core :refer [md5]]
|
||||||
|
[clojure.set :as set]))
|
||||||
|
|
||||||
(defn get-assets [root ignored-files]
|
(defn get-assets [path ignored-files]
|
||||||
(->> root
|
(->> path
|
||||||
file
|
file
|
||||||
file-seq
|
file-seq
|
||||||
(filter #(not (.isDirectory %)))
|
(filter #(not (.isDirectory %)))
|
||||||
(filter (ignore ignored-files))))
|
(filter (ignore ignored-files))))
|
||||||
|
|
||||||
(defn sum-times [path ignored-files]
|
(defn checksums [path ignored-files]
|
||||||
(->> (get-assets path ignored-files) (map #(.lastModified %)) (reduce +)))
|
(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)))))
|
||||||
|
|
||||||
(defn watch-assets [root ignored-files action]
|
(defn watch-assets [root ignored-files action]
|
||||||
(loop [times (sum-times root ignored-files)]
|
(loop [sums (checksums root ignored-files)]
|
||||||
(Thread/sleep 300)
|
(Thread/sleep 300)
|
||||||
(let [new-times (sum-times root ignored-files)]
|
(let [new-sums (checksums root ignored-files)]
|
||||||
(when-not (= times new-times)
|
(when (find-changes sums new-sums)
|
||||||
(action))
|
(action))
|
||||||
(recur new-times))))
|
(recur new-sums))))
|
||||||
|
|
||||||
(defn start-watcher! [root ignored-files action]
|
(defn start-watcher! [root ignored-files action]
|
||||||
(doto (Thread. #(watch-assets root ignored-files action))
|
(doto (Thread. #(watch-assets root ignored-files action))
|
||||||
|
|
Loading…
Reference in a new issue