Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

install.sh

140 lines5.6 KB

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)
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}"
23bin_dir="${T3CODE_INSTALL_BIN_DIR:-$HOME/.local/bin}"
24​
25fail() {
26 printf 't3 install: %s\n' "$1" >&2
27 exit 1
29​
30# Exit 44 on a 404 so callers can tell "no such asset" from a network failure.
31fetch() {
32 if command -v curl >/dev/null 2>&1; then
33 status="$(curl -sSL -w '%{http_code}' "$1" -o "$2")" || return 1
34 case "$status" in
35 2??) return 0 ;;
36 404) return 44 ;;
37 *) printf 'GET %s returned HTTP %s\n' "$1" "$status" >&2; return 1 ;;
38 esac
39 elif command -v wget >/dev/null 2>&1; then
40 wget -q --server-response "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0
41 if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; fi
42 cat "$2.headers" >&2; rm -f "$2.headers"; return 1
43 else
44 fail "curl or wget is required"
45 fi
47​
48case "$(uname -s)" in
49 Darwin) platform="darwin" ;;
50 Linux) platform="linux" ;;
51 *) fail "unsupported operating system $(uname -s); use the desktop app or npm" ;;
52esac
53case "$(uname -m)" in
54 arm64 | aarch64) arch="arm64" ;;
55 x86_64 | amd64) arch="x64" ;;
56 *) fail "unsupported architecture $(uname -m)" ;;
57esac
58command -v tar >/dev/null 2>&1 || fail "tar is required"
59if command -v sha256sum >/dev/null 2>&1; then
60 checksum() { sha256sum "$1" | cut -d' ' -f1; }
61elif command -v shasum >/dev/null 2>&1; then
62 checksum() { shasum -a 256 "$1" | cut -d' ' -f1; }
63else
64 fail "sha256sum or shasum is required"
65fi
66​
67channel="${T3CODE_CHANNEL:-stable}"
68version="${T3CODE_VERSION:-}"
69if [ -z "$version" ]; then
70 # Tags are v<semver>; the channel is the prerelease identifier, or none for
71 # stable. Only tags of the requested train are considered, so a stable
72 # install can never pick up a nightly or preview build by accident.
73 case "$channel" in
74 stable) tag_pattern='v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)' ;;
75 nightly | preview) tag_pattern="v\([0-9][^\"]*-${channel}\.[0-9]*\.[0-9]*\)" ;;
76 *) fail "T3CODE_CHANNEL must be stable, nightly, or preview" ;;
77 esac
78 tmp_index="$(mktemp)"
79 fetch "https://api.github.com/repos/${repo}/releases?per_page=100" "$tmp_index"
80 version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
81 rm -f "$tmp_index"
82 [ -n "$version" ] || fail "could not find a ${channel} release; set T3CODE_VERSION"
83fi
84case "$version" in
85 *-preview.*)
86 printf '%s\n' \
87 "t3 ${version} is a preview build." \
88 " Preview builds are cut by maintainers from unreleased branches to exercise the release" \
89 " pipeline. They can be broken, receive no fixes, and are never offered as updates." \
90 " Set T3CODE_CHANNEL=stable (the default) for a supported build." >&2
91 if [ "$channel" != "preview" ] && [ -z "${T3CODE_VERSION:-}" ]; then
92 fail "refusing a preview build that was not explicitly requested"
93 fi
94 ;;
95esac
96​
97stem="t3-${version}-${platform}-${arch}"
98archive="${stem}.tar.gz"
99versions_dir="${t3_home}/runtime/versions"
100target_dir="${versions_dir}/${version}"
101​
102if [ -f "${target_dir}/.install-complete" ] && [ "$(cat "${target_dir}/.install-complete")" = "$version" ]; then
103 printf 't3 %s is already installed at %s\n' "$version" "$target_dir"
104else
105 mkdir -p "$versions_dir"
106 staging="$(mktemp -d "${versions_dir}/.staging-XXXXXX")"
107 trap 'rm -rf "$staging"' EXIT
108​
109 printf 'Downloading %s...\n' "$archive"
110 fetch_status=0
111 fetch "${base_url}/v${version}/SHA256SUMS" "${staging}/SHA256SUMS" || fetch_status=$?
112 if [ "$fetch_status" -eq 44 ]; then
113 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}\`"
114 elif [ "$fetch_status" -ne 0 ]; then
115 fail "could not download the release checksums"
116 fi
117 fetch "${base_url}/v${version}/${archive}" "${staging}/${archive}"
118​
119 expected="$(grep " \*\{0,1\}${archive}\$" "${staging}/SHA256SUMS" | cut -d' ' -f1)"
120 [ -n "$expected" ] || fail "${archive} is not listed in SHA256SUMS"
121 actual="$(checksum "${staging}/${archive}")"
122 [ "$actual" = "$expected" ] || fail "checksum mismatch for ${archive}"
123​
124 tar -xzf "${staging}/${archive}" -C "$staging" --strip-components=1
125 rm -f "${staging}/${archive}" "${staging}/SHA256SUMS"
126 "${staging}/t3" --version >/dev/null || fail "the downloaded executable does not run"
127 printf '%s\n' "$version" > "${staging}/.install-complete"
128​
129 rm -rf "$target_dir"
130 mv "$staging" "$target_dir"
131 trap - EXIT
132fi
133​
134mkdir -p "$bin_dir"
135ln -sfn "${target_dir}/t3" "${bin_dir}/t3"
136printf 'Installed t3 %s\n %s -> %s\n' "$version" "${bin_dir}/t3" "${target_dir}/t3"
137case ":${PATH}:" in
138 *":${bin_dir}:"*) ;;
139 *) printf 'Add %s to your PATH to run `t3`.\n' "$bin_dir" ;;
140esac