Merge pull request #9 from j0ni/checksums

Use checksums instead of last modified times.
This commit is contained in:
Carmen La 2015-01-02 10:41:14 -05:00
commit 98d93e7921
2 changed files with 35 additions and 25 deletions

View file

@ -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"]])

View file

@ -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))