#!/usr/bin/env bash set -euo pipefail # Builds one microslice, or every microslice, into a static bundle. # # usage: scripts/microslice.sh |--all|--budget # # Each bundle lands in output/microslices//: 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-bindgen if [[ "$(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" fi fi # The package name a directory's Cargo.toml declares. package_of() { python3 - "$1" <<'PY' import sys, tomllib with 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'
  • {name} — {description}
  • ' for name, description in entries ) page = f""" protokill microslices

    protokill microslices

    Each build plays one gameplay loop with the game's real rules.

    """ 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 |--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