chobit/run
Claude Code 5b03041db0 scripts(execution): 🔨 Update startup scripts and PID files for improved Godot Engine runtime behavior
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-03-27 21:03:08 -07:00

151 lines
3.7 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
GODOT_DIR="$ROOT/godot"
TRAY_DIR="$ROOT/tray"
GODOT="flatpak run --user org.godotengine.Godot"
PIDFILE="$ROOT/.godot.pid"
TRAY_PIDFILE="$ROOT/.tray.pid"
cmd_start() {
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
echo "Already running (pid $(cat "$PIDFILE"))"
return 1
fi
setsid $GODOT --path "$GODOT_DIR" &
echo $! > "$PIDFILE"
echo "Started Godot (pid $!)"
# Start tray sidecar (manages bridge + vision subprocesses)
if [ -f "$TRAY_DIR/chobit_tray.py" ]; then
python3 "$TRAY_DIR/chobit_tray.py" &
echo $! > "$TRAY_PIDFILE"
echo "Started tray (pid $!)"
fi
}
cmd_stop() {
# Stop tray (also stops bridge + vision subprocesses via atexit)
if [ -f "$TRAY_PIDFILE" ]; then
local tray_pid
tray_pid=$(cat "$TRAY_PIDFILE" 2>/dev/null)
if [ -n "$tray_pid" ] && kill -0 "$tray_pid" 2>/dev/null; then
kill "$tray_pid" 2>/dev/null && echo "Stopped tray (pid $tray_pid)" || true
fi
rm -f "$TRAY_PIDFILE"
fi
# Kill any remaining tray processes
pgrep -f "chobit_tray\\.py" | while read -r cpid; do
kill "$cpid" 2>/dev/null
done || true
# Stop Godot — kill by PID file first, then sweep for stragglers
local stopped=0
if [ -f "$PIDFILE" ]; then
local pid
pid=$(cat "$PIDFILE" 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
kill -- -"$pid" 2>/dev/null || kill "$pid" 2>/dev/null || true
pkill -P "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
echo "Stopped Godot (pid $pid)"
stopped=1
fi
rm -f "$PIDFILE"
fi
# Sweep: kill any godot-bin instances running with @chobit's godot dir.
# Inside flatpak the cmdline is "godot-bin --path godot" (relative),
# so match that pattern plus the absolute-path variant.
local sweep_count=0
for cpid in $(pgrep -f "godot-bin.*(--path godot|@chobit/godot)" 2>/dev/null); do
kill "$cpid" 2>/dev/null && sweep_count=$((sweep_count + 1))
done
# Also kill the bwrap/sh wrappers that flatpak spawned for these
for cpid in $(pgrep -f "bwrap.*-- godot --path godot" 2>/dev/null); do
kill "$cpid" 2>/dev/null
done
if [ "$stopped" -eq 0 ] && [ "$sweep_count" -eq 0 ]; then
echo "Godot not running"
elif [ "$sweep_count" -gt 0 ]; then
echo "Swept $sweep_count stale Godot process(es)"
fi
}
cmd_restart() {
cmd_stop
sleep 2
cmd_start
}
cmd_verify() {
local failed=0
echo "=== Lint ==="
if (cd "$GODOT_DIR" && gdlint scripts/); then
echo "PASS"
else
echo "FAIL"
failed=1
fi
echo ""
echo "=== Format Check ==="
if (cd "$GODOT_DIR" && gdformat --check scripts/ 2>&1); then
echo "PASS"
else
echo "FAIL (run: cd godot && gdformat scripts/)"
failed=1
fi
echo ""
echo "=== Godot Import ==="
local import_errors
import_errors=$($GODOT --headless --path "$GODOT_DIR" --import 2>&1 | grep -iE "error|fail" || true)
if [ -z "$import_errors" ]; then
echo "PASS"
else
echo "$import_errors"
echo "FAIL"
failed=1
fi
echo ""
if [ "$failed" -eq 0 ]; then
echo "All checks passed."
else
echo "Verification failed."
return 1
fi
}
cmd_editor() {
$GODOT --editor --path "$GODOT_DIR"
}
cmd_screenshot() {
$GODOT --path "$GODOT_DIR" --script tools/screenshot.gd 2>&1 | tail -1
}
case "${1:-}" in
""|start) cmd_start ;;
stop) cmd_stop ;;
restart) cmd_restart ;;
verify) cmd_verify ;;
editor) cmd_editor ;;
screenshot) cmd_screenshot ;;
*)
echo "Usage: ./run [command]"
echo ""
echo "Commands:"
echo " (none), start Launch companion + tray"
echo " stop Stop everything"
echo " restart Stop then start"
echo " verify Run lint, format check, and Godot import"
echo " editor Open Godot editor"
echo " screenshot Capture a screenshot"
exit 1
;;
esac