~subsetpark/bagatto

ref: c22c5b081d01751c3ed14046b11ffff11cebbfa6 bagatto/src/writers.janet -rw-r--r-- 922 bytes
c22c5b08 — Zach Smith Cache path creation 10 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
(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 [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 (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)))))