Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165#!/usr/bin/env bashset -euo pipefail
# Builds one microslice, or every microslice, into a static bundle.## usage: scripts/microslice.sh <name>|--all|--budget## Each bundle lands in output/microslices/<name>/: index.html, the wasm-bindgen# glue and the wasm. `--all` also writes output/microslices/index.html listing# every slice with its package description. Serve the directory with# scripts/microslice-serve.sh.## `--budget` checks iteration speed: it builds every slice once, then touches# each slice's own source, rebuilds it and fails if that rebuild takes longer# than MICROSLICE_BUDGET_SECS (default 30).
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"cd "$REPO_ROOT"
SLICES="$REPO_ROOT/crates/microslice"OUT="$REPO_ROOT/output/microslices"TEMPLATE="$SLICES/kit/web/index.html"
# The kit embeds the frontend's fetched fonts.scripts/fetch-fonts.sh
# The runner refuses a test binary built by a different wasm-bindgen version.LOCKED="$( awk '/^name = "wasm-bindgen"$/ { found = 1 } found && /^version = / { gsub(/"/, "", $3); print $3; exit }' Cargo.lock)"# Use the wasm-bindgen on PATH when it matches the lock. Otherwise install the# locked version once into target/tools, as bevy's CLI does for the game.WASM_BINDGEN=wasm-bindgenif [[ "$(wasm-bindgen --version 2>/dev/null | awk '{print $2}')" != "$LOCKED" ]]; then TOOLS="$REPO_ROOT/target/tools/wasm-bindgen-$LOCKED" WASM_BINDGEN="$TOOLS/bin/wasm-bindgen" if [[ ! -x "$WASM_BINDGEN" ]]; then echo "microslice: installing wasm-bindgen-cli $LOCKED into $TOOLS" >&2 cargo install --quiet --locked --root "$TOOLS" wasm-bindgen-cli --version "$LOCKED" fifi
# The package name a directory's Cargo.toml declares.package_of() { python3 - "$1" <<'PY'import sys, tomllibwith open(sys.argv[1], "rb") as handle: print(tomllib.load(handle)["package"]["name"])PY}
build_one() { local name="$1" dir="$SLICES/$1" [[ -f "$dir/Cargo.toml" ]] || { echo "microslice: no slice named $name" >&2; exit 2; }
local package wasm stage package="$(package_of "$dir/Cargo.toml")" echo "==> Building $name ($package)" >&2 cargo build --package "$package" --target wasm32-unknown-unknown --profile microslice
wasm="target/wasm32-unknown-unknown/microslice/${package//-/_}.wasm" [[ -f "$wasm" ]] || { echo "microslice: expected $wasm after the build" >&2; exit 1; }
# Assemble in a temporary directory, then move it into place, so a served # bundle is never half-built. stage="$(mktemp -d)" trap 'rm -rf "$stage"' RETURN mkdir -p "$stage/build" "$WASM_BINDGEN" --target web --out-dir "$stage/build" --out-name app "$wasm" cp "$TEMPLATE" "$stage/index.html" sed -i "s/__MICROSLICE_NAME__/$name/g" "$stage/index.html"
mkdir -p "$OUT" rm -rf "${OUT:?}/$name" mv "$stage" "$OUT/$name" echo "==> Built output/microslices/$name/" >&2}
write_index() { python3 - "$SLICES" "$OUT/index.html" <<'PY'import os, sys, tomllib
slices, dest = sys.argv[1], sys.argv[2]entries = []for name in sorted(os.listdir(slices)): manifest = os.path.join(slices, name, "Cargo.toml") if name == "kit" or not os.path.isfile(manifest): continue with open(manifest, "rb") as handle: description = tomllib.load(handle)["package"].get("description", "") entries.append((name, description))
rows = "\n".join( f' <li><a href="./{name}/">{name}</a> — {description}</li>' for name, description in entries)page = f"""<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>protokill microslices</title><style> body {{ background: #0a0a0f; color: #ccc; font: 16px/1.5 monospace; margin: 3em; }} a {{ color: #6f6; }} li {{ margin: 0.4em 0; }}</style></head><body> <h1>protokill microslices</h1> <p>Each build plays one gameplay loop with the game's real rules.</p> <ul>{rows} </ul></body></html>"""with open(dest, "w") as handle: handle.write(page)print(f"==> Wrote output/microslices/index.html ({len(entries)} slices)", file=sys.stderr)PY}
check_budget() { local budget="${MICROSLICE_BUDGET_SECS:-30}" slow=0 dir name start took for dir in "$SLICES"/*/; do name="$(basename "$dir")" [[ "$name" == "kit" ]] && continue build_one "$name" >/dev/null 2>&1 touch "$dir"src/*.rs start=$SECONDS build_one "$name" >/dev/null 2>&1 took=$((SECONDS - start)) if ((took > budget)); then printf 'microslice: %s took %ss to rebuild, over the %ss budget\n' "$name" "$took" "$budget" >&2 slow=1 else printf 'microslice: %s rebuilt in %ss\n' "$name" "$took" >&2 fi done return "$slow"}
case "${1:-}" in "") echo "usage: $0 <name>|--all|--budget" >&2 exit 2 ;; --all) mkdir -p "$OUT" for dir in "$SLICES"/*/; do name="$(basename "$dir")" [[ "$name" == "kit" ]] && continue build_one "$name" done write_index ;; --budget) check_budget ;; *) build_one "$1" ;;esac