//! Lists every file of templates/ and static/ for `src/embedded.rs`: one //! `(name, include_bytes!(..))` pair per file, sorted by name, so the binary //! carries its pages and needs no files next to it. Also a short hash of the //! static files for cache busting (`/static/app.css?v=`), and the //! commit the binary is built from (`COMMIT`). use std::fmt::Write as _; use std::path::{Path, PathBuf}; fn main() { println!("cargo:rerun-if-changed=build.rs"); let root = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()); let mut out = String::from("// Generated by build.rs.\n"); let mut hash: u64 = 0xcbf2_9ce4_8422_2325; for (name, dir) in [("TEMPLATES", "templates"), ("STATIC", "static")] { // A directory here makes cargo rerun this script when a file in it // is added or removed; each file is listed below for edits. println!("cargo:rerun-if-changed={dir}"); let base = root.join(dir); let mut files = Vec::new(); walk(&base, &base, &mut files); files.sort(); writeln!(out, "pub static {name}: &[(&str, &[u8])] = &[").unwrap(); for (rel, abs) in &files { println!("cargo:rerun-if-changed={abs}"); if dir == "static" { // FNV-1a over names and contents for b in rel.bytes().chain(std::fs::read(abs).unwrap()) { hash ^= u64::from(b); hash = hash.wrapping_mul(0x0100_0000_01b3); } } writeln!(out, " ({rel:?}, include_bytes!({abs:?})),").unwrap(); } writeln!(out, "];").unwrap(); } writeln!( out, "pub const STATIC_HASH: &str = \"{:012x}\";", hash >> 16 ) .unwrap(); // The commit, for the footer and /version. A Nix build has no .git, so // the flake passes its source's revision in VISA_COMMIT; a build in a // checkout asks git; otherwise it stays empty. println!("cargo:rerun-if-env-changed=VISA_COMMIT"); let commit = std::env::var("VISA_COMMIT") .ok() .filter(|c| !c.is_empty()) .or_else(|| git_head(&root)) .unwrap_or_default(); writeln!(out, "pub const COMMIT: &str = {commit:?};").unwrap(); let dest = Path::new(&std::env::var_os("OUT_DIR").unwrap()).join("embedded.rs"); std::fs::write(dest, out).unwrap(); } /// HEAD's commit in a git checkout. The script runs again when HEAD or the /// branch it names moves. fn git_head(root: &Path) -> Option { let git = |args: &[&str]| { std::process::Command::new("git") .args(args) .current_dir(root) .output() .ok() .filter(|o| o.status.success()) .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) }; let head = git(&["rev-parse", "HEAD"])?; let dir = PathBuf::from(git(&["rev-parse", "--absolute-git-dir"])?); let common = PathBuf::from(git(&[ "rev-parse", "--path-format=absolute", "--git-common-dir", ])?); let mut watch = vec![dir.join("HEAD"), common.join("packed-refs")]; if let Some(branch) = git(&["symbolic-ref", "-q", "HEAD"]) { watch.push(common.join(branch)); } // a path that does not exist would make cargo run the script every time for p in watch.iter().filter(|p| p.exists()) { println!("cargo:rerun-if-changed={}", p.display()); } Some(head) } fn walk(base: &Path, dir: &Path, out: &mut Vec<(String, String)>) { let Ok(entries) = std::fs::read_dir(dir) else { return; }; for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { walk(base, &path, out); continue; } let rel = path .strip_prefix(base) .unwrap() .components() .map(|c| c.as_os_str().to_string_lossy().into_owned()) .collect::>() .join("/"); out.push((rel, path.to_string_lossy().into_owned())); } }