~subsetpark/bagatto

ref: d4f43c82fb988f1e77bb64c61c157d507741fb3d bagatto/src/writers.janet -rw-r--r-- 761 bytes
d4f43c82 — Zach Smith Use wait-for-fibs 4 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
(import spork/path)

(import /src/util)

(defn writer-init
  "Return an empty table to be used as path cache"
  []
  @{})

(defn ensure-path
  [path cache]
  (let [s (string path)]
    (unless (in cache s)
      (util/mkpath s cache)
      (put cache s true))))

(defn write-handler
  [output-dir path-cache]

  (fn handler
    [msg]
    (match msg
      [:write path contents]
      (let [path (if output-dir (path/join output-dir path) path)
            ppath (path/dirname path)]

        (ensure-path ppath path-cache)
        (spit path contents))

      [:copy from to]
      (let [to (if output-dir (path/join output-dir to) to)
            ppath (path/dirname to)]

        (ensure-path ppath path-cache)
        (util/copy-file from to path-cache)))))