Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

3.0 KB
Raw Blame
#!/usr/bin/env bash
# sdk/godot's generated files, from the GodotJS editor raea setup godot installs (apps/engines/src/godot.rs pins it):
#   src/types/*.d.ts      Godot's whole API as TypeScript, as GodotJS's codegen writes it (--generate-types, headless,
#                         on an empty project), and src/types/index.d.ts, which references each
#   src/shim/names.gen.js the "godot" module's names, which the bundles' shim answers (tools/names.ts says why)
# Run it after the pin moves: tools/gen.sh [GODOT_BINARY]
set -euo pipefail
here=$(cd "$(dirname "$0")/.." && pwd)

godot=${1:-${RAEA_GODOT:-$(git config --global raea.godot 2>/dev/null || true)}}
if [ -z "$godot" ]; then
  case "$(uname -s)" in
    Darwin) godot="$HOME/.raea/tools/godot/Godot.app/Contents/MacOS/Godot" ;;
    Linux) godot="$HOME/.raea/tools/godot/godot.linuxbsd.editor.x86_64" ;;
    *) godot="$HOME/.raea/tools/godot/godot.windows.editor.x86_64.exe" ;;
  esac
fi
[ -x "$godot" ] || { echo "gen.sh: no GodotJS editor at $godot (raea setup godot installs it)" >&2; exit 1; }

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
printf 'config_version=5\n\n[application]\nconfig/name="raea-godot-types"\n' > "$work/project.godot"
log="$work/gen.log"
if ! "$godot" --headless --editor --path "$work" --generate-types > "$log" 2>&1 || ! grep -q "Type generation complete" "$log"; then
  tail -20 "$log" >&2
  echo "gen.sh: GodotJS's codegen failed (log above)" >&2
  exit 1
fi
version=$("$godot" --version | head -1 | tr -d '\r')

rm -f "$here"/src/types/*.d.ts
cp "$work"/typings/*.d.ts "$here/src/types/"
# godot.minimal.d.ts ends with GodotJS's globals (console, the timers), which the DOM and Bun's types also declare:
# a project with either couldn't import raea/godot. Godot's scripts have them either way.
min="$here/src/types/godot.minimal.d.ts"
grep -q '^// Globals$' "$min" || { echo "gen.sh: godot.minimal.d.ts has no '// Globals' section; look at what it declares" >&2; exit 1; }
sed -i.bak '/^\/\/ Globals$/,$d' "$min" && rm -f "$min.bak"
# GodotJS writes ResourceTypes per project, one property per resource file (its res:// path: its type); the empty
# project has none, which would leave ResourceLoader.load taking no path at all
cat > "$here/src/types/resources.d.ts" <<'EOF'
// Written by tools/gen.sh (not GodotJS): ResourceTypes for a project GodotJS hasn't typed, any path a Resource.
declare module "godot" {
    interface ResourceTypes {
        [path: string]: Resource;
    }
}
EOF
{
  echo "// Generated by tools/gen.sh: Godot $version's API as TypeScript, written by GodotJS's codegen"
  echo "// (github.com/godotjs/GodotJS, MIT; Godot is MIT). raea/godot (src/index.ts) references this file."
  for f in $(cd "$here/src/types" && ls *.d.ts | grep -v '^index.d.ts$' | sort -V); do
    echo "/// <reference path=\"./$f\" />"
  done
} > "$here/src/types/index.d.ts"
bun "$here/tools/names.ts" "$here/src/types" "$here/src/shim/names.gen.js"
echo "gen.sh: $(ls "$here"/src/types/*.d.ts | wc -l | tr -d ' ') typings files from Godot $version"