-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
executable file
·192 lines (167 loc) · 5.02 KB
/
Copy pathinstall
File metadata and controls
executable file
·192 lines (167 loc) · 5.02 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
#!/usr/bin/env bash
set -euo pipefail
APP=opencode-xk
REPO=${OPENCODE_XK_REPO:-FengYunCalm/opencode-xk}
MUTED='\033[0;2m'
RED='\033[0;31m'
NC='\033[0m'
usage() {
cat <<EOF
opencode-xk installer
Usage: install [options]
Options:
-h, --help Display this help message
-v, --version <version> Install a specific XK version, for example XK-2.0.0
-b, --binary <path> Install from a local binary instead of downloading
--repo <owner/repo> Download from a different GitHub repository
--no-modify-path Do not modify shell config files
Examples:
./install --version XK-2.0.0
./install --binary packages/opencode/dist/opencode-linux-x64/bin/opencode
OPENCODE_XK_REPO=owner/repo ./install --version XK-2.0.0
EOF
}
requested_version=${VERSION:-}
binary_path=""
no_modify_path=false
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
requested_version="${2:-}"
if [[ -z "$requested_version" ]]; then
echo -e "${RED}Error: --version requires a version argument${NC}"
exit 1
fi
shift 2
;;
-b|--binary)
binary_path="${2:-}"
if [[ -z "$binary_path" ]]; then
echo -e "${RED}Error: --binary requires a path argument${NC}"
exit 1
fi
shift 2
;;
--repo)
REPO="${2:-}"
if [[ -z "$REPO" ]]; then
echo -e "${RED}Error: --repo requires owner/repo${NC}"
exit 1
fi
shift 2
;;
--no-modify-path)
no_modify_path=true
shift
;;
*)
echo -e "${RED}Error: unknown option '$1'${NC}"
exit 1
;;
esac
done
INSTALL_DIR="$HOME/.opencode/bin"
mkdir -p "$INSTALL_DIR"
has_avx2() {
if [[ "${OPENCODE_XK_BASELINE:-}" == "1" ]]; then
return 1
fi
if [[ -r /proc/cpuinfo ]]; then
grep -qi 'avx2' /proc/cpuinfo && return 0
fi
if command -v sysctl >/dev/null 2>&1; then
sysctl -a 2>/dev/null | grep -qi 'avx2' && return 0
fi
return 1
}
is_musl() {
[[ "${OPENCODE_XK_MUSL:-}" == "1" ]] && return 0
ldd --version 2>&1 | grep -qi 'musl'
}
detect_target() {
local raw_os arch target suffix abi
raw_os=$(uname -s)
arch=$(uname -m)
case "$raw_os" in
Darwin*) target="darwin" ;;
Linux*) target="linux" ;;
MINGW*|MSYS*|CYGWIN*) target="windows" ;;
*) echo -e "${RED}Unsupported OS: $raw_os${NC}"; exit 1 ;;
esac
case "$arch" in
aarch64|arm64) arch="arm64" ;;
x86_64|amd64) arch="x64" ;;
*) echo -e "${RED}Unsupported architecture: $arch${NC}"; exit 1 ;;
esac
suffix=""
abi=""
if [[ "$arch" == "x64" ]] && ! has_avx2; then
suffix="-baseline"
fi
if [[ "$target" == "linux" ]] && is_musl; then
abi="-musl"
fi
echo "opencode-${target}-${arch}${suffix}${abi}"
}
latest_version() {
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p'
}
install_binary() {
local src="$1"
cp "$src" "$INSTALL_DIR/$APP"
chmod 755 "$INSTALL_DIR/$APP"
}
if [[ -n "$binary_path" ]]; then
if [[ ! -f "$binary_path" ]]; then
echo -e "${RED}Error: binary not found at $binary_path${NC}"
exit 1
fi
echo -e "${MUTED}Installing ${NC}$APP ${MUTED}from local binary${NC}"
install_binary "$binary_path"
else
version="$requested_version"
if [[ -z "$version" ]]; then
version=$(latest_version)
fi
if [[ -z "$version" || "$version" != XK-* ]]; then
echo -e "${RED}Error: expected an XK-* release version, got '$version'${NC}"
exit 1
fi
asset=$(detect_target)
if [[ "$asset" == opencode-windows-* ]]; then
asset="$asset.exe"
fi
tmp="${TMPDIR:-/tmp}/opencode_xk_install_$$"
mkdir -p "$tmp"
trap 'rm -rf "$tmp"' EXIT
url="https://github.com/${REPO}/releases/download/${version}/${asset}"
echo -e "${MUTED}Downloading ${NC}${url}"
curl -fL -o "$tmp/$asset" "$url"
install_binary "$tmp/$asset"
fi
add_to_path() {
local file=$1
local line="export PATH=$INSTALL_DIR:\$PATH"
if grep -Fxq "$line" "$file"; then
return
fi
if [[ -w "$file" ]]; then
printf '\n# opencode-xk\n%s\n' "$line" >> "$file"
echo -e "${MUTED}Added ${NC}$INSTALL_DIR ${MUTED}to ${NC}$file"
fi
}
if [[ "$no_modify_path" != "true" && ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
case "$(basename "${SHELL:-sh}")" in
zsh) [[ -f "$HOME/.zshrc" ]] && add_to_path "$HOME/.zshrc" ;;
bash) [[ -f "$HOME/.bashrc" ]] && add_to_path "$HOME/.bashrc" ;;
fish) echo "Add $INSTALL_DIR to PATH in fish config if needed." ;;
*) echo "Add $INSTALL_DIR to PATH if needed." ;;
esac
fi
echo ""
"$INSTALL_DIR/$APP" --version
echo "Installed $APP to $INSTALL_DIR/$APP"