cryogen-core/src/cryogen_core/watcher.clj

37 lines
1.2 KiB
Clojure
Raw Normal View History

2014-12-05 15:56:40 +00:00
(ns cryogen-core.watcher
2017-01-16 07:37:19 +00:00
(:require [clojure.java.io :as io]
[clojure.set :as set]
[hawk.core :as hawk]
2017-01-16 07:37:19 +00:00
[pandect.algo.md5 :as md5]
[cryogen-core.io :as cryogen-io]))
2014-12-04 16:38:48 +00:00
(defn get-assets [path ignored-files]
(->> path
2017-01-16 07:37:19 +00:00
io/file
file-seq
2015-09-14 10:06:35 +00:00
(filter #(not (.isDirectory ^java.io.File %)))
2017-01-16 07:37:19 +00:00
(filter (cryogen-io/ignore ignored-files))))
2014-12-04 16:38:48 +00:00
(defn checksums [path ignored-files]
(let [files (get-assets path ignored-files)]
2017-01-16 07:37:19 +00:00
(zipmap (map md5/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
(defn watch-assets [sums root ignored-files action]
(let [new-sums (checksums root ignored-files)]
(when (find-changes @sums new-sums)
(action)
(reset! sums new-sums))))
2014-12-04 16:38:48 +00:00
(defn start-watcher! [root ignored-files action]
(let [sums (atom (checksums root ignored-files))
handler (fn [ctx e]
(watch-assets sums root ignored-files action))]
(hawk/watch! [{:paths [root]
:handler handler}])))