master
John Lauer 2.14.3: LED row was throwing on every refresh (shortState, busySince); build now gated on behaviour 92037e6 1mo ago
#!/usr/bin/env bash
# Build and stage the adom-lbr release binary.
#
# It exists because the published binary once could not be rebuilt from the repo
# (adom/adom-lbr, 2026-08-21). Three things had drifted at once: the version was
# bumped in package.json alone, the vendored Babylon bundle was invisible in every
# source diff because it rides in via include_str!, and the wiki page's src/ was
# never synced with the GitHub tree the tarball was built from. This script makes
# the first and third mechanical, and checks the second.
#
#   ./build.sh            build, verify, stage bin/adom-lbr
#   ./build.sh 2.8.0      set the version everywhere first, then do that
set -euo pipefail
cd "$(dirname "$0")"

if [ $# -ge 1 ]; then
  V="$1"
  echo "[build] version -> $V (VERSION, Cargo.toml, package.json together)"
  printf '%s\n' "$V" > VERSION
  sed -i "0,/^version = \".*\"/s//version = \"$V\"/" Cargo.toml
  python3 - "$V" <<'PY'
import json, sys
p = json.load(open("package.json")); p["version"] = sys.argv[1]
json.dump(p, open("package.json", "w"), indent=2); open("package.json", "a").write("\n")
PY
fi

V="$(cat VERSION | tr -d '[:space:]')"
CARGO_V="$(grep -m1 '^version' Cargo.toml | cut -d'"' -f2)"
PKG_V="$(python3 -c 'import json;print(json.load(open("package.json"))["version"])')"
if [ "$V" != "$CARGO_V" ] || [ "$V" != "$PKG_V" ]; then
  echo "[build] FAIL version drift: VERSION=$V Cargo.toml=$CARGO_V package.json=$PKG_V" >&2
  echo "        run './build.sh <version>' to set all three at once" >&2
  exit 1
fi
echo "[build] version $V agrees across VERSION, Cargo.toml and package.json"

# A missing function is invisible to a byte-check. 2.14.0 shipped with api() deleted by an
# over-greedy regex: the markup and CSS were correct, every call site swallowed its own
# error, and the dashboard rendered an empty panel instead of failing. This gate fails the
# build on any identifier that is CALLED in manage.html and DEFINED nowhere.
python3 tools/js-call-check.py src/manage.html || exit 1

echo "[build] test"
cargo test --release --quiet

echo "[build] release"
cargo build --release --quiet

mkdir -p bin && cp target/release/adom-lbr bin/adom-lbr && chmod +x bin/adom-lbr

REPORTED="$(./bin/adom-lbr --version | awk '{print $2}')"
[ "$REPORTED" = "$V" ] || { echo "[build] FAIL binary reports $REPORTED, expected $V" >&2; exit 1; }

# The 3D viewer ships as FILES now, not inside the binary. Two ways that breaks
# quietly, so check both: the assets go missing (viewer generation dies at
# runtime), or they creep back into the binary (the 12 MB payload returns to every
# generate user, invisible in a source diff, which is how three releases shipped a
# bundle no committed source referenced).
[ -s assets/adom-3d-viewer.min.js ] || { echo "[build] FAIL assets/adom-3d-viewer.min.js missing or empty" >&2; exit 1; }
[ -s assets/environmentSpecular.env ] || { echo "[build] FAIL assets/environmentSpecular.env missing or empty" >&2; exit 1; }
HITS="$(grep -a -c BABYLON bin/adom-lbr || true)"
[ "${HITS:-0}" -lt 10 ] || { echo "[build] FAIL the viewer is back INSIDE the binary (BABYLON hits=$HITS); it should ship in assets/" >&2; exit 1; }
grep -q 'assets/\*\*' package.json || { echo "[build] FAIL package.json files[] does not ship assets/**" >&2; exit 1; }

# Behaviour, not bytes: run the REAL page against the REAL binary and assert what a person
# would look at (prompt rows, the two icon buttons, the LED row, the demo button), with the
# page's own swallowing catches made to confess. Byte-checks passed while 2.14.0 and 2.14.2
# shipped broken; this is the check that would have caught both.
if [ -d tools/node_modules ]; then
  NODE_PATH=tools/node_modules node tools/page-smoke.js || exit 1
else
  echo "[smoke] NOT RUN: tools/node_modules is missing, so the page's BEHAVIOUR is unverified." >&2
  echo "        run: (cd tools && npm install)" >&2
fi

printf '[build] OK  %s  binary %.2f MB + assets %.2f MB (was one %.2f MB binary)\n' "$V" \
  "$(stat -c%s bin/adom-lbr | awk '{print $1/1048576}')" \
  "$(du -sb assets | awk '{print $1/1048576}')" \
  "$(echo "$(stat -c%s bin/adom-lbr) $(du -sb assets | cut -f1)" | awk '{print ($1+$2)/1048576}')"
cat <<HINT

[build] next, and do BOTH or the page stops matching the tarball:
  git push origin main
  adom-wiki repo push adom/adom-lbr --files <changed source> -m "..."   # keep the page's src/ in step
  adom-wiki pkg publish --org adom                                      # ships bin/adom-lbr
HINT