#!/usr/bin/env bash # Validate every .xctemplate under Xcode/Templates/Lyrebird/. # # Three modes: # (default / --quick) # 1. plutil -lint TemplateInfo.plist — plist syntax + structure # 2. expand placeholders with Probe — sed-substitute the canonical fields # 3. swift -frontend -parse — Swift grammar check # # --full # adds: # 4. drop expanded file into Sources/Lyrebird/Generators/Math/Probe.swift # 5. swift build — verifies every API reference resolves # in-tree (sees module-internal helpers + bases). Templates importing # LyrebirdUI skip this step (LyrebirdUI is a separate module). # 6. clean up Probe.swift unconditionally — trap on exit # # --external # for every template whose body contains `import Lyrebird` (with or without # an @_spi attribute), additionally: # 7. create a temporary SPM package depending on the local Lyrebird path # (and LyrebirdUI when the template also imports LyrebirdUI) # 8. swift build that package — proves the template works for users # who consume Lyrebird as a package # Templates with no `import Lyrebird` line are skipped here (they are in-tree # contributor templates and use module-internal symbols by design). Templates # containing top-level statements or @Suite are skipped too — those compile # only inside an executableTarget / Test target respectively. # # Run from repo root: # bash scripts/validate-xcode-templates.sh # bash scripts/validate-xcode-templates.sh --full # bash scripts/validate-xcode-templates.sh --external set -euo pipefail MODE="${1:-quick}" case "$MODE" in --full|full) MODE="full" ;; --external|external) MODE="external" ;; --quick|quick|"") MODE="quick" ;; *) echo "usage: $0 [--quick|--full|--external]" >&2; exit 2 ;; esac REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" TEMPLATES_DIR="$REPO_ROOT/Sources/LyrebirdCLI/Resources/XcodeTemplates" PROBE_PATH="$REPO_ROOT/Sources/Lyrebird/Generators/Math/Probe.swift" EXT_DIR="$(mktemp -d -t xctmpl-extprobe)" if [[ ! -d "$TEMPLATES_DIR" ]]; then echo "error: $TEMPLATES_DIR not found" >&2 exit 1 fi cleanup() { rm -f "$PROBE_PATH" rm -rf "$EXT_DIR" } trap cleanup EXIT # Rewrite the external-probe Package.swift each time we switch between # Lyrebird-only and Lyrebird+LyrebirdUI dependency shapes. write_external_package() { local extra_deps="$1" # "" or ".product(name: \"LyrebirdUI\", package: \"LyrebirdII\")," mkdir -p "$EXT_DIR/Sources/extprobe" cat > "$EXT_DIR/Package.swift" < "$dst" if [[ "$strip_lyrebird_imports" == "true" ]]; then # In-tree compile: the file lives inside the Lyrebird module so any # `import Lyrebird` (with or without SPI annotation) is a self-import. sed -i.bak -E '/^@_spi\([^)]*\) import Lyrebird$/d; /^import Lyrebird$/d' "$dst" rm -f "${dst}.bak" fi } template_imports_lyrebird() { grep -qE '^(@_spi\([^)]*\) )?(@testable )?(@_spi\([^)]*\) )?import Lyrebird$' "$1" } template_imports_lyrebird_ui() { grep -qE '^import LyrebirdUI$' "$1" } # Reject from in-tree compile + external compile when the file has top-level # code (not inside a class/struct/enum/func) or declares an `@Suite` — those # compile only inside an executableTarget / Test target respectively. template_has_top_level_code() { # Crude heuristic: a non-import top-level `let engine`, `try engine.start`, # `print(`, or `exit(` outside a block. grep -qE '^(let|var) [A-Za-z_]+ =|^try engine\.start|^engine\.add|^print\(|^exit\(' "$1" } template_is_test_suite() { grep -qE '^@Suite\b' "$1" } while IFS= read -r -d '' template; do COUNT=$((COUNT + 1)) name="$(basename "$template" .xctemplate)" echo "── $name" plist="$template/TemplateInfo.plist" if [[ ! -f "$plist" ]]; then echo " FAIL: missing TemplateInfo.plist" FAIL=$((FAIL + 1)) continue fi if ! plutil -lint "$plist" >/dev/null; then echo " FAIL: plutil -lint $plist" FAIL=$((FAIL + 1)) continue fi main_swift="$template/___FILEBASENAME___.swift" if [[ ! -f "$main_swift" ]]; then echo " note: no ___FILEBASENAME___.swift (skipping Swift checks)" continue fi # ── Quick: parse only ───────────────────────────────────────────────────── tmp_parse="$(mktemp -t xctmpl).swift" expand_template "$main_swift" "$tmp_parse" false parse_log="${tmp_parse}.log" if swift -frontend -parse "$tmp_parse" 2> "$parse_log"; then lines=$(wc -l < "$tmp_parse" | tr -d ' ') echo " parse: OK ($lines lines)" else echo " FAIL: swift -frontend -parse" cat "$parse_log" FAIL=$((FAIL + 1)) rm -f "$tmp_parse" "$parse_log" continue fi rm -f "$tmp_parse" "$parse_log" # ── Full: compile against Lyrebird in-tree ──────────────────────────────── # Skip in-tree compile for templates that import LyrebirdUI (separate module # not visible from Sources/Lyrebird/), for test suites (need a Test target), # and for executable main.swift (need an executableTarget). if [[ "$MODE" == "full" || "$MODE" == "external" ]]; then if template_imports_lyrebird_ui "$main_swift"; then echo " build (in-tree): SKIP (imports LyrebirdUI)" elif template_is_test_suite "$main_swift"; then echo " build (in-tree): SKIP (test suite — needs Test target)" elif template_has_top_level_code "$main_swift"; then echo " build (in-tree): SKIP (top-level code — needs executableTarget)" else expand_template "$main_swift" "$PROBE_PATH" true build_log="$(mktemp -t xctmpl-build).log" if (cd "$REPO_ROOT" && swift build) > "$build_log" 2>&1; then echo " build (in-tree): OK" else echo " FAIL: swift build (in-tree)" tail -40 "$build_log" FAIL=$((FAIL + 1)) rm -f "$build_log" rm -f "$PROBE_PATH" continue fi rm -f "$build_log" rm -f "$PROBE_PATH" fi fi # ── External: compile against Lyrebird as a package dependency ──────────── if [[ "$MODE" == "external" ]]; then if ! template_imports_lyrebird "$main_swift"; then echo " build (external): SKIP (in-tree-only template)" continue fi if template_is_test_suite "$main_swift"; then echo " build (external): SKIP (test suite — needs Test target)" continue fi if template_has_top_level_code "$main_swift"; then echo " build (external): SKIP (top-level code — needs executableTarget)" continue fi if template_imports_lyrebird_ui "$main_swift"; then write_external_package '.product(name: "LyrebirdUI", package: "LyrebirdII"),' else write_external_package '' fi expand_template "$main_swift" "$EXT_DIR/Sources/extprobe/Probe.swift" false ext_log="$(mktemp -t xctmpl-ext).log" if (cd "$EXT_DIR" && swift build) > "$ext_log" 2>&1; then echo " build (external): OK" else echo " FAIL: swift build (external)" tail -60 "$ext_log" FAIL=$((FAIL + 1)) fi rm -f "$ext_log" fi done < <(find "$TEMPLATES_DIR" -type d -name "*.xctemplate" -print0) echo if [[ $FAIL -gt 0 ]]; then echo "✗ $FAIL of $COUNT templates failed" exit 1 else echo "✓ $COUNT templates validated ($MODE)" fi