-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·216 lines (183 loc) · 5.09 KB
/
install.sh
File metadata and controls
executable file
·216 lines (183 loc) · 5.09 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
#!/bin/sh
set -eu
AIMA_REPO="${AIMA_REPO:-Approaching-AI/AIMA}"
AIMA_VERSION="${AIMA_VERSION:-}"
AIMA_INSTALL_DIR="${AIMA_INSTALL_DIR:-}"
say() {
printf '%s\n' "$*"
}
fail() {
printf 'error: %s\n' "$*" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "required command not found: $1"
}
curl_get() {
url="$1"
if [ -n "${GITHUB_TOKEN:-}" ]; then
curl -fsSL \
--retry 3 \
--retry-delay 2 \
--retry-all-errors \
--connect-timeout 15 \
--max-time 300 \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"$url"
return
fi
curl -fsSL \
--retry 3 \
--retry-delay 2 \
--retry-all-errors \
--connect-timeout 15 \
--max-time 300 \
"$url"
}
resolve_platform_asset() {
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$os" in
linux) os="linux" ;;
darwin) os="darwin" ;;
*)
fail "unsupported OS: $os (supported: linux, darwin)"
;;
esac
case "$arch" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*)
fail "unsupported architecture: $arch (supported: amd64, arm64)"
;;
esac
case "$os/$arch" in
linux/amd64) printf 'aima-linux-amd64\n' ;;
linux/arm64) printf 'aima-linux-arm64\n' ;;
darwin/arm64) printf 'aima-darwin-arm64\n' ;;
*)
fail "unsupported platform: $os/$arch"
;;
esac
}
resolve_latest_product_tag() {
tags_json="$(curl_get "https://api.github.com/repos/${AIMA_REPO}/tags?per_page=100")"
tag="$(
printf '%s\n' "$tags_json" \
| sed -n 's/.*"name"[[:space:]]*:[[:space:]]*"\(v[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' \
| awk -F. '
{
tag = $0
gsub(/^v/, "", $0)
printf "%09d %09d %09d %s\n", $1, $2, $3, tag
}
' \
| sort \
| tail -n 1 \
| awk '{print $4}'
)"
[ -n "$tag" ] || fail "no product tag found in ${AIMA_REPO}"
printf '%s\n' "$tag"
}
resolve_latest_installable_release() {
releases_json="$(curl_get "https://api.github.com/repos/${AIMA_REPO}/releases?per_page=100")"
tag="$(
printf '%s\n' "$releases_json" \
| sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\(v[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' \
| awk -F. '
{
tag = $0
gsub(/^v/, "", $0)
printf "%09d %09d %09d %s\n", $1, $2, $3, tag
}
' \
| sort \
| tail -n 1 \
| awk '{print $4}'
)"
[ -n "$tag" ] || fail "no installable product release found in ${AIMA_REPO}"
printf '%s\n' "$tag"
}
resolve_install_dir() {
if [ -n "$AIMA_INSTALL_DIR" ]; then
printf '%s\n' "$AIMA_INSTALL_DIR"
return
fi
if [ -w /usr/local/bin ]; then
printf '/usr/local/bin\n'
return
fi
[ -n "${HOME:-}" ] || fail "HOME is not set and /usr/local/bin is not writable; set AIMA_INSTALL_DIR"
printf '%s\n' "$HOME/.local/bin"
}
sha256_file() {
file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
return
fi
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{print $1}'
return
fi
fail "sha256sum or shasum is required for checksum verification"
}
install_binary() {
src="$1"
dst="$2"
if command -v install >/dev/null 2>&1; then
install -m 0755 "$src" "$dst"
return
fi
cp "$src" "$dst"
chmod 0755 "$dst"
}
need_cmd curl
need_cmd uname
need_cmd sed
need_cmd awk
need_cmd sort
need_cmd mktemp
asset="$(resolve_platform_asset)"
latest_tag="$(resolve_latest_product_tag)"
version="${AIMA_VERSION:-$(resolve_latest_installable_release)}"
install_dir="$(resolve_install_dir)"
tmpdir="$(mktemp -d)"
cleanup() {
rm -rf "$tmpdir"
}
trap cleanup EXIT INT TERM
asset_url="https://github.com/${AIMA_REPO}/releases/download/${version}/${asset}"
checksums_url="https://github.com/${AIMA_REPO}/releases/download/${version}/checksums.txt"
asset_path="${tmpdir}/${asset}"
checksums_path="${tmpdir}/checksums.txt"
say "AIMA repo: ${AIMA_REPO}"
say "AIMA version: ${version}"
say "AIMA asset: ${asset}"
if [ -z "$AIMA_VERSION" ] && [ "$version" != "$latest_tag" ]; then
say "Warning: latest product tag is ${latest_tag}, but latest installable binary release is ${version}."
fi
if ! curl_get "$asset_url" >"$asset_path"; then
fail "release asset not found: ${asset_url}. Publish ${asset} for tag ${version} first, or set AIMA_VERSION/AIMA_REPO."
fi
if ! curl_get "$checksums_url" >"$checksums_path"; then
fail "checksums.txt not found for ${version}. Publish release checksums before using the installer."
fi
expected="$(
awk -v asset="$asset" '
$2 == asset { print $1; exit }
' "$checksums_path"
)"
[ -n "$expected" ] || fail "checksum entry for ${asset} not found in checksums.txt"
actual="$(sha256_file "$asset_path")"
[ "$actual" = "$expected" ] || fail "checksum mismatch for ${asset}: expected ${expected}, got ${actual}"
mkdir -p "$install_dir"
install_binary "$asset_path" "${install_dir}/aima"
say "Installed to ${install_dir}/aima"
case ":${PATH:-}:" in
*":${install_dir}:"*) ;;
*)
say "Add ${install_dir} to PATH if needed."
;;
esac
say "Run: aima version"