automatically build view-thought.hy if files in thoughts updated

This commit is contained in:
2025-05-31 01:37:58 -07:00
parent 978ae8fd03
commit 7636655877

View File

@ -30,6 +30,7 @@
(defmain []
(setv last-compile-time (last-changed "../site"))
(setv data-dir-changed? (> (last-changed "data") last-compile-time))
(setv assets-dir-changed? (> (last-changed "assets") last-compile-time))
(setv last-commit-newer (> (-> (check-output #[[git log -1 --format="%at"]] :shell True) (.decode) (.strip) (int)) last-compile-time))
(print f"last compiled: {last-compile-time :.0f}")
@ -37,10 +38,21 @@
(when (not (dir? "output"))
(mkdir "output"))
(for [path (glob "./pages/**/*" :recursive True)]
(when (in "__pycache__" path)
(continue))
;; only recompile pages when:
;; - the source page is newer than the output
;; - the `src/data` directory is newer than the output
;; - the latest commit is newer than the output
;; - the page is view-thought.hy and there are thoughts being recompiled
(setv pages-to-build (lfor path (glob "./pages/**/*" :recursive True)
:if (not-in "__pycache__" path)
:if (or (> (last-changed path) (last-changed-prev-compile path)) data-dir-changed? assets-dir-changed? last-commit-newer)
path))
(when (any (map (fn [x] (in "/thoughts/" x)) pages-to-build))
(when (all (map (fn [x] (not-in "./pages/html/view-thought.hy" x)) pages-to-build))
(setv pages-to-build ["./pages/html/view-thought.hy" #* pages-to-build])))
(for [path pages-to-build]
(cond
(and (dir? path) (not (dir? (.replace path "pages" "output"))))
(do
@ -48,21 +60,13 @@
(mkdir (.replace path "pages" "output")))
(file? path)
;; only recompile pages when:
;; - the source page is newer than the output
;; - the `src/data` directory is newer than the output
;; - the latest commit is newer than the output
(if (or (> (last-changed path) (last-changed-prev-compile path)) data-dir-changed? last-commit-newer)
(do
(print f"building {path}")
(setv page-name (.split (cut path 2 -3) "/"))
(enshrine page-name)
(with [target (open (+ "./output/" (.join "/" (cut page-name 1 None)) ".html") "w")]
(.write target (form->html
(hy-eval (hy.read (with [source (open path "r")]
(.read source)))
:locals local-scope
:globals global-scope)))))
(print f"{(.replace path #[[./pages/]] #[[../site/html/]])} more recent, skipping")))))
(do
(print f"building {path}")
(setv page-name (.split (cut path 2 -3) "/"))
(enshrine page-name)
(with [target (open (+ "./output/" (.join "/" (cut page-name 1 None)) ".html") "w")]
(.write target (form->html
(hy-eval (hy.read (with [source (open path "r")]
(.read source)))
:locals local-scope
:globals global-scope))))))))