Files
Bevy_Forge/scripts/check-generated-code.sh

114 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Bevy Forge — end-to-end generated-code gate.
#
# 1. Exports the demo project's scenes with the real `forge` CLI (`--format bsn`).
# 2. Writes the exports into the excluded `compat/bsn-smoke` crate.
# 3. Runs `cargo check` there, compiling the generated code against real
# Bevy 0.20.0 (the only Bevy dependency in the repository).
#
# Usage (from anywhere):
# bash scripts/check-generated-code.sh
#
# Environment:
# FORGE_SKIP_GENERATED=1 Skip the gate with a clear message (used only
# before the CLI / demo project exist).
# FORGE_SKIP_SECOND_SCENE=1 Only export the 3D garden scene.
# FORGE_GARDEN_SCENE=... Override the 3D scene path.
# FORGE_PLATFORMER_SCENE=... Override the 2D scene path.
# CARGO_TARGET_DIR=... Passed through to cargo (useful for a shared
# target dir; the first cold check compiles Bevy).
#
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd -- "$SCRIPT_DIR/.." && pwd)"
cd "$ROOT"
GARDEN_SCENE="${FORGE_GARDEN_SCENE:-examples/demo-project/scenes/garden.forge.ron}"
PLATFORMER_SCENE="${FORGE_PLATFORMER_SCENE:-examples/demo-project/scenes/platformer.forge.ron}"
GENERATED_DIR="compat/bsn-smoke/src"
log() { printf '[bsn-gate] %s\n' "$*"; }
fail() {
printf '[bsn-gate] ERROR: %s\n' "$*" >&2
exit 1
}
if [[ "${FORGE_SKIP_GENERATED:-0}" == "1" ]]; then
log "FORGE_SKIP_GENERATED=1: skipping the generated-code gate."
log "The committed starter scenes in compat/bsn-smoke/src were NOT regenerated."
log "The compat crate can still be checked with: (cd compat/bsn-smoke && cargo check)"
exit 0
fi
[[ -f "$GARDEN_SCENE" ]] ||
fail "scene not found: $GARDEN_SCENE (is the demo project from A05 ready?)"
[[ -f "crates/bevy_forge_cli/Cargo.toml" ]] ||
fail "CLI crate not found: crates/bevy_forge_cli (is A06 ready?)"
[[ -d "$GENERATED_DIR" ]] ||
fail "compat crate not found: $GENERATED_DIR"
# The generated files live inside modules of the smoke crate. Prefix a shim that
# (a) brings crate-root support types (the demo project's custom components)
# into scope and (b) silences the resulting redundant-prelude warning. The
# generated code itself is appended unchanged.
write_generated() {
local source="$1" destination="$2"
{
printf '%s\n' '// compat/bsn-smoke shim: expose crate-root support types (demo custom components).'
printf '%s\n' '// The generated code below this line is unchanged.'
printf '%s\n' '#![allow(unused_imports)] // the crate::* glob also provides the Bevy prelude names'
printf '%s\n' 'use crate::*;'
cat "$source"
} >"$destination"
}
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
log "Bevy Forge generated-code gate"
log "repo: $ROOT"
log "Bevy feature set: see compat/bsn-smoke/Cargo.toml (0.20.0, default-features = false)"
# --- 1. Export the 3D garden scene as BSN -------------------------------------
log "exporting BSN: $GARDEN_SCENE"
if ! CARGO_NET_OFFLINE=true cargo run -q -p bevy_forge_cli -- \
export "$GARDEN_SCENE" --format bsn -o "$TMP_DIR/garden.rs"; then
fail "forge export failed for $GARDEN_SCENE (see the error above; not a generated-code failure yet)"
fi
[[ -s "$TMP_DIR/garden.rs" ]] ||
fail "forge export produced no output for $GARDEN_SCENE"
grep -Eq 'bsn!|bsn_list!' "$TMP_DIR/garden.rs" ||
fail "exported file does not contain a bsn! macro: $GARDEN_SCENE"
write_generated "$TMP_DIR/garden.rs" "$GENERATED_DIR/generated.rs"
log "wrote $GENERATED_DIR/generated.rs ($(wc -l <"$GENERATED_DIR/generated.rs") lines)"
# --- 2. Export the 2D platformer scene (optional second module) ---------------
if [[ "${FORGE_SKIP_SECOND_SCENE:-0}" != "1" && -f "$PLATFORMER_SCENE" ]]; then
log "exporting BSN: $PLATFORMER_SCENE"
if ! CARGO_NET_OFFLINE=true cargo run -q -p bevy_forge_cli -- \
export "$PLATFORMER_SCENE" --format bsn -o "$TMP_DIR/platformer.rs"; then
fail "forge export failed for $PLATFORMER_SCENE"
fi
[[ -s "$TMP_DIR/platformer.rs" ]] ||
fail "forge export produced no output for $PLATFORMER_SCENE"
grep -Eq 'bsn!|bsn_list!' "$TMP_DIR/platformer.rs" ||
fail "exported file does not contain a BSN macro: $PLATFORMER_SCENE"
write_generated "$TMP_DIR/platformer.rs" "$GENERATED_DIR/generated_2d.rs"
log "wrote $GENERATED_DIR/generated_2d.rs ($(wc -l <"$GENERATED_DIR/generated_2d.rs") lines)"
SECOND_SCENE_NOTE=" and $(basename "$PLATFORMER_SCENE")"
else
log "skipping the 2D scene export (second scene disabled or not present); committed starter kept"
SECOND_SCENE_NOTE=""
fi
# --- 3. Compile generated code against Bevy 0.20.0 ---------------------------
log "compiling generated BSN against Bevy 0.20.0: (cd compat/bsn-smoke && cargo check)"
if ! (cd compat/bsn-smoke && CARGO_NET_OFFLINE=true cargo check); then
fail "generated BSN failed to compile against Bevy 0.20.0 (see rustc errors above; generated files were kept for diagnosis)"
fi
log "SUCCESS: generated BSN from $(basename "$GARDEN_SCENE")${SECOND_SCENE_NOTE} compiled against Bevy 0.20.0"
log "generated files: $GENERATED_DIR/generated.rs, $GENERATED_DIR/generated_2d.rs"