Files
shopdb-flask/scripts/build-site.sh
cproudlock 4d5b6c3c55
Some checks failed
CI / backend (push) Failing after 2m1s
CI / naming (push) Failing after 1s
CI / frontend (push) Successful in 9s
CI / migrations-mysql (push) Failing after 8s
build(site): also stage a subpath frontend build
Vite compiles the mount path into the bundle, so it cannot be chosen at install
time from a single build - a page served under /shopdb would load and then
request its assets from /assets/, and render nothing.

build-site.sh now produces both:
  frontend-dist           base /        - the app on its own IIS site
  frontend-dist-subpath   base /<alias> - an IIS Application under an existing
                          site, e.g. http://<server-fqdn>/shopdb/

SUBPATH_ALIAS (default 'shopdb') is fixed per bundle and written into the staged
build as .alias, so the three places that must agree - the IIS application alias,
MOUNT_PATH in .env, and this compiled base - cannot drift apart. The installer
checks that marker and refuses rather than serving a page that cannot load.

The subpath build runs FIRST and is held in a temp dir: the root build has to be
last so frontend/dist is left in the state a developer expects, and the copy into
$OUT has to happen after the staging step that does rm -rf "$OUT".
2026-08-03 01:55:21 -04:00

114 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# Build a LEAN per-site artifact from a site profile (ADR-013 Phase 5).
#
# A site declares its plugins in a profile (deploy/site-profile.example.json).
# This resolves the hard-dependency closure, builds the frontend carrying only
# those plugins (via SITE_PLUGINS -> scripts/stage-frontend.mjs), and stages a
# backend tree containing core + only the chosen plugin dirs. A plugin a site
# did not choose ends up in neither the bundle nor the image.
#
# Usage: scripts/build-site.sh <site-profile.json> [output-dir]
set -euo pipefail
REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PROFILE="${1:?usage: build-site.sh <site-profile.json> [output-dir]}"
OUT="${2:-$REPO/build/site}"
[ -f "$PROFILE" ] || { echo "profile not found: $PROFILE"; exit 1; }
# Resolve the chosen plugins + their hard-dependency closure from the manifests.
CLOSURE=$(python3 - "$PROFILE" "$REPO" <<'PY'
import json, sys, os
profile_path, repo = sys.argv[1], sys.argv[2]
chosen = json.load(open(profile_path)).get('plugins', [])
plugins_dir = os.path.join(repo, 'plugins')
def deps(name):
mpath = os.path.join(plugins_dir, name, 'manifest.json')
if not os.path.exists(mpath):
sys.exit(f'profile plugin not found on disk: {name}')
out = []
for dep in json.load(open(mpath)).get('dependencies', []):
# name-only (strip any PEP440 range)
for sep in '><=!~ ':
dep = dep.split(sep)[0]
out.append(dep.strip())
return out
closure, seen = [], set()
def add(name):
if name in seen: return
seen.add(name)
for d in deps(name): add(d)
closure.append(name)
for p in chosen: add(p)
print(','.join(closure))
PY
)
echo "Site profile: $PROFILE"
echo "Plugin closure: $CLOSURE"
# Frontend: TWO builds, because Vite compiles the mount path into the bundle and
# it therefore cannot be chosen at install time from a single build - the page
# would load and then request its assets from the wrong path.
# dist -> own IIS site (method A, the default)
# dist-subpath -> IIS Application under an existing site (method B)
# SUBPATH_ALIAS is fixed per bundle so the three places that must agree - the IIS
# application alias, MOUNT_PATH and this base - cannot drift apart.
SUBPATH_ALIAS="${SUBPATH_ALIAS:-shopdb}"
SUBPATH_TMP="$(mktemp -d)"
trap 'rm -rf "$SUBPATH_TMP"' EXIT
echo "==> Building frontend for /$SUBPATH_ALIAS/ (SITE_PLUGINS=$CLOSURE) ..."
( cd "$REPO/frontend" && SITE_PLUGINS="$CLOSURE" VITE_BASE_PATH="/$SUBPATH_ALIAS/" npm run build --silent )
cp -r "$REPO/frontend/dist" "$SUBPATH_TMP/dist-subpath"
echo "$SUBPATH_ALIAS" > "$SUBPATH_TMP/dist-subpath/.alias"
# Root build LAST, so frontend/dist is left in the state a developer expects.
echo "==> Building frontend for / (SITE_PLUGINS=$CLOSURE) ..."
( cd "$REPO/frontend" && SITE_PLUGINS="$CLOSURE" npm run build --silent )
# Backend: stage core + only the chosen plugin dirs.
echo "==> Staging backend into $OUT ..."
rm -rf "$OUT"
mkdir -p "$OUT/plugins"
# cp (not rsync) so a minimal runner/deploy box without rsync can stage;
# bytecode is pruned afterwards to match the old --exclude filters.
cp -a "$REPO/shopdb" "$OUT/"
for name in ${CLOSURE//,/ }; do
cp -a "$REPO/plugins/$name" "$OUT/plugins/"
done
cp -r "$REPO/frontend/dist" "$OUT/frontend-dist"
# Staged after the rm -rf above, or it would be deleted with everything else.
cp -r "$SUBPATH_TMP/dist-subpath" "$OUT/frontend-dist-subpath"
# Runtime files a deployable tree needs beyond the Python packages. Without these
# the staged tree can be imported but not actually run or migrated, so the
# Windows installer (which consumes this output as its app\ payload) had to
# assemble them separately - and could assemble a tree whose plugin set did not
# match the profile it was built from.
cp "$REPO/wsgi.py" "$REPO/requirements.txt" "$OUT/"
cp -a "$REPO/migrations" "$OUT/"
[ -d "$REPO/deploy" ] && cp -a "$REPO/deploy" "$OUT/"
# Stage the profile INTO the tree. This is what makes the set self-describing:
# `flask plugin apply-profile` at provisioning time reads the same profile the
# tree was staged from, so the installed plugin set and the shipped plugin code
# cannot drift. It is also what `flask plugin prune-schema` effectively keys off
# (via what ends up installed), so a mismatch here would drop the wrong tables.
cp "$PROFILE" "$OUT/site-profile.json"
find "$OUT" -type d -name '__pycache__' -prune -exec rm -rf {} +
find "$OUT" -type f -name '*.pyc' -delete
echo ""
echo "Lean site staged at: $OUT"
echo " backend plugins: $(ls "$OUT/plugins" | tr '\n' ' ')"
echo " (a plugin not listed is absent from both the backend tree and the bundle)"
echo " profile staged as: $OUT/site-profile.json"
echo ""
echo "At provisioning, after 'flask db upgrade' and 'flask plugin upgrade-all':"
echo " flask plugin apply-profile site-profile.json"
echo " flask plugin prune-schema --yes --force # ADR-014; BOTH flags required"