-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·358 lines (293 loc) · 8.45 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·358 lines (293 loc) · 8.45 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
#!/usr/bin/env bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OPENCODE_BIN_DIR="$SCRIPT_DIR/.opencode"
VERSION_FILE="$OPENCODE_BIN_DIR/version"
GITHUB_REPO="GutMutCode/opencode"
GITHUB_API="https://api.github.com/repos/$GITHUB_REPO/releases/latest"
GITHUB_RELEASE="https://github.com/$GITHUB_REPO/releases/latest/download"
QUIET=false
INLINE=false
FORCE=false
CODE_STATUS="skip"
RUNTIME_STATUS="skip"
CODE_UPDATED=false
RUNTIME_UPDATED=false
print_inline() {
[[ "$INLINE" == true ]] && echo -e "${CYAN}agentation >${NC} ${1}"
}
print_info() {
if [[ "$INLINE" == true ]]; then
print_inline "$1"
elif [[ "$QUIET" == false ]]; then
echo -e "${CYAN}[update]${NC} ${1}"
fi
}
print_success() {
if [[ "$INLINE" == true ]]; then
print_inline "$1"
elif [[ "$QUIET" == false ]]; then
echo -e "${GREEN}[update]${NC} ${1}"
fi
}
print_warning() {
if [[ "$INLINE" == true ]]; then
print_inline "$1"
elif [[ "$QUIET" == false ]]; then
echo -e "${YELLOW}[update]${NC} ${1}"
fi
}
print_error() {
if [[ "$INLINE" == true ]]; then
echo -e "${RED}agentation >${NC} ${1}" >&2
else
echo -e "${RED}[update]${NC} ${1}" >&2
fi
}
usage() {
cat <<EOF
Usage: ./update.sh [OPTIONS]
Options:
--quiet, -q Suppress all output
--inline, -i Show progress in "agentation > message" format
--force, -f Force update even if up-to-date
-h, --help Show this help message
Examples:
./update.sh # Check and update if needed
./update.sh --quiet # Silent mode
./update.sh --inline # Progress mode for wrapper script
./update.sh --force # Force re-download
EOF
exit 0
}
while [[ $# -gt 0 ]]; do
case $1 in
--quiet|-q)
QUIET=true
shift
;;
--inline|-i)
INLINE=true
shift
;;
--force|-f)
FORCE=true
shift
;;
-h|--help)
usage
;;
*)
shift
;;
esac
done
detect_platform() {
local os arch
case "$(uname -s)" in
Darwin) os="darwin" ;;
Linux) os="linux" ;;
MINGW*|MSYS*|CYGWIN*) os="windows" ;;
*) echo "unknown"; return ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="x64" ;;
arm64|aarch64) arch="arm64" ;;
*) echo "unknown"; return ;;
esac
echo "${os}-${arch}"
}
get_current_version() {
if [[ -f "$VERSION_FILE" ]]; then
cat "$VERSION_FILE"
else
echo "unknown"
fi
}
get_latest_version() {
local response version
if command -v curl &> /dev/null; then
response=$(curl -fsSL "$GITHUB_API" 2>/dev/null)
elif command -v wget &> /dev/null; then
response=$(wget -qO- "$GITHUB_API" 2>/dev/null)
fi
version=$(echo "$response" | grep -o '"tag_name"[^,]*' | head -1 | cut -d'"' -f4)
echo "${version:-unknown}"
}
update_code() {
cd "$SCRIPT_DIR"
if [[ ! -d ".git" ]]; then
CODE_STATUS="skip"
return 0
fi
git fetch origin main --quiet 2>/dev/null || {
CODE_STATUS="skip"
return 0
}
local LOCAL_HEAD REMOTE_HEAD
LOCAL_HEAD=$(git rev-parse HEAD 2>/dev/null)
REMOTE_HEAD=$(git rev-parse origin/main 2>/dev/null)
if [[ "$LOCAL_HEAD" == "$REMOTE_HEAD" && "$FORCE" == false ]]; then
CODE_STATUS="ok"
return 0
fi
print_info "Updating code..."
if [[ -n $(git status --porcelain 2>/dev/null) ]]; then
git stash push -m "auto-stash before update" --quiet
local STASHED=true
fi
git pull origin main --quiet || {
print_error "Code update failed"
[[ "${STASHED:-false}" == true ]] && git stash pop --quiet
CODE_STATUS="fail"
return 1
}
if [[ -f "package.json" ]]; then
local PKG_MANAGER
if command -v pnpm &> /dev/null; then
PKG_MANAGER="pnpm"
else
PKG_MANAGER="npm"
fi
$PKG_MANAGER install >/dev/null 2>&1
$PKG_MANAGER run build >/dev/null 2>&1 || {
print_error "Build failed"
[[ "${STASHED:-false}" == true ]] && git stash pop --quiet
CODE_STATUS="fail"
return 1
}
fi
[[ "${STASHED:-false}" == true ]] && git stash pop --quiet
CODE_STATUS="ok"
CODE_UPDATED=true
return 0
}
update_runtime() {
local platform="$1"
if [[ "$platform" == "unknown" ]]; then
RUNTIME_STATUS="skip"
return 0
fi
if [[ "$platform" == "darwin-x64" ]]; then
RUNTIME_STATUS="skip"
return 0
fi
local current_version latest_version
current_version=$(get_current_version)
latest_version=$(get_latest_version)
if [[ "$latest_version" == "unknown" ]]; then
RUNTIME_STATUS="skip"
return 0
fi
if [[ "$current_version" == "$latest_version" && "$FORCE" == false ]]; then
RUNTIME_STATUS="ok"
return 0
fi
print_info "Updating runtime: $current_version → $latest_version"
local archive_name extract_cmd
case "$platform" in
darwin-*|windows-*)
archive_name="opencode-${platform}.zip"
extract_cmd="unzip -o -q"
;;
*)
archive_name="opencode-${platform}.tar.gz"
extract_cmd="tar -xzf"
;;
esac
local download_url="${GITHUB_RELEASE}/${archive_name}"
local archive_path="${OPENCODE_BIN_DIR}/${archive_name}"
mkdir -p "$OPENCODE_BIN_DIR"
if command -v curl &> /dev/null; then
curl -fsSL "$download_url" -o "$archive_path" || {
print_error "Download failed"
RUNTIME_STATUS="fail"
return 1
}
elif command -v wget &> /dev/null; then
wget -q "$download_url" -O "$archive_path" || {
print_error "Download failed"
RUNTIME_STATUS="fail"
return 1
}
else
print_error "Neither curl nor wget found"
RUNTIME_STATUS="fail"
return 1
fi
cd "$OPENCODE_BIN_DIR"
$extract_cmd "$archive_name"
rm -f "$archive_name"
chmod +x "$OPENCODE_BIN_DIR/opencode" 2>/dev/null || true
echo "$latest_version" > "$VERSION_FILE"
RUNTIME_STATUS="ok"
RUNTIME_UPDATED=true
return 0
}
update_wrapper() {
local platform="$1"
local bin_dir="$HOME/.local/bin"
local wrapper="$bin_dir/agentation"
local config_file="${XDG_CONFIG_HOME:-$HOME/.config}/opencode/agentation.json"
local source_bin="$OPENCODE_BIN_DIR/opencode"
local update_script="$SCRIPT_DIR/update.sh"
[[ ! -f "$wrapper" ]] && return 0
local expected_wrapper
expected_wrapper=$(cat <<EOF
#!/bin/bash
if [[ -f "$update_script" ]]; then
"$update_script" --inline || true
fi
export OPENCODE_CONFIG="$config_file"
exec "$source_bin" "\$@"
EOF
)
local current_wrapper
current_wrapper=$(cat "$wrapper" 2>/dev/null || echo "")
if [[ "$current_wrapper" != "$expected_wrapper" ]]; then
echo "$expected_wrapper" > "$wrapper"
chmod +x "$wrapper"
fi
}
print_summary() {
if [[ "$INLINE" != true && "$QUIET" == true ]]; then
return 0
fi
local code_icon runtime_icon
case "$CODE_STATUS" in
ok) code_icon="✓" ;;
skip) code_icon="-" ;;
fail) code_icon="✗" ;;
esac
case "$RUNTIME_STATUS" in
ok) runtime_icon="✓" ;;
skip) runtime_icon="-" ;;
fail) runtime_icon="✗" ;;
esac
if [[ "$CODE_UPDATED" == true || "$RUNTIME_UPDATED" == true ]]; then
print_success "Updated (code: $code_icon, runtime: $runtime_icon)"
elif [[ "$CODE_STATUS" == "fail" || "$RUNTIME_STATUS" == "fail" ]]; then
print_error "Update failed (code: $code_icon, runtime: $runtime_icon)"
else
print_info "Up-to-date (code: $code_icon, runtime: $runtime_icon)"
fi
}
main() {
local platform
platform=$(detect_platform)
print_info "Checking for updates..."
update_code || true
update_runtime "$platform" || true
update_wrapper "$platform"
print_summary
if [[ "$CODE_STATUS" == "fail" || "$RUNTIME_STATUS" == "fail" ]]; then
return 1
fi
return 0
}
main "$@"