-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·256 lines (223 loc) · 7.17 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·256 lines (223 loc) · 7.17 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
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# install.sh — fetch the latest promptzero release and drop it on $PATH.
#
# This script intentionally does one thing: bootstrap a fresh install.
# Once promptzero is on your system, use its own subcommands to stay
# current:
#
# promptzero version --check — see if a newer release is available
# promptzero upgrade — replace yourself with the latest
#
# Usage:
# sh install.sh Install the latest release.
# sh install.sh --version vX.Y.Z Pin a specific release.
# sh install.sh --prefix <dir> Install directory (see below).
# sh install.sh --help This text.
#
# Environment overrides:
# PROMPTZERO_VERSION Same effect as --version.
# PROMPTZERO_PREFIX Same effect as --prefix.
#
# Install directory resolution (first writable wins):
# 1. --prefix / PROMPTZERO_PREFIX
# 2. $XDG_BIN_HOME
# 3. $HOME/.local/bin
# 4. /usr/local/bin
#
# Supports Linux and macOS on amd64/arm64. Windows users should download
# the .zip from https://github.com/xunholy/promptzero/releases.
#
# Requires: sh, curl, tar, sha256sum (Linux) or shasum (macOS).
set -eu
REPO="xunholy/promptzero"
BINARY="promptzero"
CLEANUP_TMP=""
cleanup() {
if [ -n "${CLEANUP_TMP:-}" ] && [ -d "${CLEANUP_TMP}" ]; then
rm -rf "${CLEANUP_TMP}"
fi
}
trap cleanup EXIT INT TERM
log() { printf '%s\n' "$*" >&2; }
info() { printf '\033[36m▸\033[0m %s\n' "$*" >&2; }
ok() { printf '\033[32m✓\033[0m %s\n' "$*" >&2; }
warn() { printf '\033[33m!\033[0m %s\n' "$*" >&2; }
die() { printf '\033[31m✗\033[0m %s\n' "$*" >&2; exit 1; }
usage() {
# Print the doc banner (contiguous leading `#` lines after the SPDX
# marker). Extracting from $0 keeps the help text and the source in
# sync without a second heredoc to maintain.
awk 'NR==1 {next}
/^# SPDX/ {next}
/^#!/ {next}
/^#/ {sub(/^# ?/,""); print; next}
{exit}' "$0"
}
# --- Argument parsing ---------------------------------------------------
VERSION="${PROMPTZERO_VERSION:-}"
PREFIX="${PROMPTZERO_PREFIX:-}"
while [ $# -gt 0 ]; do
case "$1" in
--version)
shift
[ $# -gt 0 ] || die "--version needs a value"
VERSION="$1"
;;
--version=*) VERSION="${1#--version=}" ;;
--prefix)
shift
[ $# -gt 0 ] || die "--prefix needs a value"
PREFIX="$1"
;;
--prefix=*) PREFIX="${1#--prefix=}" ;;
-h|--help) usage; exit 0 ;;
upgrade|uninstall)
die "'$1' is no longer handled by install.sh — use 'promptzero $1' instead"
;;
*) usage >&2; die "unknown argument: $1" ;;
esac
shift
done
# --- Dependencies & platform detection ---------------------------------
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}
sha256_of() {
# Portable SHA-256: GNU coreutils ships sha256sum, macOS ships shasum.
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
else
die "need sha256sum or shasum for checksum verification"
fi
}
detect_platform() {
os="$(uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m 2>/dev/null)"
case "$os" in
linux) OS=linux ;;
darwin) OS=darwin ;;
mingw*|msys*|cygwin*)
die "Windows is not supported by install.sh — download the .zip from https://github.com/${REPO}/releases"
;;
*) die "unsupported OS: $os" ;;
esac
case "$arch" in
x86_64|amd64) ARCH=amd64 ;;
aarch64|arm64) ARCH=arm64 ;;
*) die "unsupported architecture: $arch (expected amd64 or arm64)" ;;
esac
}
# --- Install-dir resolution --------------------------------------------
writable_dir() {
d="$1"
[ -z "$d" ] && return 1
if [ -d "$d" ] && [ -w "$d" ]; then return 0; fi
parent="$(dirname "$d")"
[ -d "$parent" ] && [ -w "$parent" ]
}
resolve_prefix() {
if [ -n "$PREFIX" ]; then
printf '%s' "$PREFIX"
return
fi
for d in "${XDG_BIN_HOME:-}" "$HOME/.local/bin" "/usr/local/bin"; do
if writable_dir "$d"; then
printf '%s' "$d"
return
fi
done
die "no writable install dir found — try: sh install.sh --prefix ~/bin"
}
# --- Version resolution -------------------------------------------------
normalise_version() {
v="$1"
case "$v" in
v*) printf '%s' "$v" ;;
*) printf 'v%s' "$v" ;;
esac
}
# latest_version follows the /releases/latest redirect on github.com and
# pulls the tag off the end of the final URL. Unauthenticated, no rate
# limit, no JSON parsing — the same approach the CLI uses internally.
latest_version() {
url="$(curl -fsSIL -o /dev/null -w '%{url_effective}' \
"https://github.com/${REPO}/releases/latest" 2>/dev/null)" \
|| die "failed to resolve latest version (network error)"
tag="${url##*/}"
case "$tag" in
v*) printf '%s' "$tag" ;;
*) die "couldn't resolve latest release (redirect url: $url)" ;;
esac
}
resolve_version() {
if [ -n "$VERSION" ]; then
normalise_version "$VERSION"
return
fi
latest_version
}
# --- PATH hint ----------------------------------------------------------
check_path() {
dir="$1"
case ":${PATH:-}:" in
*":${dir}:"*) return 0 ;;
esac
warn "${dir} is not on your PATH."
log ""
log " Add this to your shell profile (~/.bashrc, ~/.zshrc, ~/.profile):"
log ""
log " export PATH=\"${dir}:\$PATH\""
log ""
}
# --- Install flow -------------------------------------------------------
detect_platform
need_cmd curl
need_cmd tar
prefix="$(resolve_prefix)"
target="$(resolve_version)"
bin_path="${prefix}/${BINARY}"
info "target : ${target}"
info "host : ${OS}/${ARCH}"
info "prefix : ${prefix}"
asset="${BINARY}-${OS}-${ARCH}.tar.gz"
base="https://github.com/${REPO}/releases/download/${target}"
CLEANUP_TMP="$(mktemp -d 2>/dev/null || mktemp -d -t promptzero)"
info "downloading ${asset}"
curl -fsSL -o "${CLEANUP_TMP}/${asset}" "${base}/${asset}" \
|| die "download failed: ${base}/${asset}"
info "downloading checksums.txt"
curl -fsSL -o "${CLEANUP_TMP}/checksums.txt" "${base}/checksums.txt" \
|| die "download failed: ${base}/checksums.txt"
info "verifying sha256"
expected="$(awk -v f="$asset" '$2 == f || $2 == "*"f {print $1; exit}' \
"${CLEANUP_TMP}/checksums.txt")"
[ -n "$expected" ] || die "no checksum entry for ${asset} in checksums.txt"
got="$(sha256_of "${CLEANUP_TMP}/${asset}")"
if [ "$got" != "$expected" ]; then
die "checksum mismatch for ${asset}
expected: ${expected}
got: ${got}"
fi
info "extracting"
( cd "${CLEANUP_TMP}" && tar xzf "${asset}" )
src="${CLEANUP_TMP}/${BINARY}-${OS}-${ARCH}"
[ -f "$src" ] || die "binary '${BINARY}-${OS}-${ARCH}' not found after extract"
chmod +x "$src"
info "installing to ${bin_path}"
mkdir -p "$prefix"
mv "$src" "$bin_path"
got_ver="$("$bin_path" --version 2>/dev/null | awk '{print $2; exit}')" || true
if [ -n "$got_ver" ] && [ "$got_ver" != "$target" ]; then
warn "installed binary reports ${got_ver}, expected ${target}"
fi
ok "installed ${BINARY} ${target} → ${bin_path}"
log ""
log " Stay current with:"
log " promptzero version --check"
log " promptzero upgrade"
log ""
check_path "$prefix"