Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·318 lines (270 loc) · 10.8 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·318 lines (270 loc) · 10.8 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
#!/usr/bin/env sh
# CellScript installer — curl -fsSL <URL> | sh
set -eu
if (set -o pipefail) 2>/dev/null; then
set -o pipefail
fi
REPO="CellScript-Labs/CellScript"
BINARY="cellc"
INSTALL_DIR="${CELLSCRIPT_HOME:-$HOME/.cellscript}/bin"
# ---------------------------------------------------------------------------
# Mirror sources — try in order until one succeeds
#
# Set CELLSCRIPT_MIRROR to override, e.g.:
# CELLSCRIPT_MIRROR=ghgo — use ghgo.xyz GitHub proxy (China-friendly)
# CELLSCRIPT_MIRROR=ghproxy — use gh-proxy.com (China-friendly)
# CELLSCRIPT_MIRROR=ghfast — use ghfast.top (China-friendly)
# CELLSCRIPT_MIRROR=direct — force github.com only, no mirrors
#
# Mirrors may supply the release archive, never its checksum: SHA256SUMS is
# fetched from github.com only (or pinned via CELLSCRIPT_INSTALL_SHA256) and
# verification is mandatory.
# ---------------------------------------------------------------------------
# github.com release download rewrite for each mirror
# Each function accepts (REPO, VERSION, FILENAME) and prints the full URL.
_github_url() { printf 'https://github.com/%s/releases/download/v%s/%s' "$1" "$2" "$3"; }
_ghgo_url() { printf 'https://ghgo.xyz/https://github.com/%s/releases/download/v%s/%s' "$1" "$2" "$3"; }
_ghproxy_url() { printf 'https://gh-proxy.com/https://github.com/%s/releases/download/v%s/%s' "$1" "$2" "$3"; }
_ghfast_url() { printf 'https://ghfast.top/https://github.com/%s/releases/download/v%s/%s' "$1" "$2" "$3"; }
MIRROR_FUNCS="_github_url _ghgo_url _ghproxy_url _ghfast_url"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
info() { printf '[cellscript] %s\n' "$1"; }
warn() { printf '[cellscript] WARNING: %s\n' "$1" >&2; }
die() { printf '[cellscript] ERROR: %s\n' "$1" >&2; exit 1; }
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
die "required command not found: $1"
fi
}
# Try curl with multiple URL sources, return on first success.
curl_any() {
# Usage: curl_any <arg1> <arg2> ... -- <url1> <url2> ...
_curl_args=""
_urls=""
_after_separator=0
for _arg in "$@"; do
if [ "$_arg" = "--" ]; then
_after_separator=1
continue
fi
if [ "$_after_separator" -eq 0 ]; then
_curl_args="$_curl_args $_arg"
else
_urls="$_urls $_arg"
fi
done
for _url in $_urls; do
if curl $_curl_args "$_url"; then
return 0
fi
warn "source unreachable: $_url"
done
return 1
}
# ---------------------------------------------------------------------------
# Platform detection
# ---------------------------------------------------------------------------
detect_platform() {
_OS="$(uname -s)"
_ARCH="$(uname -m)"
case "$_OS" in
Darwin) _PLATFORM="apple-darwin" ;;
Linux) _PLATFORM="unknown-linux-musl" ;;
MINGW*|MSYS*|CYGWIN*) _PLATFORM="pc-windows-msvc"; _EXT=".exe" ;;
*) die "unsupported operating system: $_OS" ;;
esac
case "$_ARCH" in
x86_64|amd64) _ARCH="x86_64" ;;
aarch64|arm64) _ARCH="aarch64" ;;
*) die "unsupported architecture: $_ARCH" ;;
esac
TARGET="${_ARCH}-${_PLATFORM}"
}
# ---------------------------------------------------------------------------
# Version resolution
# ---------------------------------------------------------------------------
get_latest_version() {
# Accept explicit version from environment
if [ -n "${CELLSCRIPT_VERSION:-}" ]; then
VERSION="$CELLSCRIPT_VERSION"
info "using requested version: $VERSION"
return 0
fi
need_cmd curl
# Try multiple version sources — api.github.com is often blocked in China
# Source 1: GitHub API (global)
_TAG="$(curl -fsSL --max-time 10 "https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null \
| grep '"tag_name"' | head -1 | sed 's/.*"v\(.*\)".*/\1/')"
if [ -n "$_TAG" ]; then
VERSION="$_TAG"
info "latest version: $VERSION"
return 0
fi
# Source 2: GitHub API via ghgo proxy (China-friendly)
_TAG="$(curl -fsSL --max-time 10 "https://ghgo.xyz/https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null \
| grep '"tag_name"' | head -1 | sed 's/.*"v\(.*\)".*/\1/')"
if [ -n "$_TAG" ]; then
VERSION="$_TAG"
info "latest version: $VERSION (via ghgo)"
return 0
fi
# Source 3: GitHub API via ghfast proxy (China-friendly)
_TAG="$(curl -fsSL --max-time 10 "https://ghfast.top/https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null \
| grep '"tag_name"' | head -1 | sed 's/.*"v\(.*\)".*/\1/')"
if [ -n "$_TAG" ]; then
VERSION="$_TAG"
info "latest version: $VERSION (via ghfast)"
return 0
fi
# Source 4: GitHub tags API fallback
_TAG="$(curl -fsSL --max-time 10 "https://api.github.com/repos/${REPO}/tags" 2>/dev/null \
| grep '"name"' | head -1 | sed 's/.*"v\(.*\)".*/\1/')"
if [ -n "$_TAG" ]; then
VERSION="$_TAG"
info "latest version: $VERSION"
return 0
fi
die "unable to determine the latest CellScript version. Set CELLSCRIPT_VERSION manually."
}
# ---------------------------------------------------------------------------
# Download and install
# ---------------------------------------------------------------------------
# Resolve the list of mirror functions to try for binary downloads.
_resolve_mirrors() {
case "${CELLSCRIPT_MIRROR:-auto}" in
direct) MIRRORS="_github_url" ;;
ghgo) MIRRORS="_ghgo_url _github_url" ;;
ghproxy) MIRRORS="_ghproxy_url _github_url" ;;
ghfast) MIRRORS="_ghfast_url _github_url" ;;
auto|"") MIRRORS="$MIRROR_FUNCS" ;;
*) die "unknown CELLSCRIPT_MIRROR value: $CELLSCRIPT_MIRROR (use: auto, direct, ghgo, ghproxy, ghfast)" ;;
esac
}
download_and_install() {
_ARCHIVE="cellscript-${VERSION}-${TARGET}.tar.gz"
_TMPDIR="$(mktemp -d)"
_CLEANUP=1
trap '_cleanup' EXIT
# Build URL list from mirrors
_resolve_mirrors
_URLS=""
for _mf in $MIRRORS; do
_URLS="$_URLS $($_mf "$REPO" "$VERSION" "$_ARCHIVE")"
done
info "downloading ${_ARCHIVE} ..."
curl_any -fsSL --max-time 120 -o "$_TMPDIR/$_ARCHIVE" -- $_URLS \
|| die "download failed from all mirrors"
# Mandatory SHA256 verification. The archive may come from any mirror,
# but its checksum must come from the release origin (github.com) or from
# the caller: a mirror that supplied the bytes must never also supply the
# checksum. Verification fails closed — set CELLSCRIPT_INSTALL_SHA256 to
# the digest published in the release notes when github.com is
# unreachable.
_EXPECTED_SHA="${CELLSCRIPT_INSTALL_SHA256:-}"
if [ -z "$_EXPECTED_SHA" ]; then
_SHA_URL="$(_github_url "$REPO" "$VERSION" "SHA256SUMS")"
if ! curl -fsSL --max-time 15 -o "$_TMPDIR/SHA256SUMS" "$_SHA_URL"; then
die "cannot fetch SHA256SUMS from github.com; set CELLSCRIPT_INSTALL_SHA256 to the published digest and retry"
fi
_EXPECTED_SHA="$(grep "[[:space:]]${_ARCHIVE}\$" "$_TMPDIR/SHA256SUMS" | head -1 | awk '{print $1}' || true)"
if [ -z "$_EXPECTED_SHA" ]; then
die "SHA256SUMS from github.com has no entry for ${_ARCHIVE}"
fi
fi
if command -v sha256sum >/dev/null 2>&1; then
_ACTUAL_SHA="$(sha256sum "$_TMPDIR/$_ARCHIVE" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
_ACTUAL_SHA="$(shasum -a 256 "$_TMPDIR/$_ARCHIVE" | awk '{print $1}')"
else
die "neither sha256sum nor shasum is available; refusing to install an unverified binary"
fi
if [ "$_ACTUAL_SHA" != "$_EXPECTED_SHA" ]; then
die "SHA256 checksum mismatch (expected ${_EXPECTED_SHA}, got ${_ACTUAL_SHA})"
fi
info "SHA256 checksum verified"
info "installing to ${INSTALL_DIR} ..."
mkdir -p "$INSTALL_DIR"
tar xzf "$_TMPDIR/$_ARCHIVE" -C "$_TMPDIR" "${BINARY}${_EXT:-}"
mv "$_TMPDIR/${BINARY}${_EXT:-}" "$INSTALL_DIR/${BINARY}${_EXT:-}"
chmod +x "$INSTALL_DIR/${BINARY}${_EXT:-}"
}
_cleanup() {
if [ -n "${_TMPDIR:-}" ] && [ -d "$_TMPDIR" ] && [ "$_CLEANUP" -eq 1 ]; then
rm -rf "$_TMPDIR"
fi
}
# ---------------------------------------------------------------------------
# PATH configuration
# ---------------------------------------------------------------------------
ensure_path() {
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) info "PATH already contains ${INSTALL_DIR}"; return 0 ;;
esac
_RC_FILE=""
case "${SHELL:-}" in
*/fish) _RC_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" ;;
*/zsh) _RC_FILE="$HOME/.zshrc" ;;
*/bash) _RC_FILE="$HOME/.bashrc" ;;
*/sh) _RC_FILE="$HOME/.profile" ;;
esac
if [ -z "$_RC_FILE" ]; then
warn "could not detect shell config file. Add ${INSTALL_DIR} to your PATH manually."
return 0
fi
if ! grep -q "$INSTALL_DIR" "$_RC_FILE" 2>/dev/null; then
mkdir -p "$(dirname "$_RC_FILE")"
case "${SHELL:-}" in
*/fish)
printf 'set -gx PATH %s $PATH\n' "$INSTALL_DIR" >> "$_RC_FILE"
;;
*)
printf 'export PATH="%s:$PATH"\n' "$INSTALL_DIR" >> "$_RC_FILE"
;;
esac
info "added ${INSTALL_DIR} to PATH in ${_RC_FILE}"
fi
export PATH="${INSTALL_DIR}:${PATH}"
}
# ---------------------------------------------------------------------------
# Dry-run support
# ---------------------------------------------------------------------------
dry_run() {
info "=== DRY RUN ==="
info "platform: ${TARGET}"
info "version: ${VERSION}"
info "binary: ${BINARY}"
info "install: ${INSTALL_DIR}/${BINARY}${_EXT:-}"
info "mirrors: ${CELLSCRIPT_MIRROR:-auto}"
_resolve_mirrors
for _mf in $MIRRORS; do
info "source: $($_mf "$REPO" "$VERSION" "cellscript-${VERSION}-${TARGET}.tar.gz")"
done
info "=============== No files were modified."
exit 0
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
main() {
need_cmd curl
need_cmd tar
need_cmd uname
detect_platform
get_latest_version
if [ "${CELLSCRIPT_DRY_RUN:-0}" = "1" ]; then
dry_run
fi
download_and_install
ensure_path
# Verify installation
if command -v "$BINARY" >/dev/null 2>&1; then
_VER="$("$BINARY" --version 2>/dev/null || true)"
info "installation complete! ${_VER}"
else
info "installation complete!"
info "restart your shell or run: export PATH=\"${INSTALL_DIR}:\$PATH\""
fi
}
main "$@"