Repository navigation
Expand file tree
/
Copy pathsetup-opencode.sh
More file actions
executable file
·373 lines (344 loc) · 12 KB
/
Copy pathsetup-opencode.sh
File metadata and controls
executable file
·373 lines (344 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env bash
#
# setup-opencode.sh — Install OpenCode-compatible plugins from this
# marketplace into either ~/.config/opencode/ (global) or
# <project>/.opencode/ (per-project).
#
# Auto-discovers plugins by scanning plugin-*/{plugins/*.ts,commands/*.md,
# agents/opencode-*.md} under the repo root. No external manifest.
#
# Usage:
# setup-opencode.sh [options]
#
# Options:
# --global Install to ~/.config/opencode/
# --project <path> Install to <path>/.opencode/
# --plugin <name> Install only this plugin (repeatable)
# --list List discovered OpenCode-compatible plugins
# --dry-run Show what would happen, write nothing
# --force Overwrite existing files without prompting
# --uninstall Remove files this script installed
# (combine with --global or --project <path>)
# -h, --help Show this help text
set -euo pipefail
# --- Output colors (TTY only) ------------------------------------------------
if [[ -t 1 ]]; then
C_OK=$'\033[32m'; C_SKIP=$'\033[33m'; C_ERR=$'\033[31m'
C_HDR=$'\033[1;36m'; C_DIM=$'\033[2m'; C_RST=$'\033[0m'
else
C_OK=''; C_SKIP=''; C_ERR=''; C_HDR=''; C_DIM=''; C_RST=''
fi
# --- Defaults ----------------------------------------------------------------
SCOPE="" # "global" | "project"
PROJECT_PATH=""
ACTION="install" # "install" | "list" | "uninstall"
DRY_RUN=0
FORCE=0
SELECTED=()
REPO_ROOT=""
TRACKER_NAME=".opencode-setup-tracker"
# --- Helpers -----------------------------------------------------------------
die() { printf '%serror:%s %s\n' "$C_ERR" "$C_RST" "$*" >&2; exit 1; }
log() { printf '%s\n' "$*"; }
hdr() { printf '\n%s%s%s\n' "$C_HDR" "$*" "$C_RST"; }
dim() { printf '%s%s%s\n' "$C_DIM" "$*" "$C_RST"; }
yn_prompt() {
local prompt="$1" ans
[[ -t 0 ]] || return 1
read -r -p "$prompt" ans
[[ "$ans" =~ ^[Yy]$ ]]
}
usage() {
sed -n '2,/^set -euo/p' "$0" \
| sed 's/^# \{0,1\}//; /^$/d; /^set -euo/d'
exit 0
}
# Walk up from $0 looking for .claude-plugin/marketplace.json.
find_repo_root() {
local dir
dir="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
[[ -z "$dir" ]] && dir="$(pwd)"
while [[ "$dir" != "/" ]]; do
if [[ -f "$dir/.claude-plugin/marketplace.json" ]]; then
printf '%s\n' "$dir"
return 0
fi
dir="$(dirname "$dir")"
done
printf '%s\n' "$(pwd)"
}
# Discover OpenCode-compatible plugins.
# Emits: <name>|<kind>|<abs-source-path> kind ∈ plugins|commands|agents
discover_plugins() {
local entry name plugins_dir commands_dir agents_dir f
for entry in "$REPO_ROOT"/plugin-*/; do
[[ -d "$entry" ]] || continue
entry="${entry%/}"
name="${entry##*/}"; name="${name#plugin-}"
plugins_dir="$entry/plugins"
commands_dir="$entry/commands"
agents_dir="$entry/agents"
if [[ -d "$plugins_dir" ]]; then
for f in "$plugins_dir"/*.ts; do
[[ -f "$f" ]] || continue
printf '%s\n' "${name}|plugins|${f}"
done
fi
if [[ -d "$commands_dir" ]]; then
for f in "$commands_dir"/*.md; do
[[ -f "$f" ]] || continue
printf '%s\n' "${name}|commands|${f}"
done
fi
if [[ -d "$agents_dir" ]]; then
for f in "$agents_dir"/opencode-*.md; do
[[ -f "$f" ]] || continue
printf '%s\n' "${name}|agents|${f}"
done
fi
done
# Top-level wandavision/ holds the source-of-truth for OpenCode files.
# `plugin-wandavision/` is a Claude Code marketplace shim — it no longer
# ships `plugins/` or `commands/`, so we scan the standalone dir instead.
local wandavision_root="$REPO_ROOT/wandavision"
if [[ -d "$wandavision_root" ]]; then
plugins_dir="$wandavision_root/opencode-plugin"
if [[ -d "$plugins_dir" ]]; then
for f in "$plugins_dir"/*.ts; do
[[ -f "$f" ]] || continue
printf '%s\n' "wandavision|plugins|${f}"
done
fi
fi
}
plugin_selected() {
local name="$1"
if [[ ${#SELECTED[@]} -eq 0 ]]; then return 0; fi
local p; for p in "${SELECTED[@]}"; do [[ "$p" == "$name" ]] && return 0; done
return 1
}
# --- Argument parsing --------------------------------------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
--global) SCOPE="global"; shift ;;
--project) SCOPE="project"
PROJECT_PATH="${2:-}"
[[ -n "$PROJECT_PATH" ]] || die "--project requires a path"
shift 2 ;;
--plugin) [[ -n "${2:-}" ]] || die "--plugin requires a name"
SELECTED+=("$2"); shift 2 ;;
--list) ACTION="list"; shift ;;
--dry-run) DRY_RUN=1; shift ;;
--force) FORCE=1; shift ;;
--uninstall) ACTION="uninstall"; shift ;;
-h|--help) usage ;;
--) shift; break ;;
-*) die "Unknown option: $1 (try --help)" ;;
*) die "Unexpected positional arg: $1" ;;
esac
done
# --- Pre-flight --------------------------------------------------------------
REPO_ROOT="$(find_repo_root)"
[[ -d "$REPO_ROOT" ]] || die "Repo root not found"
DISCOVERED="$(discover_plugins | sort -u)"
# --- --list ------------------------------------------------------------------
if [[ "$ACTION" == "list" ]]; then
hdr "OpenCode-compatible plugins in $REPO_ROOT"
if [[ -z "$DISCOVERED" ]]; then
dim "(none found)"
else
printf '%-22s %-10s %s\n' "PLUGIN" "KIND" "SOURCE"
printf '%-22s %-10s %s\n' "------" "----" "------"
while IFS='|' read -r n k s; do
[[ -z "$n" ]] && continue
rel="${s#"$REPO_ROOT"/}"
printf '%-22s %-10s %s\n' "$n" "$k" "$rel"
done <<<"$DISCOVERED"
fi
printf '\n'
dim "Run with --global or --project <path> to install."
exit 0
fi
# --- Validate --plugin names against discovery --------------------------------
if [[ ${#SELECTED[@]} -gt 0 ]]; then
available="$(printf '%s\n' "$DISCOVERED" | cut -d'|' -f1 | sort -u)"
for p in "${SELECTED[@]}"; do
if ! grep -Fxq "$p" <<<"$available"; then
pretty="$(printf '%s, ' $available | sed 's/, $//')"
die "Plugin '$p' is not OpenCode-compatible. Available: $pretty"
fi
done
fi
# --- Resolve scope -----------------------------------------------------------
if [[ -z "$SCOPE" ]]; then
if [[ ! -t 0 ]]; then
die "No scope given. Use --global, --project <path>, --list, or --help."
fi
hdr "Install scope?"
echo " 1) global → ~/.config/opencode/"
echo " 2) project → <path>/.opencode/"
read -r -p "Choose [1/2]: " choice
case "$choice" in
1|g|G) SCOPE="global" ;;
2|p|P) SCOPE="project"
read -r -p "Project path: " PROJECT_PATH
[[ -n "$PROJECT_PATH" ]] || die "Project path required" ;;
*) die "Invalid choice" ;;
esac
fi
if [[ "$SCOPE" == "project" ]]; then
if [[ ! -d "$PROJECT_PATH" ]]; then
if [[ "$DRY_RUN" -eq 1 ]]; then
dim "[dry-run] would mkdir -p $PROJECT_PATH"
elif yn_prompt "Path '$PROJECT_PATH' does not exist. Create it? [y/N] "; then
mkdir -p "$PROJECT_PATH"
else
die "Aborted"
fi
fi
TARGET_ROOT="$PROJECT_PATH/.opencode"
else
TARGET_ROOT="$HOME/.config/opencode"
fi
TRACKER="$TARGET_ROOT/$TRACKER_NAME"
# --- Uninstall ---------------------------------------------------------------
if [[ "$ACTION" == "uninstall" ]]; then
hdr "Uninstall from $TARGET_ROOT"
[[ -f "$TRACKER" ]] || die "No tracker at $TRACKER. Refusing to uninstall (safety)."
removed=0; skipped=0
while IFS= read -r rel; do
[[ -z "$rel" || "$rel" == \#* ]] && continue
target="$TARGET_ROOT/$rel"
if [[ "$DRY_RUN" -eq 1 ]]; then
printf '%s[DRY-RUN]%s %s\n' "$C_SKIP" "$C_RST" "$rel"
elif [[ -f "$target" ]]; then
rm -f "$target"
printf '%s[REMOVED]%s %s\n' "$C_OK" "$C_RST" "$rel"
removed=$((removed + 1))
else
printf '%s[SKIP]%s %s (not present)\n' "$C_SKIP" "$C_RST" "$rel"
skipped=$((skipped + 1))
fi
done < "$TRACKER"
# Tidy empty subdirs.
for sub in plugins commands agents; do
[[ -d "$TARGET_ROOT/$sub" ]] || continue
[[ -z "$(ls -A "$TARGET_ROOT/$sub" 2>/dev/null || true)" ]] \
&& rmdir "$TARGET_ROOT/$sub" 2>/dev/null || true
done
[[ "$DRY_RUN" -eq 0 ]] && rm -f "$TRACKER"
hdr "Uninstall summary"
printf ' removed: %d\n skipped: %d\n' "$removed" "$skipped"
exit 0
fi
# --- Install -----------------------------------------------------------------
hdr "Install to $TARGET_ROOT"
[[ "$DRY_RUN" -eq 1 ]] && dim "[dry-run] no files will be written"
copied=(); skipped=(); failed=()
while IFS='|' read -r name kind src; do
[[ -z "$name" ]] && continue
plugin_selected "$name" || continue
target_dir="$TARGET_ROOT/$kind"
target_file="$target_dir/$(basename "$src")"
rel_target="$kind/$(basename "$src")"
if [[ -f "$target_file" ]]; then
if [[ "$FORCE" -ne 1 && "$DRY_RUN" -ne 1 ]]; then
if [[ ! -t 0 ]]; then
printf '%s[SKIP]%s %s (exists; pass --force to overwrite)\n' \
"$C_SKIP" "$C_RST" "$rel_target"
skipped+=("$rel_target")
continue
fi
read -r -p " $rel_target exists — (s)kip / (o)verwrite / (a)bort: " choice || choice="a"
case "$choice" in
o|O) ;; # fall through to overwrite
a|A) die "Aborted by user" ;;
*) printf '%s[SKIP]%s %s\n' "$C_SKIP" "$C_RST" "$rel_target"
skipped+=("$rel_target"); continue ;;
esac
fi
label="[OVERWRITE]"
else
label="[OK]"
fi
if [[ "$DRY_RUN" -eq 1 ]]; then
printf '%s[DRY-RUN]%s %s\n' "$C_SKIP" "$C_RST" "$rel_target"
continue
fi
if mkdir -p "$target_dir" && cp "$src" "$target_file"; then
printf '%s%s%s %s\n' "$C_OK" "$label" "$C_RST" "$rel_target"
copied+=("$rel_target")
else
printf '%s[FAIL]%s %s\n' "$C_ERR" "$C_RST" "$rel_target"
failed+=("$rel_target")
fi
done <<<"$DISCOVERED"
# --- Write tracker -----------------------------------------------------------
if [[ "$DRY_RUN" -ne 1 && ${#copied[@]} -gt 0 ]]; then
tmp="$(mktemp)"
{
printf '# setup-opencode.sh tracker v1\n'
printf '# installed_at: %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
printf '# repo_root: %s\n' "$REPO_ROOT"
printf '# scope: %s\n' "$SCOPE"
for f in "${copied[@]}"; do printf '%s\n' "$f"; done
} > "$tmp"
mv "$tmp" "$TRACKER"
dim "Tracker: $TRACKER"
fi
# --- Summary -----------------------------------------------------------------
hdr "Summary"
printf ' copied: %d\n' "${#copied[@]}"
printf ' skipped: %d\n' "${#skipped[@]}"
printf ' failed: %d\n' "${#failed[@]}"
# --- Hints for plugins that need extra config -------------------------------
plugin_in_scope() {
local name="$1"
if [[ ${#SELECTED[@]} -gt 0 ]]; then
printf '%s\n' "${SELECTED[@]}" | grep -Fxq "$name"
else
grep -q "^${name}|" <<<"$DISCOVERED"
fi
}
if plugin_in_scope "gh-issue-to-pr" && [[ "$DRY_RUN" -ne 1 ]]; then
hdr "Add this to your opencode.json (manual)"
cat <<'EOF'
{
"agent": {
"gh-issue-to-pr": {
"description": "Drives a single GitHub issue end-to-end to a merged PR",
"mode": "subagent",
"permission": { "edit": "allow", "bash": "allow", "webfetch": "allow" }
}
}
}
EOF
fi
if plugin_in_scope "wandavision" && [[ "$DRY_RUN" -ne 1 ]]; then
hdr "wandavision follow-up"
dim "The reminder hook is now copied to plugins/, but the MCP wrapper still"
dim "needs to be installed into the XDG data directory. Two steps:"
dim ""
dim " 1. ./setup-wandavision.sh # copies wandavision/{bin,skill,opencode-plugin}/ ->"
dim " # ~/.local/share/com.jcchikikomori.llmworkflow/wandavision/"
dim ""
dim " 2. Add this MCP entry to opencode.json:"
cat <<'EOF'
{
"mcp": {
"wandavision": {
"type": "local",
"command": ["{env:HOME}/.local/share/com.jcchikikomori.llmworkflow/wandavision/bin/run-wandavision.sh"],
"enabled": true
}
}
}
EOF
dim ""
dim "The /wandavision slash command lives in the dotfiles project (or copy"
dim "wandavision/skill/wandavision/SKILL.md's command-file equivalent into"
dim "~/.config/opencode/commands/ yourself). See plugin-wandavision/README.md."
fi
log ""
log "Done. Reload OpenCode to pick up the new plugins."
exit 0