Open History to see the last commit
1#!/bin/sh
2# Installs the T3 Code CLI from a GitHub Release archive. Needs only sh, tar,
3# sha256sum or shasum, and curl or wget; no Node, npm, or compiler.
4#
5# curl -fsSL https://t3.codes/install.sh | sh
6#
7# Environment:
8# T3CODE_CHANNEL release train to follow: stable, nightly, or preview
9# (default: stable; preview is a maintainers' test train)
10# T3CODE_VERSION exact version to install (overrides T3CODE_CHANNEL)
11# T3CODE_HOME T3 home directory (default: ~/.t3)
12# T3CODE_INSTALL_BIN_DIR where the `t3` symlink goes (default: ~/.local/bin)
13# T3CODE_RELEASE_BASE_URL mirror for releases/download (default: GitHub)
14#
15# The archive is unpacked into $T3CODE_HOME/runtime/versions/<version>, the
16# same layout `t3 service install` uses, so the service reuses this download
17# instead of fetching the release again.
18set -eu
19
20repo="pingdotgg/t3code"
21base_url="${T3CODE_RELEASE_BASE_URL:-https://github.com/${repo}/releases/download}"
22t3_home="${T3CODE_HOME:-$HOME/.t3}"
23case "$t3_home" in
24 /*) ;;
25 *)
26 # Preserve cwd's trailing newlines; trim only pwd's newline and the sentinel.
27 t3_cwd="$(pwd -P && printf .)"
28 t3_home="${t3_cwd%??}/$t3_home"
29 ;;
30esac
31bin_dir="${T3CODE_INSTALL_BIN_DIR:-$HOME/.local/bin}"
32
33fail() {
34 printf '\nt3 install: %s\n' "$1" >&2
35 exit 1
36}
37
38# ANSI stays on stderr, so `curl ... | sh` still gets progress.
39interactive=false
40if [ -t 2 ] && [ "${TERM:-}" != dumb ]; then interactive=true; fi
41reset= bold= muted= accent= green=
42if "$interactive" && [ -z "${NO_COLOR:-}" ]; then
43 reset="$(printf '\033[0m')"; bold="$(printf '\033[1m')"
44 muted="$(printf '\033[2m')"; accent="$(printf '\033[94m')"; green="$(printf '\033[32m')"
45fi
46step() {
47 if "$interactive"; then printf '\r\033[2K %s%s%s' "$muted" "$1" "$reset" >&2
48 else printf ' %s\n' "$1" >&2; fi
49}
50if "$interactive"; then
51 printf '\n%s' "$bold" >&2
52 printf ' %s\n' '██████████ ████████ ' >&2
53 printf ' %s\n' ' ███ ▄██▀ T3 Code' >&2
54 printf ' %s%s %sCLI installer%s\n' ' ███ ████▄ ' "$reset" "$muted" "$reset$bold" >&2
55 printf ' %s\n' ' ███ ▄ ███' >&2
56 printf ' %s\n' ' ███ ███████▀ ' >&2
57 printf '%s\n' "$reset" >&2
58fi
59step "Finding your release..."
60
61# Exit 44 on a 404 so callers can tell "no such asset" from a network failure.
62fetch() {
63 if command -v curl >/dev/null 2>&1; then
64 status="$(curl -sSL -H "Accept: ${3:-*/*}" -w '%{http_code}' "$1" -o "$2")" || return 1
65 case "$status" in
66 2??) return 0 ;;
67 404) return 44 ;;
68 *) printf 'GET %s returned HTTP %s\n' "$1" "$status" >&2; return 1 ;;
69 esac
70 elif command -v wget >/dev/null 2>&1; then
71 wget -q --server-response --header="Accept: ${3:-*/*}" "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0
72 if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; fi
73 cat "$2.headers" >&2; rm -f "$2.headers"; return 1
74 else
75 fail "curl or wget is required"
76 fi
77}
78
79mb() {
80 tenths=$((($1 * 10 + 524288) / 1048576))
81 printf '%s.%s' "$((tenths / 10))" "$((tenths % 10))"
82}
83# Poll the file written by the downloader; no progress-output parsing or extra request.
84download() {
85 if ! "$interactive"; then fetch "$1" "$2"; return; fi
86 if command -v curl >/dev/null 2>&1; then
87 curl -fsSL -D "$2.headers" "$1" -o "$2" 2>"$2.errors" &
88 else
89 wget -q --server-response "$1" -O "$2" 2>"$2.headers" &
90 fi
91 download_pid=$!
92 previous=-1
93 cr="$(printf '\r')"
94 while kill -0 "$download_pid" 2>/dev/null; do
95 bytes=0; total=0
96 if [ -f "$2" ]; then bytes="$(wc -c < "$2")"; fi
97 if [ -f "$2.headers" ]; then
98 while read -r key value; do
99 case "$key" in
100 HTTP/*) total=0 ;;
101 [Cc]ontent-[Ll]ength:) total="${value%"$cr"}" ;;
102 esac
103 done < "$2.headers"
104 fi
105 case "$total" in ''|*[!0-9]*) total=0 ;; esac
106 if [ "$bytes" -ne "$previous" ]; then
107 if [ "$total" -gt 0 ]; then
108 percent=$((bytes * 100 / total)); [ "$percent" -le 100 ] || percent=100
109 filled=$((percent * 32 / 100)); bar=; rest=; n=0
110 while [ "$n" -lt 32 ]; do
111 if [ "$n" -lt "$filled" ]; then bar="${bar}■"; else rest="${rest}·"; fi
112 n=$((n + 1))
113 done
114 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
115 else
116 printf '\r\033[2K %sDownloading%s %s MB' "$muted" "$reset" "$(mb "$bytes")" >&2
117 fi
118 previous="$bytes"
119 fi
120 sleep 0.1
121 done
122 result=0; wait "$download_pid" || result=$?
123 download_pid=
124 if [ "$result" -ne 0 ]; then
125 printf '\n' >&2
126 if [ -f "$2.errors" ]; then cat "$2.errors" >&2; else cat "$2.headers" >&2; fi
127 return "$result"
128 fi
129 size="$(mb "$(wc -c < "$2")")"
130 printf '\r\033[2K %s■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■%s 100%% %s%s / %s MB%s\n' "$accent" "$reset" "$muted" "$size" "$size" "$reset" >&2
131 rm -f "$2.headers" "$2.errors"
132}
133
134case "$(uname -s)" in
135 Darwin) platform="darwin" ;;
136 Linux) platform="linux" ;;
137 *) fail "unsupported operating system $(uname -s); use the desktop app or npm" ;;
138esac
139case "$(uname -m)" in
140 arm64 | aarch64) arch="arm64" ;;
141 x86_64 | amd64) arch="x64" ;;
142 *) fail "unsupported architecture $(uname -m)" ;;
143esac
144if [ "${platform}-${arch}" = darwin-x64 ]; then
145 fail "there is no t3 CLI for Intel Macs. Install the desktop app from https://t3.codes/download, or run a server from source: https://github.com/${repo}/blob/main/docs/user/install.md#intel-macs"
146fi
147command -v tar >/dev/null 2>&1 || fail "tar is required"
148if command -v sha256sum >/dev/null 2>&1; then
149 checksum() { sha256sum < "$1" | cut -d' ' -f1; }
150elif command -v shasum >/dev/null 2>&1; then
151 checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; }
152else
153 fail "sha256sum or shasum is required"
154fi
155
156channel="${T3CODE_CHANNEL:-stable}"
157version="${T3CODE_VERSION:-}"
158if [ -z "$version" ]; then
159 case "$channel" in
160 stable) tag_pattern='v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)' ;;
161 nightly | preview) tag_pattern="v\([0-9][^\"]*-${channel}\.[0-9]*\.[0-9]*\)" ;;
162 *) fail "T3CODE_CHANNEL must be stable, nightly, or preview" ;;
163 esac
164 tmp_index="$(mktemp)"
165 if [ "$channel" = stable ]; then
166 # release.yml marks only plain stable releases latest. This is a github.com
167 # page, so it is not cut off by a page of newer nightlies or the API quota.
168 fetch "https://github.com/${repo}/releases/latest" "$tmp_index" application/json
169 version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
170 else
171 page=1
172 while [ -z "$version" ] && [ "$page" -le 10 ]; do
173 fetch "https://api.github.com/repos/${repo}/releases?per_page=100&page=${page}" "$tmp_index"
174 version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
175 grep -q '"tag_name"' "$tmp_index" || break
176 page=$((page + 1))
177 done
178 fi
179 rm -f "$tmp_index"
180 [ -n "$version" ] || fail "could not find a ${channel} release; set T3CODE_VERSION"
181fi
182case "$version" in
183 *-preview.*)
184 printf '%s\n' \
185 "t3 ${version} is a preview build." \
186 " Preview builds are cut by maintainers from unreleased branches to exercise the release" \
187 " pipeline. They can be broken, receive no fixes, and are never offered as updates." \
188 " Set T3CODE_CHANNEL=stable (the default) for a supported build." >&2
189 if [ "$channel" != "preview" ] && [ -z "${T3CODE_VERSION:-}" ]; then
190 fail "refusing a preview build that was not explicitly requested"
191 fi
192 ;;
193esac
194
195stem="t3-${version}-${platform}-${arch}"
196archive="${stem}.tar.gz"
197versions_dir="${t3_home}/runtime/versions"
198target_dir="${versions_dir}/${version}"
199
200if [ -f "${target_dir}/.install-complete" ] && [ "$(cat "${target_dir}/.install-complete")" = "$version" ]; then
201 step "Version ${version} is already downloaded."
202else
203 mkdir -p "$versions_dir"
204 staging="$(mktemp -d "${versions_dir}/.staging-XXXXXX")"
205 download_pid=
206 trap '[ -z "$download_pid" ] || { kill "$download_pid" 2>/dev/null || true; wait "$download_pid" 2>/dev/null || true; }; rm -rf "$staging"' EXIT
207 trap 'printf "\n" >&2; exit 130' INT
208 trap 'printf "\n" >&2; exit 143' TERM
209
210 if "$interactive"; then printf '\r\033[2K' >&2; fi
211 printf ' %sInstalling%s T3 Code %s%s%s\n\n' "$muted" "$reset" "$bold" "$version" "$reset" >&2
212 step "Downloading..."
213 fetch_status=0
214 fetch "${base_url}/v${version}/SHA256SUMS" "${staging}/SHA256SUMS" || fetch_status=$?
215 if [ "$fetch_status" -eq 44 ]; then
216 fail "t3 ${version} has no release archive for ${platform}-${arch}; releases before the self-contained CLI can only be installed with \`npm install -g t3@${version}\`"
217 elif [ "$fetch_status" -ne 0 ]; then
218 fail "could not download the release checksums"
219 fi
220 expected="$(grep " \*\{0,1\}${archive}\$" "${staging}/SHA256SUMS" | cut -d' ' -f1)"
221 [ -n "$expected" ] || fail "t3 ${version} has no release archive for ${platform}-${arch}"
222 download "${base_url}/v${version}/${archive}" "${staging}/${archive}"
223
224 step "Verifying the download..."
225 actual="$(checksum "${staging}/${archive}")"
226 [ "$actual" = "$expected" ] || fail "checksum mismatch for ${archive}"
227
228 step "Extracting T3 Code..."
229 tar -xzf "${staging}/${archive}" -C "$staging" --strip-components=1
230 rm -f "${staging}/${archive}" "${staging}/SHA256SUMS"
231 "${staging}/t3" --version >/dev/null || fail "the downloaded executable does not run"
232 printf '%s\n' "$version" > "${staging}/.install-complete"
233
234 rm -rf "$target_dir"
235 mv "$staging" "$target_dir"
236 trap - EXIT
237fi
238
239step "Setting up the t3 command..."
240mkdir -p "$bin_dir"
241ln -sfn "${target_dir}/t3" "${bin_dir}/t3"
242# Pruning keeps what this launcher points at; a background service cannot see bin_dir otherwise.
243launcher="$(CDPATH='' cd -P -- "$bin_dir" && pwd)/t3"
244launchers="${t3_home}/runtime/launchers"
245grep -qxF "$launcher" "$launchers" 2>/dev/null || printf '%s\n' "$launcher" >> "$launchers"
246if "$interactive"; then printf '\r\033[2K' >&2; fi
247printf ' %sInstalled T3 Code %s%s\n\n' "$green" "$version" "$reset" >&2
248case ":${PATH}:" in
249 *":${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" ;;
250 *) printf ' Add %s to your PATH, then run %st3%s.\n\n' "$bin_dir" "$bold" "$reset" ;;
251esac