Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·417 lines (402 loc) · 18.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·417 lines (402 loc) · 18.1 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
#!/bin/sh
# Installs the T3 Code CLI from a GitHub Release archive. Needs only sh, tar,
# sha256sum or shasum, and curl or wget; no Node, npm, or compiler.
#
# curl -fsSL https://raw.githubusercontent.com/ScriptedAlchemy/t3code/main/scripts/install.sh | sh
#
# Environment:
# T3CODE_CHANNEL release train to follow: stable, nightly, or preview
# (default: nightly; the fork publishes nightly builds)
# T3CODE_VERSION exact version to install (overrides T3CODE_CHANNEL)
# T3CODE_HOME T3 home directory (default: ~/.t3)
# T3CODE_INSTALL_BIN_DIR where the `t3` command goes (default: ~/.local/bin)
# T3CODE_RELEASE_BASE_URL mirror for releases/download (default: GitHub)
#
# The archive is unpacked into $T3CODE_HOME/runtime/versions/<version>, the
# same layout `t3 service install` uses, so the service reuses this download
# instead of fetching the release again.
set -eu
repo="ScriptedAlchemy/t3code"
base_url="${T3CODE_RELEASE_BASE_URL:-https://github.com/${repo}/releases/download}"
base_url="${base_url%%#*}"
base_path="${base_url%%\?*}"
base_query="${base_url#"$base_path"}"
while [ "${base_path%/}" != "$base_path" ]; do base_path="${base_path%/}"; done
t3_home="${T3CODE_HOME:-$HOME/.t3}"
case "$t3_home" in
/*) ;;
*)
# Preserve cwd's trailing newlines; trim only pwd's newline and the sentinel.
t3_cwd="$(pwd -P && printf .)"
t3_home="${t3_cwd%??}/$t3_home"
;;
esac
bin_dir="${T3CODE_INSTALL_BIN_DIR:-$HOME/.local/bin}"
# Trailing slashes would put `//` in the paths the t3 command guard compares.
while [ "${t3_home%/}" != "$t3_home" ] && [ "$t3_home" != / ]; do t3_home="${t3_home%/}"; done
while [ "${bin_dir%/}" != "$bin_dir" ] && [ "$bin_dir" != / ]; do bin_dir="${bin_dir%/}"; done
# Absolute, so the PATH hint never adds a folder that changes with the working directory.
case "$bin_dir" in /*) ;; *) bin_dir="$PWD/$bin_dir" ;; esac
fail() {
printf '\nt3 install: %s\n' "$1" >&2
exit 1
}
# ANSI stays on stderr, so `curl ... | sh` still gets progress.
interactive=false
if [ -t 2 ] && [ "${TERM:-}" != dumb ]; then interactive=true; fi
reset= bold= muted= accent= green=
if "$interactive" && [ -z "${NO_COLOR:-}" ]; then
reset="$(printf '\033[0m')"; bold="$(printf '\033[1m')"
muted="$(printf '\033[2m')"; accent="$(printf '\033[94m')"; green="$(printf '\033[32m')"
fi
step() {
if "$interactive"; then printf '\r\033[2K %s%s%s' "$muted" "$1" "$reset" >&2
else printf ' %s\n' "$1" >&2; fi
}
if "$interactive"; then
printf '\n%s' "$bold" >&2
printf ' %s\n' '██████████ ████████ ' >&2
printf ' %s\n' ' ███ ▄██▀ T3 Code' >&2
printf ' %s%s %sCLI installer%s\n' ' ███ ████▄ ' "$reset" "$muted" "$reset$bold" >&2
printf ' %s\n' ' ███ ▄ ███' >&2
printf ' %s\n' ' ███ ███████▀ ' >&2
printf '%s\n' "$reset" >&2
fi
step "Finding your release..."
# Sleeps 1, 2, then 4 s before retrying a 408, 429, 5xx, or network error. Not curl
# --retry: older curl waits out any Retry-After, however long.
backoff() {
case "$1" in 408 | 429) ;; 4??) return 1 ;; esac
[ "$2" -lt 4 ] && sleep "$((1 << ($2 - 1)))"
}
# Exit 44 on a 404 so callers can tell "no such asset" from a network failure.
display_url() {
display_path="${1%%\?*}"
display_path="${display_path%%#*}"
case "$display_path" in
*://*@*)
display_rest="${display_path#*://}"
display_authority="${display_rest%%/*}"
printf '%s://%s%s' "${display_path%%://*}" "${display_authority##*@}" "${display_rest#"$display_authority"}"
;;
*) printf '%s' "$display_path" ;;
esac
}
fetch() {
if command -v curl >/dev/null 2>&1; then
attempt=1
while :; do
code=0; status="$(curl -sSL -H "Accept: ${3:-*/*}" -w '%{http_code}' "$1" -o "$2" 2>/dev/null)" || code=$?
case "$code:$status" in 0:2??) break ;; esac
backoff "$status" "$attempt" || break
attempt=$((attempt + 1))
done
[ "$code" -eq 0 ] || { printf 'GET %s failed to connect\n' "$(display_url "$1")" >&2; return 1; }
case "$status" in
2??) return 0 ;;
404) return 44 ;;
*) printf 'GET %s returned HTTP %s\n' "$(display_url "$1")" "$status" >&2; return 1 ;;
esac
elif command -v wget >/dev/null 2>&1; then
wget -q --server-response --header="Accept: ${3:-*/*}" "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0
if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; fi
printf 'GET %s failed\n' "$(display_url "$1")" >&2; rm -f "$2.headers"; return 1
else
fail "curl or wget is required"
fi
}
mb() {
tenths=$((($1 * 10 + 524288) / 1048576))
printf '%s.%s' "$((tenths / 10))" "$((tenths % 10))"
}
download_start() {
if command -v curl >/dev/null 2>&1; then
curl -fsSL -w '%{http_code}' -D "$2.headers" "$1" -o "$2" >"$2.status" 2>"$2.errors" &
else
wget -q --server-response "$1" -O "$2" 2>"$2.headers" &
fi
download_pid=$!
}
# Reaps the finished download, then restarts it if curl hit a transient error.
download_retry() {
result=0; wait "$download_pid" || result=$?
download_pid=
[ "$result" -ne 0 ] && [ -f "$2.status" ] && backoff "$(cat "$2.status")" "$attempt" || return 1
attempt=$((attempt + 1))
download_start "$1" "$2"
}
# Poll the file written by the downloader; no progress-output parsing or extra request.
download() {
if ! "$interactive"; then fetch "$1" "$2"; return; fi
attempt=1
download_start "$1" "$2"
previous=-1
cr="$(printf '\r')"
while kill -0 "$download_pid" 2>/dev/null || download_retry "$1" "$2"; do
bytes=0; total=0
if [ -f "$2" ]; then bytes="$(wc -c < "$2")"; fi
if [ -f "$2.headers" ]; then
while read -r key value; do
case "$key" in
HTTP/*) total=0 ;;
[Cc]ontent-[Ll]ength:) total="${value%"$cr"}" ;;
esac
done < "$2.headers"
fi
case "$total" in ''|*[!0-9]*) total=0 ;; esac
if [ "$bytes" -ne "$previous" ]; then
if [ "$total" -gt 0 ]; then
percent=$((bytes * 100 / total)); [ "$percent" -le 100 ] || percent=100
filled=$((percent * 32 / 100)); bar=; rest=; n=0
while [ "$n" -lt 32 ]; do
if [ "$n" -lt "$filled" ]; then bar="${bar}■"; else rest="${rest}·"; fi
n=$((n + 1))
done
printf '\r\033[2K %s%s%s%s%s %3d%% %s%s / %s MB%s' "$accent" "$bar" "$reset$muted" "$rest" "$reset" "$percent" "$muted" "$(mb "$bytes")" "$(mb "$total")" "$reset" >&2
else
printf '\r\033[2K %sDownloading%s %s MB' "$muted" "$reset" "$(mb "$bytes")" >&2
fi
previous="$bytes"
fi
sleep 0.1
done
if [ "$result" -ne 0 ]; then
printf '\n' >&2
printf 'GET %s failed (downloader exit %s)\n' "$(display_url "$1")" "$result" >&2
return "$result"
fi
size="$(mb "$(wc -c < "$2")")"
printf '\r\033[2K %s■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■%s 100%% %s%s / %s MB%s\n' "$accent" "$reset" "$muted" "$size" "$size" "$reset" >&2
rm -f "$2.headers" "$2.errors" "$2.status"
}
case "$(uname -s)" in
Darwin) platform="darwin" ;;
Linux) platform="linux" ;;
*) fail "unsupported operating system $(uname -s); use a desktop release from https://github.com/${repo}/releases" ;;
esac
case "$(uname -m)" in
arm64 | aarch64) arch="arm64" ;;
x86_64 | amd64) arch="x64" ;;
*) fail "unsupported architecture $(uname -m)" ;;
esac
if [ "${platform}-${arch}" = darwin-x64 ]; then
fail "there is no t3 CLI for Intel Macs. Install the desktop app from https://github.com/${repo}/releases, or run a server from source: https://github.com/${repo}/blob/main/docs/user/install.md#intel-macs"
fi
command -v tar >/dev/null 2>&1 || fail "tar is required"
if command -v sha256sum >/dev/null 2>&1; then
checksum() { sha256sum < "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; }
else
fail "sha256sum or shasum is required"
fi
channel="${T3CODE_CHANNEL:-nightly}"
version="${T3CODE_VERSION:-}"
if [ -z "$version" ]; then
case "$channel" in
stable) tag_pattern='v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)' ;;
nightly | preview) tag_pattern="v\([0-9][^\"]*-${channel}\.[0-9]*\.[0-9]*\)" ;;
*) fail "T3CODE_CHANNEL must be stable, nightly, or preview" ;;
esac
tmp_index="$(mktemp)"
if [ "$channel" = stable ]; then
# release.yml marks only plain stable releases latest. This is a github.com
# page, so it is not cut off by a page of newer nightlies or the API quota.
fetch "https://github.com/${repo}/releases/latest" "$tmp_index" application/json
version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
else
page=1
while [ -z "$version" ] && [ "$page" -le 10 ]; do
fetch "https://api.github.com/repos/${repo}/releases?per_page=100&page=${page}" "$tmp_index"
version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
grep -q '"tag_name"' "$tmp_index" || break
page=$((page + 1))
done
fi
rm -f "$tmp_index"
[ -n "$version" ] || fail "could not find a ${channel} release; set T3CODE_VERSION"
fi
case "$version" in
*-preview.*)
printf '%s\n' \
"t3 ${version} is a preview build." \
" Preview builds are cut by maintainers from unreleased branches to exercise the release" \
" pipeline. They can be broken, receive no fixes, and are never offered as updates." \
" Set T3CODE_CHANNEL=nightly (the default) for the fork's published builds." >&2
if [ "$channel" != "preview" ] && [ -z "${T3CODE_VERSION:-}" ]; then
fail "refusing a preview build that was not explicitly requested"
fi
;;
esac
stem="t3-${version}-${platform}-${arch}"
archive="${stem}.tar.gz"
versions_dir="${t3_home}/runtime/versions"
target_dir="${versions_dir}/${version}"
if [ -f "${target_dir}/.install-complete" ] && [ "$(cat "${target_dir}/.install-complete")" = "$version" ]; then
step "Version ${version} is already downloaded."
else
mkdir -p "$versions_dir"
staging="$(mktemp -d "${versions_dir}/.staging-XXXXXX")"
download_pid=
trap '[ -z "$download_pid" ] || { kill "$download_pid" 2>/dev/null || true; wait "$download_pid" 2>/dev/null || true; }; rm -rf "$staging"' EXIT
trap 'printf "\n" >&2; exit 130' INT
trap 'printf "\n" >&2; exit 143' TERM
if "$interactive"; then printf '\r\033[2K' >&2; fi
printf ' %sInstalling%s T3 Code %s%s%s\n\n' "$muted" "$reset" "$bold" "$version" "$reset" >&2
step "Downloading..."
fetch_status=0
fetch "${base_path}/v${version}/SHA256SUMS${base_query}" "${staging}/SHA256SUMS" || fetch_status=$?
if [ "$fetch_status" -eq 44 ]; then
fail "t3 ${version} has no release archive for ${platform}-${arch}; choose a published fork build from https://github.com/${repo}/releases"
elif [ "$fetch_status" -ne 0 ]; then
fail "could not download the release checksums"
fi
expected="$(grep " \*\{0,1\}${archive}\$" "${staging}/SHA256SUMS" | cut -d' ' -f1)"
[ -n "$expected" ] || fail "t3 ${version} has no release archive for ${platform}-${arch}"
download "${base_path}/v${version}/${archive}${base_query}" "${staging}/${archive}" || fail "could not download ${archive}"
step "Verifying the download..."
actual="$(checksum "${staging}/${archive}")"
[ "$actual" = "$expected" ] || fail "checksum mismatch for ${archive}"
step "Extracting T3 Code..."
tar -xzf "${staging}/${archive}" -C "$staging" --strip-components=1
rm -f "${staging}/${archive}" "${staging}/SHA256SUMS"
"${staging}/t3" --version >/dev/null || fail "the downloaded executable does not run"
printf '%s\n' "$version" > "${staging}/.install-complete"
rm -rf "$target_dir"
mv "$staging" "$target_dir"
# When a concurrent install published first, mv moved this copy inside it; keep only theirs.
rm -rf "${target_dir}/${staging##*/}"
trap - EXIT
fi
step "Setting up the t3 command..."
mkdir -p "$bin_dir"
# Exec by absolute path: through a symlink, argv0 is the link, and the Cursor SDK looks for its
# helpers beside it. t3 update and t3 install write the same script.
nl='
'
launcher_head="#!/bin/sh${nl}exec '"
launcher_tail="' \"\$@\"${nl}"
# The x keeps a trailing newline in the path through command substitution.
launcher_script() {
quoted="$(printf '%sx' "$1" | sed "s/'/'\\\\''/g")"
printf '%s%s%s' "$launcher_head" "${quoted%x}" "$launcher_tail"
}
# The executable an unedited launcher script at $1 runs; a user's own wrapper prints nothing.
script_t3() {
[ -f "$1" ] && [ ! -L "$1" ] && [ "$(wc -c < "$1")" -le 4096 ] || return 0
contents="$(cat "$1"; printf x)"
contents="${contents%x}"
case "$contents" in "$launcher_head"*"$launcher_tail") ;; *) return 0 ;; esac
quoted="${contents#"$launcher_head"}"
unquoted="$(printf '%sx' "${quoted%"$launcher_tail"}" | sed "s/'\\\\''/'/g")"
unquoted="${unquoted%x}"
[ -n "$unquoted" ] && [ "$(launcher_script "$unquoted"; printf x)" = "${contents}x" ] && printf '%s' "$unquoted"
return 0
}
# Replace only this home's own t3 launchers: into its versions, or the desktop app's launcher.
# Anything else (a user's script, a copied binary) belongs to someone else.
# Exactly <versions>/<version>/t3 ($1 under $2), never another program inside a version.
version_t3() {
case "$1" in "$2"/*) ;; *) return 1 ;; esac
case "${1#"$2"/}" in */*/*) return 1 ;; */t3) return 0 ;; *) return 1 ;; esac
}
slot_empty=true
if [ -e "${bin_dir}/t3" ] || [ -L "${bin_dir}/t3" ]; then
slot_empty=false
owned=false
real_versions_dir="$(CDPATH='' cd -P -- "$versions_dir" && pwd)"
if [ -L "${bin_dir}/t3" ]; then
existing="$(readlink "${bin_dir}/t3" 2>/dev/null || true)"
case "$existing" in /*) ;; *) existing="${bin_dir}/${existing}" ;; esac
else
existing="$(script_t3 "${bin_dir}/t3")"
fi
if [ -n "$existing" ]; then
# Through a symlinked T3 home the path names the alias; resolve its folder to compare.
existing_dir="$(CDPATH='' cd -P -- "$(dirname -- "$existing")" 2>/dev/null && pwd || true)"
# The text alone counts only without . or .. segments (a pruned version leaves a dangling path).
case "$existing" in
*/./* | */../* | */. | */..) ;;
*)
if version_t3 "$existing" "$versions_dir" || version_t3 "$existing" "$real_versions_dir" ||
[ "$existing" = "${t3_home}/bin/t3" ]; then owned=true; fi
;;
esac
if [ -n "$existing_dir" ] && version_t3 "${existing_dir}/${existing##*/}" "$real_versions_dir"; then
owned=true
fi
fi
"$owned" || fail "another t3 is already at ${bin_dir}/t3. Remove it and run this again, or run ${target_dir}/t3 directly."
fi
launcher_tmp="$(mktemp "${bin_dir}/.t3.XXXXXX")"
trap 'rm -f "$launcher_tmp"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
launcher_script "${target_dir}/t3" > "$launcher_tmp"
chmod 755 "$launcher_tmp"
# An empty spot is hard-linked without -f, so a file that appears after the check above is kept.
if "$slot_empty"; then
ln "$launcher_tmp" "${bin_dir}/t3" || fail "could not write ${bin_dir}/t3; something else was created there."
else
# Known race, accepted: a file written here after the ownership check above is overwritten.
# mv replaces a legacy symlink rather than writing through it.
mv -f "$launcher_tmp" "${bin_dir}/t3"
fi
rm -f "$launcher_tmp"
trap - EXIT
# Pruning keeps what this launcher points at; a background service cannot see bin_dir otherwise.
launcher="$(CDPATH='' cd -P -- "$bin_dir" && pwd)/t3"
launchers="${t3_home}/runtime/launchers"
grep -qxF "$launcher" "$launchers" 2>/dev/null || printf '%s\n' "$launcher" >> "$launchers"
if "$interactive"; then printf '\r\033[2K' >&2; fi
printf ' %sInstalled T3 Code %s%s\n\n' "$green" "$version" "$reset" >&2
# The folder a shell reads its startup file from: $1 when set and safe to paste, else $2 when
# unset. A set but unsafe folder prints nothing, since the default would never be read.
read_from() {
case "$1" in
"") printf '%s' "$2" ;;
/*) case "$1" in *[!A-Za-z0-9_./-]*) ;; *) printf '%s' "$1" ;; esac ;;
esac
}
# A command to paste for zsh, Linux bash, or fish, like `t3 install` prints. macOS bash reads only
# one of several startup files, so no single file is safe to change there.
path_command=
path_rest="$bin_dir"
path_prefix=
case "$bin_dir" in "$HOME"/*) path_rest="${bin_dir#"$HOME"}" path_prefix='$HOME' ;; esac
case "$path_rest" in *[!A-Za-z0-9_./-]*) path_rest= ;; esac
if [ -n "$path_rest" ]; then
# Last on PATH, so it never shadows another t3.
path_line="export PATH=\"\$PATH:${path_prefix}${path_rest}\""
shell_name="${SHELL:-}"
case "${shell_name##*/}" in
zsh)
# zsh reads .zshrc from ZDOTDIR when it is set; ~/.zshrc would never be read then.
zsh_dir="$(read_from "${ZDOTDIR:-}" '~')"
[ -z "$zsh_dir" ] || path_command="printf '\\n%s\\n' '${path_line}' >> ${zsh_dir}/.zshrc" ;;
bash) [ "$platform" = darwin ] || path_command="printf '\\n%s\\n' '${path_line}' >> ~/.bashrc" ;;
fish)
# --path changes $PATH itself; fish_user_paths would go ahead of the system folders.
config_dir="$(read_from "${XDG_CONFIG_HOME:-}" '~/.config')"
fish_dir="${config_dir}/fish"
[ -z "$config_dir" ] || path_command="mkdir -p ${fish_dir} && printf '\\n%s\\n' 'fish_add_path --append --path ${path_prefix}${path_rest}' >> ${fish_dir}/config.fish" ;;
esac
fi
# A t3 earlier on PATH runs instead; the PATH command adds this folder last.
# A PATH entry with a trailing slash gives `dir//t3`.
first_t3="$(command -v t3 2>/dev/null | tr -s / || true)"
case "$first_t3" in "" | "${bin_dir}/t3") first_t3= ;; esac
if [ -n "$first_t3" ]; then
printf ' Another t3 at %s runs first, so run %s%s/t3%s to use this one.\n\n' "$first_t3" "$bold" "$bin_dir" "$reset"
else
case ":${PATH}:" in
*":${bin_dir}:"* | *":${bin_dir}/:"*) printf ' Run %st3%s to use T3 Code here, or %st3 up%s to use it from your other devices.\n\n' "$bold" "$reset" "$bold" "$reset" ;;
*)
if [ -n "$path_command" ]; then
printf ' %s is not on your PATH yet. Add it with:\n\n %s\n\n Then open a new terminal and run %st3%s.\n\n' "$bin_dir" "$path_command" "$bold" "$reset"
else
printf ' Add %s to your PATH, then run %st3%s.\n\n' "$bin_dir" "$bold" "$reset"
fi
;;
esac
fi