forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·521 lines (438 loc) · 13.3 KB
/
install
File metadata and controls
executable file
·521 lines (438 loc) · 13.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env bash
set -euo pipefail
APP="opencode"
RELEASE_REPO_DEFAULT="heidi-dang/opencode"
OHMY_REPO_URL="https://github.com/heidi-dang/oh-my-opencode"
MUTED='\033[0;2m'
RED='\033[0;31m'
ORANGE='\033[38;5;214m'
GREEN='\033[0;32m'
NC='\033[0m'
requested_version="${VERSION:-}"
binary_path=""
no_modify_path=false
heidi_dang=false
local_repo=false
repo_path=""
release_repo="$RELEASE_REPO_DEFAULT"
ohmy_branch="main"
ROOT_DIR="$PWD"
INSTALL_DIR="${HOME}/.opencode/bin"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
OPENCODE_CONFIG_DIR="${XDG_CONFIG_HOME}/opencode"
PLUGIN_ROOT="${OPENCODE_CONFIG_DIR}/plugins"
PLUGIN_PATH="${PLUGIN_ROOT}/oh-my-opencode"
CONFIG_PATH="${OPENCODE_CONFIG_DIR}/opencode.json"
LOCK_DIR="${TMPDIR:-/tmp}/opencode-install.lock"
usage() {
cat <<EOF
OpenCode Installer
Usage:
./install [options]
Options:
-h, --help Show help
-v, --version <version> Install specific release version
-b, --binary <path> Install from an existing binary
--local-repo Build and install from the local opencode repo
--repo-path <path> Repo path for --local-repo (default: current directory)
--heidi-dang Clone/build latest oh-my-opencode and register local plugin path
--oh-my-branch <name> Branch for oh-my-opencode (default: main)
--release-repo <slug> Release repo owner/name (default: heidi-dang/opencode)
--no-modify-path Do not edit shell rc files
Examples:
./install
./install --version 1.2.20
./install --binary /path/to/opencode
./install --local-repo
./install --repo-path ~/work/opencode-heidi --local-repo
./install --heidi-dang --local-repo
EOF
}
print_message() {
local level="$1"
local message="$2"
local color="$NC"
case "$level" in
info) color="$NC" ;;
warning) color="$ORANGE" ;;
error) color="$RED" ;;
success) color="$GREEN" ;;
esac
echo -e "${color}${message}${NC}"
}
fail() {
print_message error "$1"
exit 1
}
cleanup() {
rm -rf "$LOCK_DIR" 2>/dev/null || true
}
trap cleanup EXIT
acquire_lock() {
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
fail "Another install appears to be running. Remove $LOCK_DIR if this is stale."
fi
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "Required command not found: $1"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
[[ -n "${2:-}" ]] || fail "--version requires a value"
requested_version="$2"
shift 2
;;
-b|--binary)
[[ -n "${2:-}" ]] || fail "--binary requires a value"
binary_path="$2"
shift 2
;;
--local-repo)
local_repo=true
shift
;;
--repo-path)
[[ -n "${2:-}" ]] || fail "--repo-path requires a value"
repo_path="$2"
shift 2
;;
--heidi-dang)
heidi_dang=true
shift
;;
--oh-my-branch)
[[ -n "${2:-}" ]] || fail "--oh-my-branch requires a value"
ohmy_branch="$2"
shift 2
;;
--release-repo)
[[ -n "${2:-}" ]] || fail "--release-repo requires a value"
release_repo="$2"
shift 2
;;
--no-modify-path)
no_modify_path=true
shift
;;
*)
fail "Unknown option '$1'"
;;
esac
done
if [[ -n "$binary_path" && "$local_repo" == "true" ]]; then
fail "--binary and --local-repo cannot be used together"
fi
if [[ -n "$binary_path" && -n "$requested_version" ]]; then
fail "--binary and --version cannot be used together"
fi
if [[ "$local_repo" == "true" && -n "$requested_version" ]]; then
fail "--local-repo and --version cannot be used together"
fi
if [[ -z "$repo_path" ]]; then
repo_path="$ROOT_DIR"
fi
}
detect_platform() {
local raw_os arch combo
raw_os="$(uname -s)"
arch="$(uname -m)"
case "$raw_os" in
Darwin*) OS="darwin" ;;
Linux*) OS="linux" ;;
MINGW*|MSYS*|CYGWIN*) OS="windows" ;;
*) fail "Unsupported OS: $raw_os" ;;
esac
case "$arch" in
x86_64) ARCH="x64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) fail "Unsupported architecture: $arch" ;;
esac
combo="${OS}-${ARCH}"
case "$combo" in
linux-x64|linux-arm64|darwin-x64|darwin-arm64|windows-x64) ;;
*) fail "Unsupported OS/Arch: ${combo}" ;;
esac
IS_MUSL=false
NEEDS_BASELINE=false
if [[ "$OS" == "linux" ]]; then
if [[ -f /etc/alpine-release ]]; then
IS_MUSL=true
elif command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl; then
IS_MUSL=true
fi
fi
if [[ "$ARCH" == "x64" ]]; then
if [[ "$OS" == "linux" ]]; then
if ! grep -qwi avx2 /proc/cpuinfo 2>/dev/null; then
NEEDS_BASELINE=true
fi
elif [[ "$OS" == "darwin" ]]; then
local avx2
avx2="$(sysctl -n hw.optional.avx2_0 2>/dev/null || echo 0)"
if [[ "$avx2" != "1" ]]; then
NEEDS_BASELINE=true
fi
fi
fi
TARGET="${OS}-${ARCH}"
[[ "$NEEDS_BASELINE" == "true" ]] && TARGET="${TARGET}-baseline"
[[ "$IS_MUSL" == "true" ]] && TARGET="${TARGET}-musl"
if [[ "$OS" == "linux" ]]; then
ARCHIVE_EXT=".tar.gz"
else
ARCHIVE_EXT=".zip"
fi
FILENAME="${APP}-${TARGET}${ARCHIVE_EXT}"
}
install_from_binary() {
[[ -f "$binary_path" ]] || fail "Binary not found at $binary_path"
mkdir -p "$INSTALL_DIR"
cp "$binary_path" "${INSTALL_DIR}/opencode"
chmod 755 "${INSTALL_DIR}/opencode"
print_message success "Installed opencode from binary: $binary_path"
}
fetch_latest_release_version() {
require_cmd curl
curl -fsSL "https://api.github.com/repos/${release_repo}/releases/latest" \
| sed -n 's/.*"tag_name": *"v\{0,1\}\([^"]*\)".*/\1/p' \
| head -n1
}
install_from_release() {
require_cmd curl
detect_platform
local version url tmp_dir archive_path
if [[ -z "$requested_version" ]]; then
version="$(fetch_latest_release_version)"
[[ -n "$version" ]] || fail "Failed to determine latest release version from ${release_repo}"
else
version="${requested_version#v}"
fi
url="https://github.com/${release_repo}/releases/download/v${version}/${FILENAME}"
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/opencode-install.XXXXXX")"
archive_path="${tmp_dir}/${FILENAME}"
print_message info "Installing ${APP} release from ${release_repo} version ${version}"
curl -fL# "$url" -o "$archive_path"
if [[ "$OS" == "linux" ]]; then
require_cmd tar
tar -xzf "$archive_path" -C "$tmp_dir"
else
require_cmd unzip
unzip -q "$archive_path" -d "$tmp_dir"
fi
[[ -f "${tmp_dir}/opencode" ]] || fail "Release archive did not contain an opencode binary"
mkdir -p "$INSTALL_DIR"
cp "${tmp_dir}/opencode" "${INSTALL_DIR}/opencode"
chmod 755 "${INSTALL_DIR}/opencode"
rm -rf "$tmp_dir"
print_message success "Installed opencode ${version} from ${release_repo}"
}
find_local_binary() {
local expected fallback
expected="${repo_path}/packages/opencode/dist/opencode-${TARGET}/bin/opencode"
if [[ -f "$expected" ]]; then
echo "$expected"
return 0
fi
fallback="$(find "${repo_path}/packages/opencode/dist" -type f -name opencode 2>/dev/null | head -n1 || true)"
if [[ -n "$fallback" && -f "$fallback" ]]; then
echo "$fallback"
return 0
fi
return 1
}
install_from_local_repo() {
require_cmd bun
detect_platform
[[ -f "${repo_path}/package.json" ]] || fail "--local-repo must point at the opencode repo root (missing package.json)"
[[ -d "${repo_path}/packages/opencode" ]] || fail "--local-repo must point at the opencode repo root (missing packages/opencode)"
local current_branch local_bin
current_branch="$(git -C "$repo_path" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")"
print_message info "Building opencode from local repo: ${repo_path}"
print_message info "Local opencode branch: ${current_branch}"
(
cd "$repo_path"
if [[ -f bun.lock || -f bun.lockb ]]; then
bun install --frozen-lockfile
else
bun install
fi
bun run --cwd packages/opencode build --single || bun run --cwd packages/opencode build
)
local_bin="$(find_local_binary)" || fail "Could not find built opencode binary under ${repo_path}/packages/opencode/dist"
mkdir -p "$INSTALL_DIR"
cp "$local_bin" "${INSTALL_DIR}/opencode"
chmod 755 "${INSTALL_DIR}/opencode"
print_message success "Installed local opencode build from branch ${current_branch}"
}
merge_plugin_path_into_config() {
require_cmd python3
mkdir -p "$OPENCODE_CONFIG_DIR"
if [[ ! -f "$CONFIG_PATH" ]]; then
cat > "$CONFIG_PATH" <<EOF
{
"\$schema": "https://opencode.ai/config.json",
"plugin": []
}
EOF
fi
python3 - "$CONFIG_PATH" "$PLUGIN_PATH" <<'PY'
import json, os, sys
config_path = sys.argv[1]
plugin_path = sys.argv[2]
with open(config_path, "r", encoding="utf-8") as f:
data = json.load(f)
plugins = data.get("plugin")
if not isinstance(plugins, list):
plugins = []
plugins = [p for p in plugins if p not in (plugin_path, "@heidi-dang/oh-my-opencode")]
plugins.append(plugin_path)
data["plugin"] = plugins
data.setdefault("$schema", "https://opencode.ai/config.json")
tmp = config_path + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
f.write("\n")
os.replace(tmp, config_path)
PY
}
setup_heidi_plugin() {
require_cmd git
require_cmd python3
mkdir -p "$PLUGIN_ROOT"
local build_cache latest_commit last_built commit_changed=false
build_cache="${PLUGIN_PATH}/.opencode-build"
if [[ ! -d "${PLUGIN_PATH}/.git" ]]; then
print_message info "Cloning oh-my-opencode (${ohmy_branch}) from GitHub"
git clone "$OHMY_REPO_URL" "$PLUGIN_PATH"
fi
(
cd "$PLUGIN_PATH"
git fetch --force origin "$ohmy_branch"
git checkout "$ohmy_branch"
git reset --hard "FETCH_HEAD"
git clean -fd
)
latest_commit="$(git -C "$PLUGIN_PATH" rev-parse HEAD)"
last_built="$(cat "$build_cache" 2>/dev/null || true)"
if [[ "$latest_commit" != "$last_built" ]]; then
commit_changed=true
fi
if [[ "$commit_changed" == "true" ]]; then
print_message info "Building oh-my-opencode branch=${ohmy_branch} commit=${latest_commit}"
(
cd "$PLUGIN_PATH"
if command -v bun >/dev/null 2>&1; then
if [[ -f bun.lock || -f bun.lockb ]]; then
if ! bun install --frozen-lockfile; then
print_message warning "oh-my-opencode lockfile drift detected on ${ohmy_branch}; retrying with non-frozen bun install"
bun install
fi
else
bun install
fi
bun run build
else
npm ci || npm install
npm run build
fi
)
[[ -d "${PLUGIN_PATH}/dist" || -d "${PLUGIN_PATH}/build" ]] || fail "oh-my-opencode build output not found under ${PLUGIN_PATH}"
printf '%s\n' "$latest_commit" > "$build_cache"
else
print_message info "oh-my-opencode already up to date at ${latest_commit}; skipping rebuild"
fi
merge_plugin_path_into_config
print_message success "Registered local oh-my-opencode plugin path in ${CONFIG_PATH}"
}
add_to_path() {
local config_file="$1"
local command="$2"
grep -Fxq "$command" "$config_file" 2>/dev/null && return 0
if [[ -w "$config_file" ]]; then
{
echo
echo "# opencode"
echo "$command"
} >> "$config_file"
print_message info "Added opencode to PATH in ${config_file}"
else
print_message warning "Could not update ${config_file}. Add this manually:"
echo " $command"
fi
}
ensure_path() {
[[ "$no_modify_path" == "true" ]] && return 0
local current_shell config_files config_file=""
current_shell="$(basename "${SHELL:-bash}")"
case "$current_shell" in
fish)
config_files="$HOME/.config/fish/config.fish"
;;
zsh)
config_files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv"
;;
bash)
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile"
;;
*)
config_files="$HOME/.bashrc $HOME/.profile"
;;
esac
for file in $config_files; do
if [[ -f "$file" ]]; then
config_file="$file"
break
fi
done
if [[ -z "$config_file" ]]; then
print_message warning "No shell config file found. Add this manually:"
echo " export PATH=${INSTALL_DIR}:\$PATH"
return 0
fi
if [[ ":$PATH:" != *":${INSTALL_DIR}:"* ]]; then
case "$current_shell" in
fish) add_to_path "$config_file" "fish_add_path ${INSTALL_DIR}" ;;
*) add_to_path "$config_file" "export PATH=${INSTALL_DIR}:\$PATH" ;;
esac
fi
}
main() {
acquire_lock
parse_args "$@"
mkdir -p "$INSTALL_DIR"
if [[ -n "$binary_path" ]]; then
install_from_binary
elif [[ "$local_repo" == "true" ]]; then
install_from_local_repo
else
install_from_release
fi
if [[ "$heidi_dang" == "true" ]]; then
setup_heidi_plugin
fi
ensure_path
if [[ -n "${GITHUB_ACTIONS-}" && "${GITHUB_ACTIONS}" == "true" ]]; then
echo "$INSTALL_DIR" >> "$GITHUB_PATH"
fi
echo
echo -e "${MUTED}Installed opencode to:${NC} ${INSTALL_DIR}/opencode"
if [[ "$local_repo" == "true" ]]; then
echo -e "${MUTED}Opencode source:${NC} ${repo_path}"
else
echo -e "${MUTED}Release source:${NC} ${release_repo}"
fi
if [[ "$heidi_dang" == "true" ]]; then
echo -e "${MUTED}oh-my-opencode plugin:${NC} ${PLUGIN_PATH}"
echo -e "${MUTED}oh-my-opencode branch:${NC} ${ohmy_branch}"
fi
echo
echo "Run: opencode"
echo
}
main "$@"