~subsetpark/bagatto

d6a048f9703c9294c7d61e32cb3831cb6922becf — Zach Smith 9 months ago d54ff08
More progress logging
4 files changed, 47 insertions(+), 18 deletions(-)

M src/core.janet
M src/loaders.janet
M src/threads.janet
M src/writers.janet
M src/core.janet => src/core.janet +1 -1
@@ 81,6 81,7 @@
    (array/push writers [type path contents]))
  
  (loop [[spec-name spec] :pairs site]
    (threads/print "Handling " spec-name "...")
    (let [with-defaults (set-defaults spec)
          filter (spec :filter)]
      (default filter (fn [_site _item] true))


@@ 88,7 89,6 @@
      (setdyn :error-context {:spec-name spec-name})
      
      (match with-defaults
        
        {:each site-selector
         :dest path-generator
         :out renderer}

M src/loaders.janet => src/loaders.janet +22 -5
@@ 1,4 1,5 @@
(import src/error)
(import src/threads)

(defn- make-attrs [parser filename &opt file-contents]
    (let [base-attrs @{:path filename :contents file-contents}]


@@ 8,29 9,45 @@
        base-attrs)))

(defn from-file-spec-loader
  ```
  A loader that takes either a single file spec or a sequence of file
  specs and sends back a list of attributes.
  ```
  [spec-name loader parser transform-f]
  (fn [parent]
    (threads/print "Loading " spec-name "...")
    (let [loader-specs (loader)
          res (match loader-specs
                {:each ind}
                (->  (seq [spec :in ind]
                         (if (indexed? spec)
                           (make-attrs parser ;spec)
                           (make-attrs parser spec)))
                    (transform-f))
                (do
                  (threads/print "[" spec-name "] Loading " (length ind) " files")
                  (->  (seq [spec :in ind]
                           (if (indexed? spec)
                             (make-attrs parser ;spec)
                             (make-attrs parser spec)))
                      (transform-f)))
                                      
                {:some spec}
                (make-attrs parser ;spec))]
      (:send parent [:res spec-name res]))))

(defn from-path-loader
  ```
  A loader that takes a literal file path and generates file
  attributes.
  ```
  [spec-name path parser]
  (fn [parent]
    (threads/print "Loading " spec-name " (" path ")...")
    (let [file-contents (slurp path)
          res (make-attrs parser path file-contents)]
      (:send parent [:res spec-name res]))))

(defn bare-attr-loader
  ```
  A loader that takes an attributes literal and returns it.
  ```
  [spec-name attrs]
  (fn [parent]
    (threads/print "Loaded " spec-name)
    (:send parent [:res spec-name attrs])))

M src/threads.janet => src/threads.janet +20 -1
@@ 36,6 36,10 @@
  [specs handler]
  
  (def pool @{})
  (def pool-size pool-size)

  (print "Starting worker pool with " pool-size " workers...")
  
  (loop [t-id :range [0 pool-size]]
    (let [f (demand-worker t-id handler)
          t (thread/new f worker-mailbox)]


@@ 48,7 52,8 @@
          dispatch [:consume spec] ]
      (:send (pool t-id) dispatch)))

  (each t pool (:send t :die)))
  (each t pool (:send t :die))
  (print "Terminated worker pool."))

(defn- one-time-worker
  [f]


@@ 65,6 70,9 @@
  Returns a mapping from `k` to `v`.
  ```
  [jobs]

  (print "Beginning " (length jobs) " jobs...")
  
  (let [workers (seq [job :in jobs] (one-time-worker job))
        res @{}]
    (var got-back 0)


@@ 75,4 83,15 @@
        (do
          (set got-back (inc got-back))
          (put res k v))))

    (print "Finished jobs.")
    res))

(defn print
  ```
  Mimic the behaviour of `print`, but concatenate the newline to the
  string and then write, rather than writing both in sequence. Ensures
  that threaded output won 't be interleaved.
  ```
  [& xs]
  (prin (string ;xs "\n")))

M src/writers.janet => src/writers.janet +4 -11
@@ 1,14 1,7 @@
(import path)
(import src/util)

(defn- print
  ```
  Mimic the behaviour of `print`, but concatenate the newline to the
  string and then write, rather than writing both in sequence. Ensures
  that threaded output won 't be interleaved.
  ```
  [s]
  (prin (string s "\n")))
(import src/util)
(import src/threads)

(defn handle-writes
  [output-dir]


@@ 19,13 12,13 @@
      [:consume [:write path contents]]
      (let [path (if output-dir (path/join output-dir path) path)
            ppath (path/dirname path)]
        (print path)
        (threads/print "[WRITE] " path)
        (util/mkpath ppath)
        (spit path contents))
      
      [:consume [:copy from to]]
      (let [to (if output-dir (path/join output-dir to) to)
            ppath (path/dirname to)]
        (print to)
        (threads/print "[COPY] " to)
        (util/mkpath ppath)
        (util/copy-file from to)))))