remove old html at build, only build pages that have been updated

This commit is contained in:
2025-05-14 14:55:26 -07:00
parent 14bde5f2dd
commit 0f2e5feb92
2 changed files with 35 additions and 13 deletions

View File

@ -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'

View File

@ -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))
@ -35,13 +48,21 @@
(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))))))))
;; 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/]] #[[./output/]])} more recent, skipping")))))