(import path)
(import src/util)
(import src/threads)
(defn writer-init
"Return an empty table to be used as path cache"
[] @{})
(defn handle-writes
[output-dir]
(fn [parent self msg path-cache]
(defn ensure-path [path]
(let [s (string path)]
(unless (in path-cache s)
(util/mkpath s path-cache)
(put path-cache s true))))
(match msg
:die (do (:send parent self)
(thread/exit))
[:consume [:write path contents]]
(let [path (if output-dir (path/join output-dir path) path)
ppath (path/dirname path)]
(threads/print "[WRITE] " path)
(ensure-path ppath)
(spit path contents))
[:consume [:copy from to]]
(let [to (if output-dir (path/join output-dir to) to)
ppath (path/dirname to)]
(threads/print "[COPY] " to)
(ensure-path ppath)
(util/copy-file from to path-cache)))))