#!/usr/bin/env bash set -euo pipefail # Builds one microslice, or every microslice, into a static bundle. # # usage: scripts/microslice.sh |--all # # 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. 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 1 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 --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'
  • {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 } case "${1:-}" in "") echo "usage: $0 |--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