~subsetpark/bagatto

bagatto/src/writers.janet -rw-r--r-- 942 bytes
4fc04b3a — Zach Smith Add render some 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(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)))))