From 76366558772ff37dab77868680d3cb33c7e6e5b2 Mon Sep 17 00:00:00 2001 From: nat Date: Sat, 31 May 2025 01:37:58 -0700 Subject: [PATCH] automatically build view-thought.hy if files in thoughts updated --- www/src/build.hy | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/www/src/build.hy b/www/src/build.hy index 10efad7..90bf6e9 100644 --- a/www/src/build.hy +++ b/www/src/build.hy @@ -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))))))))