-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·320 lines (269 loc) · 10.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·320 lines (269 loc) · 10.3 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
#!/usr/bin/env bash
# install.sh — ix-opencode-plugin installer
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/ix-infrastructure/ix-opencode-plugin/main/install.sh | bash
# curl -fsSL ... | bash -s -- --project /path/to/project
# ./install.sh --force
# ./install.sh --uninstall
set -euo pipefail
REPO_URL="https://github.com/ix-infrastructure/ix-opencode-plugin.git"
DEFAULT_SOURCE_DIR="${HOME}/.local/share/ix-opencode-plugin"
GLOBAL_CONFIG_DIR="${HOME}/.config/opencode"
# ── Flags ─────────────────────────────────────────────────────────────────────
INSTALL_MODE="global"
PROJECT_DIR="$(pwd)"
DRY_RUN=false
FORCE=false
UNINSTALL=false
SOURCE_DIR=""
usage() {
cat <<EOF
ix-opencode-plugin installer
USAGE
curl -fsSL https://raw.githubusercontent.com/ix-infrastructure/ix-opencode-plugin/main/install.sh | bash [-- OPTIONS]
./install.sh [OPTIONS]
OPTIONS
--global Install globally into ~/.config/opencode/ (default)
--project [dir] Install into .opencode/ in [dir] (default: current directory)
--source <path> Use a local plugin clone instead of downloading
--force Overwrite existing symlinks and config entries
--dry-run Print what would happen without making changes
--uninstall Remove the plugin from the target location
--help Show this message
EXAMPLES
# Global install (recommended)
curl -fsSL https://raw.githubusercontent.com/ix-infrastructure/ix-opencode-plugin/main/install.sh | bash
# Force re-install
curl -fsSL ... | bash -s -- --force
# Per-project install in current directory
curl -fsSL ... | bash -s -- --project
# Per-project install in a specific directory
curl -fsSL ... | bash -s -- --project /path/to/myproject
# Use an existing local clone (skips git clone)
./install.sh --source /path/to/ix-opencode-plugin
# Preview without making changes
./install.sh --dry-run
# Uninstall global install
./install.sh --uninstall
EOF
}
while [[ $# -gt 0 ]]; do
case $1 in
--global) INSTALL_MODE="global"; shift ;;
--project)
INSTALL_MODE="project"
if [[ $# -gt 1 && ! "${2:-}" =~ ^-- ]]; then
PROJECT_DIR="$(cd "$2" && pwd)"; shift
fi
shift ;;
--source) SOURCE_DIR="$(cd "$2" && pwd)"; shift 2 ;;
--force) FORCE=true; shift ;;
--dry-run) DRY_RUN=true; shift ;;
--uninstall) UNINSTALL=true; shift ;;
--help|-h) usage; exit 0 ;;
*) echo "Unknown flag: $1"; usage; exit 1 ;;
esac
done
# ── Helpers ───────────────────────────────────────────────────────────────────
fmt_ok() { echo " ✓ $*"; }
fmt_warn() { echo " ⚠ $*"; }
fmt_info() { echo " $*"; }
fmt_err() { echo " ✗ $*" >&2; exit 1; }
fmt_step() { echo; echo "→ $*"; }
run() {
if $DRY_RUN; then
echo " [dry-run] $*"
else
"$@"
fi
}
write_file() {
local path="$1" content="$2"
if $DRY_RUN; then
echo " [dry-run] write $path"
else
echo "$content" > "$path"
fi
}
link_or_warn() {
local src="$1" dest="$2" label="$3"
if [[ -L "$dest" ]]; then
if $FORCE; then
run rm "$dest"
else
fmt_warn "$label already linked — use --force to re-link"
return
fi
elif [[ -e "$dest" ]]; then
if $FORCE; then
run rm -rf "$dest"
else
fmt_warn "$label already exists (not a symlink) — use --force to overwrite"
return
fi
fi
run ln -s "$src" "$dest"
fmt_ok "Linked $label"
}
copy_or_warn() {
local src="$1" dest="$2" label="$3"
if [[ -e "$dest" ]]; then
if $FORCE; then
run rm -rf "$dest"
else
fmt_warn "$label already exists — use --force to overwrite"
return
fi
fi
run cp -r "$src" "$dest"
fmt_ok "Copied $label"
}
merge_opencode_json() {
local path="$1" plugin_entry="$2" agents_entry="$3"
if [[ ! -f "$path" ]]; then
write_file "$path" '{
"$schema": "https://opencode.ai/config.json",
"plugin": ["'"$plugin_entry"'"],
"instructions": ["'"$agents_entry"'"]
}'
fmt_ok "Created $(basename "$path")"
return
fi
if grep -qF "$plugin_entry" "$path" && ! $FORCE; then
fmt_warn "Plugin already registered in $(basename "$path") — use --force to re-add"
return
fi
if command -v python3 &>/dev/null; then
if $DRY_RUN; then
fmt_info "[dry-run] merge plugin/instructions entries into $path"
else
python3 - "$path" "$plugin_entry" "$agents_entry" <<'PYEOF'
import sys, json
path, plugin_entry, agents_entry = sys.argv[1], sys.argv[2], sys.argv[3]
with open(path) as f:
cfg = json.load(f)
plugins = cfg.get("plugin", [])
if plugin_entry not in plugins:
plugins.append(plugin_entry)
cfg["plugin"] = plugins
instructions = cfg.get("instructions", [])
if agents_entry not in instructions:
instructions.append(agents_entry)
cfg["instructions"] = instructions
with open(path, "w") as f:
json.dump(cfg, f, indent=2)
f.write("\n")
PYEOF
fmt_ok "Updated $(basename "$path")"
fi
else
fmt_warn "python3 not found — add these lines to $path manually:"
fmt_info " \"plugin\": [\"$plugin_entry\"]"
fmt_info " \"instructions\": [\"$agents_entry\"]"
fi
}
# ── Banner ────────────────────────────────────────────────────────────────────
echo
echo "ix-opencode-plugin"
if $DRY_RUN; then echo " (dry run — no changes will be made)"; fi
echo
# ── Prerequisites ─────────────────────────────────────────────────────────────
fmt_step "Checking prerequisites"
check_cmd() {
local name="$1" cmd="$2" hint="$3"
if command -v "$cmd" &>/dev/null; then
fmt_ok "$name"
else
fmt_err "$name not found — $hint"
fi
}
check_cmd "OpenCode" "opencode" "install from https://opencode.ai"
check_cmd "ix CLI" "ix" "install from https://github.com/ix-infrastructure/Ix"
check_cmd "Bun" "bun" "install from https://bun.sh"
# ── Resolve source directory ──────────────────────────────────────────────────
if [[ -z "$SOURCE_DIR" ]]; then
# Check if running from a local clone (./install.sh)
if [[ -n "${BASH_SOURCE[0]:-}" && -f "${BASH_SOURCE[0]}" ]]; then
CANDIDATE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "$CANDIDATE/plugins/ix-plugin.ts" ]]; then
SOURCE_DIR="$CANDIDATE"
fi
fi
# Fall back to default location (curl install or already cloned)
if [[ -z "$SOURCE_DIR" ]]; then
SOURCE_DIR="$DEFAULT_SOURCE_DIR"
fi
fi
fmt_step "Plugin source"
if [[ -f "$SOURCE_DIR/plugins/ix-plugin.ts" ]]; then
fmt_ok "Found at $SOURCE_DIR"
# Update shallow clone (curl re-installs stay current)
if [[ -d "$SOURCE_DIR/.git" ]] && command -v git &>/dev/null; then
run git -C "$SOURCE_DIR" fetch --depth 1 origin --quiet < /dev/null || true
run git -C "$SOURCE_DIR" reset --hard origin/HEAD --quiet < /dev/null || true
fmt_ok "Updated to latest"
fi
else
fmt_info "Not found at $SOURCE_DIR — cloning from GitHub..."
if ! command -v git &>/dev/null; then
fmt_err "git not found. Clone manually: git clone $REPO_URL $SOURCE_DIR"
fi
run git clone --depth 1 "$REPO_URL" "$SOURCE_DIR" < /dev/null
fmt_ok "Cloned to $SOURCE_DIR"
fi
# ── Install dependencies ──────────────────────────────────────────────────────
fmt_step "Installing dependencies"
run bash -c "cd '$SOURCE_DIR' && bun install --silent" < /dev/null
fmt_ok "@opencode-ai/plugin ready"
# ── Uninstall ─────────────────────────────────────────────────────────────────
if $UNINSTALL; then
fmt_step "Uninstalling"
if [[ "$INSTALL_MODE" == "global" ]]; then
for item in plugins commands agents runtime AGENTS.md; do
local_dest="$GLOBAL_CONFIG_DIR/$item"
if [[ -L "$local_dest" ]]; then
run rm "$local_dest"
fmt_ok "Removed $item"
fi
done
fmt_warn "opencode.json not modified — remove plugin/instructions entries manually if needed"
else
run rm -rf "$PROJECT_DIR/.opencode"
fmt_ok "Removed .opencode/"
fmt_warn "opencode.json not modified — remove plugin/instructions entries manually if needed"
fi
echo
fmt_ok "Uninstall complete"
exit 0
fi
# ── Install: global ───────────────────────────────────────────────────────────
if [[ "$INSTALL_MODE" == "global" ]]; then
fmt_step "Installing globally → $GLOBAL_CONFIG_DIR"
run mkdir -p "$GLOBAL_CONFIG_DIR"
for item in plugins commands agents runtime AGENTS.md; do
link_or_warn "$SOURCE_DIR/$item" "$GLOBAL_CONFIG_DIR/$item" "$item"
done
merge_opencode_json "$GLOBAL_CONFIG_DIR/opencode.json" \
"./plugins/ix-plugin.ts" \
"./AGENTS.md"
# ── Install: per-project ──────────────────────────────────────────────────────
else
fmt_step "Installing into project → $PROJECT_DIR/.opencode"
run mkdir -p "$PROJECT_DIR/.opencode"
for item in plugins commands agents runtime AGENTS.md; do
copy_or_warn "$SOURCE_DIR/$item" "$PROJECT_DIR/.opencode/$item" "$item"
done
merge_opencode_json "$PROJECT_DIR/opencode.json" \
".opencode/plugins/ix-plugin.ts" \
".opencode/AGENTS.md"
fi
# ── Done ──────────────────────────────────────────────────────────────────────
echo
echo "Done. Verify in OpenCode:"
echo
echo " Use the ix-health tool"
echo
echo "If the graph isn't built yet:"
echo " ix map"
echo