Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion app/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
1 change: 1 addition & 0 deletions app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ pub const OTHER_PROGRAMS: &[&str] = &[
"VISUALCI_ONCE",
"VISUALCI_ANALYTICS",
"VISUALCI_REQUIRE_WASM",
"VISUALCI_SKIP_WASM",
"VISUALCI_APP_BIN",
];

Expand Down
4 changes: 4 additions & 0 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions app/src/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions docker/app.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions frontend/scripts/wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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',
Expand Down
Loading