|
| 1 | +#!/bin/sh |
| 2 | +# install-oc.sh — install / update the OpenCode host from this checkout. |
| 3 | +# |
| 4 | +# WHY THIS EXISTS |
| 5 | +# |
| 6 | +# `opencode plugin add github:calionauta/leakguard` works now (see the note at the |
| 7 | +# bottom), but it installs a PINNED snapshot and only refreshes when you re-run |
| 8 | +# it. The pi host pulls from its git checkout on every start; the OpenCode host |
| 9 | +# has no equivalent. That asymmetry is how the installed plugin drifted a full |
| 10 | +# refactor behind the repo: 240-line core/ against the repo's 375, so OpenCode |
| 11 | +# silently ran without line/column positions, without the curated rule names, and |
| 12 | +# without the shared residual table. Nothing reported it, because a stale plugin |
| 13 | +# that loads is indistinguishable from a current one. |
| 14 | +# |
| 15 | +# So: one command that copies, then VERIFIES the copy matches. The verification is |
| 16 | +# the point — a copy step alone fails the same way it did before, silently. |
| 17 | +# |
| 18 | +# Usage: |
| 19 | +# scripts/install-oc.sh install or update, then verify |
| 20 | +# scripts/install-oc.sh --check verify only; exit 1 if stale (use in CI/hooks) |
| 21 | +# scripts/install-oc.sh --uninstall remove the installed plugin |
| 22 | +# |
| 23 | +# Targets: |
| 24 | +# default ~/.config/opencode/plugins/leakguard/ |
| 25 | +# OPENCODE_PLUGINS_DIR=/path/to/plugins explicit directory |
| 26 | +# |
| 27 | +# Safe to re-run. Removes the legacy single-file plugin (plugins/leakguard.ts) if |
| 28 | +# present, because OpenCode would otherwise load it as a SECOND plugin alongside |
| 29 | +# the directory. |
| 30 | + |
| 31 | +set -eu |
| 32 | + |
| 33 | +ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 34 | +PKG="$ROOT/packages/oc-leakguard" |
| 35 | +MODE="${1:-}" |
| 36 | + |
| 37 | +PLUGINS_DIR="${OPENCODE_PLUGINS_DIR:-${HOME}/.config/opencode/plugins}" |
| 38 | +DEST="$PLUGINS_DIR/leakguard" |
| 39 | +LEGACY="$PLUGINS_DIR/leakguard.ts" |
| 40 | + |
| 41 | +die() { printf '\033[31m%s\033[0m\n' "$*" >&2; exit 1; } |
| 42 | +note() { printf '%s\n' "$*" >&2; } |
| 43 | +ok() { printf '\033[32m%s\033[0m\n' "$*" >&2; } |
| 44 | + |
| 45 | +[ -d "$PKG" ] || die "missing $PKG (run from a full checkout)" |
| 46 | + |
| 47 | +# The shipped copy must match the repo's single source. If it does not, the |
| 48 | +# install would publish a stale core/ — exactly the drift this script exists to |
| 49 | +# catch, so refuse rather than copy the wrong thing. |
| 50 | +if ! sh "$ROOT/scripts/sync-core.sh" --check >/dev/null 2>&1; then |
| 51 | + die "packages/oc-leakguard/core is stale. Run: sh scripts/sync-core.sh" |
| 52 | +fi |
| 53 | + |
| 54 | +if [ "$MODE" = "--uninstall" ]; then |
| 55 | + rm -rf "$DEST" "$LEGACY" |
| 56 | + ok "removed $DEST" |
| 57 | + [ -e "$LEGACY" ] || ok "no legacy single-file plugin present" |
| 58 | + exit 0 |
| 59 | +fi |
| 60 | + |
| 61 | +if [ "$MODE" = "--check" ]; then |
| 62 | + stale=0 |
| 63 | + if [ ! -d "$DEST" ]; then |
| 64 | + note " MISSING $DEST" |
| 65 | + stale=$((stale + 1)) |
| 66 | + else |
| 67 | + for f in index.ts package.json README.md; do |
| 68 | + if ! cmp -s "$PKG/$f" "$DEST/$f" 2>/dev/null; then |
| 69 | + note " STALE $f" |
| 70 | + stale=$((stale + 1)) |
| 71 | + fi |
| 72 | + done |
| 73 | + for f in "$PKG"/core/*.ts; do |
| 74 | + base="$(basename "$f")" |
| 75 | + if ! cmp -s "$f" "$DEST/core/$base" 2>/dev/null; then |
| 76 | + note " STALE core/$base" |
| 77 | + stale=$((stale + 1)) |
| 78 | + fi |
| 79 | + done |
| 80 | + fi |
| 81 | + if [ -e "$LEGACY" ]; then |
| 82 | + note " LEFTOVER $LEGACY (loads as a second plugin; run without --check to remove)" |
| 83 | + stale=$((stale + 1)) |
| 84 | + fi |
| 85 | + if [ "$stale" -eq 0 ]; then |
| 86 | + ok "installed OpenCode plugin matches the repo" |
| 87 | + exit 0 |
| 88 | + fi |
| 89 | + die "installed OpenCode plugin is out of date ($stale difference(s))" |
| 90 | +fi |
| 91 | + |
| 92 | +[ -d "$PLUGINS_DIR" ] || die "no plugins directory at $PLUGINS_DIR — is OpenCode installed for this user?" |
| 93 | + |
| 94 | +rm -rf "$DEST" |
| 95 | +mkdir -p "$DEST/core" |
| 96 | +cp "$PKG/index.ts" "$PKG/package.json" "$PKG/README.md" "$DEST"/ |
| 97 | +cp "$PKG"/core/*.ts "$DEST/core"/ |
| 98 | + |
| 99 | +if [ -e "$LEGACY" ]; then |
| 100 | + rm -f "$LEGACY" |
| 101 | + note " removed $LEGACY (was loading as a second plugin)" |
| 102 | +fi |
| 103 | + |
| 104 | +# Verify by re-running the check, so a partial copy cannot report success. |
| 105 | +if ! sh "$ROOT/scripts/install-oc.sh" --check >/dev/null 2>&1; then |
| 106 | + die "copy completed but verification failed — inspect $DEST" |
| 107 | +fi |
| 108 | + |
| 109 | +ok "installed to $DEST" |
| 110 | +note "" |
| 111 | +note "OpenCode loads this directory at startup. Restart OpenCode to pick it up," |
| 112 | +note "then confirm in its log that the plugin loaded:" |
| 113 | +note " grep -i leakguard ~/.local/share/opencode/log/opencode.log | tail -3" |
| 114 | +note "" |
| 115 | +note "A load failure is only a WARN and does not surface in the UI. If the last" |
| 116 | +note "line for leakguard is not a successful load, the guard is NOT running." |
| 117 | + |
| 118 | +# NOTE on `opencode plugin add github:calionauta/leakguard` |
| 119 | +# |
| 120 | +# That path works as of the commit that dropped the root `workspaces` field |
| 121 | +# (anomalyco/opencode#49704: any build/prepare/preinstall/install/postinstall/ |
| 122 | +# prepack script OR a `workspaces` field makes the embedded npm fail the git |
| 123 | +# dependency prepare step). It installs a pinned snapshot into the config's |
| 124 | +# `plugins` array and re-resolves only when re-run. |
| 125 | +# |
| 126 | +# This script remains the recommended path because it (a) tracks the checkout, |
| 127 | +# (b) verifies what it wrote, and (c) cleans up the legacy single-file plugin. |
0 commit comments