diff --git a/www/build.sh b/www/build.sh index a80c62b..2f221d5 100755 --- a/www/build.sh +++ b/www/build.sh @@ -1,6 +1,7 @@ #!/bin/bash mkdir data (cd src; hy build.hy) +rm -rf site/html mkdir -p site/{html,assets,scripts} cp -r src/output/* site/html echo 'html generated' diff --git a/www/src/build.hy b/www/src/build.hy index 359fdbd..10efad7 100644 --- a/www/src/build.hy +++ b/www/src/build.hy @@ -1,5 +1,6 @@ (import glob [glob]) -(import os [mkdir]) +(import subprocess [check-output]) +(import os [mkdir stat]) (import os.path [isfile :as file? isdir :as dir?]) (import templates *) (import utils *) @@ -7,6 +8,7 @@ (import hy.compiler [hy-eval]) (require hyrule.control [defmain]) +(require hyrule.argmove [->]) (setv local-scope (locals)) (setv global-scope (globals)) @@ -19,11 +21,22 @@ (lfor name names `(setv (get local-scope (str (get '(~name) 0))) ~name))) +(defn last-changed [file] + (getattr (stat file) "st_ctime")) + +(defn last-changed-prev-compile [file] + (getattr (stat (-> file (.replace ".hy" ".html") (.replace "./pages/" "../site/html/"))) "st_ctime")) + (defmain [] + (setv last-compile-time (last-changed "../site")) + (setv data-dir-changed? (> (last-changed "data") 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}") + (when (not (dir? "output")) (mkdir "output")) - (for [path (glob "./pages/**/*" :recursive True)] (when (in "__pycache__" path) (continue)) @@ -34,14 +47,22 @@ (print f"creating {path}") (mkdir (.replace path "pages" "output"))) - (file? path) - (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)))))))) + (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"))))) +