Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129#!/usr/bin/env bashset -euo pipefail
# Builds one microslice, or every microslice, into a static bundle.## usage: scripts/microslice.sh <name>|--all## 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.
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 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)"BINDGEN="$(wasm-bindgen --version | awk '{print $2}')"if [[ "$BINDGEN" != "$LOCKED" ]]; then echo "microslice: wasm-bindgen $BINDGEN does not match Cargo.lock's $LOCKED" >&2 exit 1fi
# 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 --release
wasm="target/wasm32-unknown-unknown/release/${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}
case "${1:-}" in "") echo "usage: $0 <name>|--all" >&2 exit 2 ;; --all) mkdir -p "$OUT" for dir in "$SLICES"/*/; do name="$(basename "$dir")" [[ "$name" == "kit" ]] && continue build_one "$name" done write_index ;; *) build_one "$1" ;;esac