2026-06-16 07:28:45 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
# register-guards.sh — Detect available harnesses and install the appropriate
|
|
|
|
|
# automaton pre-edit guards.
|
|
|
|
|
#
|
|
|
|
|
# Called by install.sh and update.sh during framework setup.
|
|
|
|
|
#
|
|
|
|
|
# Detection:
|
2026-06-22 10:40:58 -04:00
|
|
|
# - OpenCode: checks for ~/.config/opencode/opencode.json or .jsonc
|
2026-06-16 07:28:45 -04:00
|
|
|
# - Pi Dev: checks for `pi` in PATH
|
|
|
|
|
#
|
|
|
|
|
# Usage: bash ~/.automaton/scripts/register-guards.sh
|
|
|
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
|
|
FRAMEWORK_DIR="$HOME/.automaton"
|
|
|
|
|
INSTALLED_OPENCODE=false
|
|
|
|
|
INSTALLED_PI=false
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "=== Pre-Edit Guard Registration ==="
|
|
|
|
|
|
|
|
|
|
# OpenCode guard
|
2026-06-22 10:40:58 -04:00
|
|
|
# Check for both opencode.json (default) and opencode.jsonc
|
|
|
|
|
OPENCODE_CONFIG=""
|
|
|
|
|
for candidate in "$HOME/.config/opencode/opencode.json" "$HOME/.config/opencode/opencode.jsonc"; do
|
|
|
|
|
if [ -f "$candidate" ]; then
|
|
|
|
|
OPENCODE_CONFIG="$candidate"
|
|
|
|
|
break
|
|
|
|
|
fi
|
|
|
|
|
done
|
2026-06-16 07:28:45 -04:00
|
|
|
OPENCODE_SOURCE="$FRAMEWORK_DIR/plugins/automaton-guard"
|
2026-06-22 10:40:58 -04:00
|
|
|
if [ -n "$OPENCODE_CONFIG" ]; then
|
2026-06-16 07:28:45 -04:00
|
|
|
if grep -q "automaton-guard" "$OPENCODE_CONFIG" 2>/dev/null; then
|
|
|
|
|
echo "OpenCode: already registered"
|
|
|
|
|
else
|
|
|
|
|
echo "OpenCode: registering guard plugin..."
|
2026-06-22 10:40:58 -04:00
|
|
|
# Use 'plugin' key (singular) per opencode config schema.
|
|
|
|
|
# Strip // comments (JSONC) before parsing for robustness.
|
2026-06-16 07:28:45 -04:00
|
|
|
python3 -c "
|
2026-06-22 10:40:58 -04:00
|
|
|
import json, re
|
2026-06-16 07:28:45 -04:00
|
|
|
with open('$OPENCODE_CONFIG') as f:
|
2026-06-22 10:40:58 -04:00
|
|
|
text = f.read()
|
|
|
|
|
# Strip single-line // comments (JSONC) outside of strings
|
|
|
|
|
text = re.sub(r'//.*?\$', '', text)
|
|
|
|
|
cfg = json.loads(text)
|
|
|
|
|
cfg.setdefault('plugin', []).append('$OPENCODE_SOURCE')
|
2026-06-16 07:28:45 -04:00
|
|
|
with open('$OPENCODE_CONFIG', 'w') as f:
|
|
|
|
|
json.dump(cfg, f, indent=2)
|
|
|
|
|
"
|
|
|
|
|
INSTALLED_OPENCODE=true
|
|
|
|
|
echo "OpenCode: registered (restart opencode to activate)"
|
|
|
|
|
fi
|
|
|
|
|
else
|
2026-06-22 10:40:58 -04:00
|
|
|
echo "OpenCode: not detected (no ~/.config/opencode/opencode.json or .jsonc)"
|
2026-06-16 07:28:45 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Pi Dev guard
|
|
|
|
|
PI_SOURCE="$FRAMEWORK_DIR/plugins/automaton-guard-pi"
|
|
|
|
|
if command -v pi &>/dev/null; then
|
|
|
|
|
INSTALLED=$(pi list 2>/dev/null | grep -c "automaton-guard-pi" || true)
|
|
|
|
|
if [ "$INSTALLED" -gt 0 ]; then
|
|
|
|
|
echo "Pi Dev: already installed"
|
|
|
|
|
else
|
|
|
|
|
echo "Pi Dev: installing guard extension..."
|
|
|
|
|
pi install "$PI_SOURCE" 2>&1 | sed 's/^/ /'
|
|
|
|
|
INSTALLED_PI=true
|
|
|
|
|
echo "Pi Dev: installed"
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "Pi Dev: not detected (pi not in PATH)"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
if $INSTALLED_OPENCODE || $INSTALLED_PI; then
|
|
|
|
|
echo "Restart your harness for the guard to take effect."
|
|
|
|
|
fi
|
|
|
|
|
if ! $INSTALLED_OPENCODE && ! $INSTALLED_PI; then
|
|
|
|
|
echo "No harness detected. To install a guard manually:"
|
2026-06-22 10:40:58 -04:00
|
|
|
echo " OpenCode: add '\"plugin\": [\"$OPENCODE_SOURCE\"]' to ~/.config/opencode/opencode.json"
|
2026-06-16 07:28:45 -04:00
|
|
|
echo " Pi Dev: pi install $PI_SOURCE"
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Without a pre-edit guard, git hooks (pre-commit + pre-push)"
|
|
|
|
|
echo "provide enforcement at commit/push time instead."
|
|
|
|
|
fi
|