Files
improvise/web/assemble.sh
T
Edward Langley de24c65119 fix: the build stamp's check must not depend on a title attribute
Self-review of the previous commit. The assemble-stamp pattern spelled the
whole opening tag, including the title text, so a purely cosmetic edit on
ui/shell-polish -- which is rewriting this header right now -- would have
failed the assembly with a message about a missing cache-bust stamp.

`id` moves last in the tag and the pattern is `id="build-version">TOKEN<`,
which still verifies the token is inside that element and nothing else.
Re-proved discriminating by injecting the missed substitution: exit 1 with
`assemble: refusing to ship; unstamped: .../app/index.html`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FC2jVn8Au7E9JPkw6bwXXH
2026-09-21 22:49:13 -07:00

113 lines
5.6 KiB
Bash
Executable File

#!/bin/sh
# Assemble the static site into `site/`: the landing page from `docs/` at the
# root, and the browser app from `web/` under `app/`.
#
# `web/` is the *source* directory: it also holds build.sh, assemble.sh and
# .gitignore, which have no business being served. This copies what ships.
#
# Extracted from the CI workflow so that the Pages job and the browser harness
# assemble the same tree — a harness that runs against a different arrangement
# of files is testing something nobody deploys (improvise-vvl3). Same reason
# `web/build.sh` exists rather than two copies of the wasm-pack invocation.
#
# Usage: web/assemble.sh [OUT_DIR] [CACHE_BUST_TOKEN]
#
# The token cache-busts the two generated bundles and names the build in the
# page's own #build-version (improvise-oujb). Pages serves everything
# `max-age=600` and the wasm-pack filenames carry no content hash, so for ten
# minutes after a deploy a returning visitor runs the PREVIOUS build — which
# during improvise-9x6 debugging made a shipped fix look like it had not
# shipped, twice. CI passes the commit SHA; a local run defaults to `dev`.
#
# Both halves are stamped, because a fresh glue file does NOT pull a fresh
# .wasm: the glue locates it with `new URL('..._bg.wasm', import.meta.url)`,
# and a URL constructed against a base does not inherit the base's query
# string. Stamping only the import specifier would refresh the wrapper and
# keep the stale binary — the failure mode this is here to end.
set -eu
root=$(cd "$(dirname "$0")/.." && pwd)
out=${1:-"$root/site"}
token=${2:-dev}
test -d "$root/web/pkg" || { echo "web/pkg missing — run web/build.sh first" >&2; exit 1; }
test -d "$root/web/worker-pkg" || { echo "web/worker-pkg missing — run web/build.sh first" >&2; exit 1; }
rm -rf "$out"
mkdir -p "$out"
# The landing page is the front door and the app is what it sends you to
# (user ruling, improvise-kh8). So `docs/` lands at the ROOT and the app moves
# under `app/` whole — every path in app.js and worker.js is relative, so the
# bundles travel with it and the generated glue needs no editing.
cp -f "$root/docs/index.html" "$out/index.html"
cp -rf "$root/docs/casts" "$out/casts"
app="$out/app"
mkdir -p "$app"
# shell.js and serial.js are imported by app.js and worker.js: leaving either
# out is a page that fails to load its own module graph, not a degraded one.
cp -f "$root/web/index.html" "$root/web/app.js" "$root/web/shell.js" \
"$root/web/serial.js" "$root/web/style.css" "$root/web/worker.js" "$app/"
# The document the page opens on a browser that has never seen one.
# web/worker.js fetches it by the ?seed= URL and hands the bytes to Rust,
# which writes them through the VFS — so leaving it out is an empty grid, not
# a 404 the page could report.
cp -f "$root/web/demo.improv" "$app/"
cp -rf "$root/web/pkg" "$app/pkg"
cp -rf "$root/web/worker-pkg" "$app/worker-pkg"
rm -f "$app/pkg/.gitignore" "$app/pkg/package.json"
rm -f "$app/worker-pkg/.gitignore" "$app/worker-pkg/package.json"
# BSD sed (macOS) needs an argument to -i; GNU sed must not be given one.
sedi() { sed "$1" "$2" > "$2.tmp" && mv -f "$2.tmp" "$2"; }
# The page must be able to say what it is: a site that cannot name its own
# build makes every bug report about it unanswerable (improvise-oujb). This is
# the same token the bundles are stamped with, and it is verified below on the
# same terms, so an unstamped page is a refusal rather than a silent `unstamped`
# on the front door.
sedi "s|>unstamped<|>${token}<|" "$app/index.html"
sedi "s|\./pkg/improvise_wasm_client\.js|&?v=${token}|" "$app/app.js"
sedi "s|\./worker-pkg/improvise_worker_server\.js|&?v=${token}|" "$app/worker.js"
sedi "s|'improvise_wasm_client_bg\.wasm'|'improvise_wasm_client_bg.wasm?v=${token}'|" \
"$app/pkg/improvise_wasm_client.js"
sedi "s|'improvise_worker_server_bg\.wasm'|'improvise_worker_server_bg.wasm?v=${token}'|" \
"$app/worker-pkg/improvise_worker_server.js"
# One `grep` over all four files succeeds when ANY one of them matches, so
# three failed substitutions could still produce a "successful" assembly under
# `set -e` (found by Copilot on PR #24). Each stamped reference is therefore
# verified on its own, by scripts/assemble-stamp.sh -- which is a separate
# script so its refusals can be exercised without fabricating a web/pkg
# (improvise-w8ih). Its argument is FILE PATTERN, five times -- the page's own
# #build-version among them (improvise-oujb).
sh "$root/scripts/assemble-stamp.sh" \
"$app/index.html" "id=\"build-version\">${token}<" \
"$app/app.js" "pkg/improvise_wasm_client.js?v=${token}" \
"$app/worker.js" "worker-pkg/improvise_worker_server.js?v=${token}" \
"$app/pkg/improvise_wasm_client.js" "improvise_wasm_client_bg.wasm?v=${token}" \
"$app/worker-pkg/improvise_worker_server.js" "improvise_worker_server_bg.wasm?v=${token}"
# Every relative import the two entry points make must have been copied. The
# cp list above is a second answer to "what ships", and a missing sibling is a
# 404 at module-load time, which the page cannot report.
for entry in app.js worker.js; do
deps=$(sed -n 's|.*from "\./\([A-Za-z0-9_.-]*\.js\)".*|\1|p' "$app/$entry")
for dep in $deps; do
test -f "$app/$dep" || {
echo "assemble: $entry imports ./$dep, which was not copied" >&2
exit 1
}
done
done
# The prose site at `docs/`, generated from context/ by the same script the
# harness serves. It runs LAST because its link check verifies the links that
# leave its own tree (`../index.html`, `../app/`) against the filesystem, which
# means their targets must already be here. It exits non-zero on a break, so a
# dead cross-reference fails the assembly rather than shipping.
node "$root/scripts/build-doc-site.mjs" "$out/docs" "$token"