diff --git a/app/build.rs b/app/build.rs index c5006b54..11e846c5 100644 --- a/app/build.rs +++ b/app/build.rs @@ -1,3 +1,36 @@ +//! The folders app/ embeds at compile time (database/migrations through `laravel::db::migrations!`, the repository's +//! skills/ through `include_dir!`) aren't inputs Cargo or a compile cache (kache in CI) can see: a new file in one would +//! leave the crate's key unchanged and could ship a binary without it. So each folder's contents are hashed into an +//! environment variable the code that embeds it reads (`env!`), which makes them part of the key. +use std::collections::hash_map::DefaultHasher; +use std::fs; +use std::hash::Hasher; +use std::path::Path; + +fn hash_dir(hasher: &mut DefaultHasher, root: &Path, dir: &Path) { + let mut entries: Vec<_> = fs::read_dir(dir) + .unwrap_or_else(|e| panic!("{}: {e}", dir.display())) + .map(|entry| entry.expect("a directory entry").path()) + .collect(); + entries.sort(); + for path in entries { + hasher.write(path.strip_prefix(root).unwrap().to_string_lossy().as_bytes()); + if path.is_dir() { + hash_dir(hasher, root, &path); + } else { + hasher.write(&fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))); + } + } +} + +fn contents_hash(var: &str, dir: &str) { + println!("cargo::rerun-if-changed={dir}"); + let mut hasher = DefaultHasher::new(); + hash_dir(&mut hasher, Path::new(dir), Path::new(dir)); + println!("cargo::rustc-env={var}={:016x}", hasher.finish()); +} + fn main() { - println!("cargo::rerun-if-changed=database/migrations"); + contents_hash("VISUALCI_MIGRATIONS_HASH", "database/migrations"); + contents_hash("VISUALCI_SKILLS_HASH", "../skills"); } diff --git a/app/src/config.rs b/app/src/config.rs index 95f43085..71fc4ba8 100644 --- a/app/src/config.rs +++ b/app/src/config.rs @@ -440,6 +440,7 @@ pub const OTHER_PROGRAMS: &[&str] = &[ "VISUALCI_ONCE", "VISUALCI_ANALYTICS", "VISUALCI_REQUIRE_WASM", + "VISUALCI_SKIP_WASM", "VISUALCI_APP_BIN", ]; diff --git a/app/src/lib.rs b/app/src/lib.rs index bec39fa3..6836d9b4 100644 --- a/app/src/lib.rs +++ b/app/src/lib.rs @@ -24,6 +24,10 @@ pub mod testing; use laravel::Application; use laravel::foundation::ApplicationBuilder; +/// The migrations' contents hash (build.rs): reading it makes them part of this crate's compile key, so a new +/// migration rebuilds it. +const _MIGRATIONS_HASH: &str = env!("VISUALCI_MIGRATIONS_HASH"); + pub fn configure() -> ApplicationBuilder { Application::configure() .with_migrations(laravel::db::migrations!("database/migrations")) diff --git a/app/src/skills.rs b/app/src/skills.rs index f1d59f59..8d8b7d31 100644 --- a/app/src/skills.rs +++ b/app/src/skills.rs @@ -4,6 +4,9 @@ use include_dir::{Dir, include_dir}; static SKILLS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/../skills"); +/// The skills' contents hash (build.rs): reading it makes them part of this crate's compile key, so an edited skill +/// rebuilds it. +const _SKILLS_HASH: &str = env!("VISUALCI_SKILLS_HASH"); /// One skill: its name, description and SKILL.md. pub struct Skill { diff --git a/docker/app.Dockerfile b/docker/app.Dockerfile index d16dbecb..995f8b63 100644 --- a/docker/app.Dockerfile +++ b/docker/app.Dockerfile @@ -11,6 +11,8 @@ COPY frontend/ ./ # 1 in CI's production image: the app's Plausible snippet (frontend/analytics.ts). ARG VISUALCI_ANALYTICS=0 ENV VISUALCI_ANALYTICS=${VISUALCI_ANALYTICS} +# The site's Compare two PDFs page (and its WebAssembly) is served by the site's Worker, never by the app. +ENV VISUALCI_SKIP_WASM=1 RUN if [ -f pnpm-lock.yaml ]; then pnpm build; else npm run build; fi # cargo-chef: the dependencies build in a layer of their own, rebuilt only when a Cargo.toml or Cargo.lock changes. diff --git a/frontend/scripts/wasm.mjs b/frontend/scripts/wasm.mjs index fbcf48c7..0ba841d3 100644 --- a/frontend/scripts/wasm.mjs +++ b/frontend/scripts/wasm.mjs @@ -4,6 +4,7 @@ // // Needs Rust's wasm32-unknown-unknown target (`rustup target add wasm32-unknown-unknown`). Without Rust it warns and // leaves the page's tool unable to start, unless VISUALCI_REQUIRE_WASM=1 (the production site's build), where it fails. +// VISUALCI_SKIP_WASM=1 skips it quietly, for a build that serves no site page (the app's image). import { spawnSync } from 'node:child_process' import { copyFile, mkdir, stat } from 'node:fs/promises' import { join } from 'node:path' @@ -14,6 +15,12 @@ const target = 'wasm32-unknown-unknown' const out = join(root, 'public/visualci-pdf.wasm') const required = process.env.VISUALCI_REQUIRE_WASM === '1' +// The app's image (docker/app.Dockerfile) serves no site page, so it builds without the wasm on purpose. +if (process.env.VISUALCI_SKIP_WASM === '1') { + console.log('wasm: skipped (VISUALCI_SKIP_WASM=1: this build serves no Compare two PDFs page)') + process.exit(0) +} + const build = spawnSync('cargo', ['build', '--release', '--locked', '-p', 'visualci-wasm', '--target', target], { cwd: repo, stdio: 'inherit',