-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·307 lines (266 loc) · 9.92 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·307 lines (266 loc) · 9.92 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
#!/usr/bin/env bash
#
# install.sh — one-line installer for zvm (Zig Version Manager)
#
# Usage:
# curl -L https://github.com/lispking/zvm/releases/latest/download/install.sh | bash
# or: curl -L https://raw.githubusercontent.com/lispking/zvm/main/install.sh | bash
#
set -euo pipefail
REPO="lispking/zvm"
INSTALL_DIR="${ZVM_INSTALL:-$HOME/.local/bin}"
ZVM_DIR="${ZVM_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/zvm}"
# ── Colors ──────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
RESET='\033[0m'
info() { printf "${CYAN}>>>${RESET} %s\n" "$*"; }
ok() { printf "${GREEN} ✓${RESET} %s\n" "$*"; }
warn() { printf "${YELLOW} !${RESET} %s\n" "$*"; }
err() { printf "${RED} ✗${RESET} %s\n" "$*" >&2; }
# ── Detect platform ─────────────────────────────────────────────────────
detect_platform() {
local os arch
case "$(uname -s)" in
Darwin) os="macos" ;;
Linux) os="linux" ;;
*)
err "Unsupported OS: $(uname -s). Only macOS and Linux are supported by this script."
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="x86_64" ;;
arm64|aarch64) arch="aarch64" ;;
*)
err "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
echo "${os}-${arch}"
}
# ── Download helper ─────────────────────────────────────────────────────
download() {
local url="$1" dest="$2"
if command -v curl &>/dev/null; then
curl -fSL --progress-bar -o "$dest" "$url"
elif command -v wget &>/dev/null; then
wget -q --show-progress -O "$dest" "$url"
else
err "Neither curl nor wget found. Please install one and retry."
exit 1
fi
}
# ── Detect current shell ────────────────────────────────────────────────
detect_shell_rc() {
local shell_name=""
# Prefer $SHELL (user's login shell) over version variables,
# because the script may run under bash via shebang while the user uses zsh.
if [ -n "${SHELL:-}" ]; then
shell_name="$(basename "$SHELL")"
fi
# Fallback to the running interpreter's version
if [ -z "$shell_name" ]; then
if [ -n "${ZSH_VERSION:-}" ]; then
shell_name="zsh"
elif [ -n "${BASH_VERSION:-}" ]; then
shell_name="bash"
fi
fi
case "$shell_name" in
zsh) echo "$HOME/.zshrc" ;;
bash) echo "$HOME/.bashrc" ;;
*) echo "" ;;
esac
}
# ── Add string to file if not already present ───────────────────────────
append_once() {
local file="$1" marker="$2" content="$3"
if [ ! -f "$file" ]; then
touch "$file"
fi
if ! grep -qF "$marker" "$file" 2>/dev/null; then
printf '\n%s\n' "$content" >> "$file"
fi
}
# ── Temp directory (script-level so EXIT trap can access it in zsh) ──
TMP_DIR=""
cleanup() {
if [ -n "${TMP_DIR:-}" ] && [ -d "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi
}
trap cleanup EXIT
# ── Main ────────────────────────────────────────────────────────────────
main() {
echo ""
printf "${BOLD} zvm — Zig Version Manager${RESET}"
echo ""
echo ""
# 1. Detect platform
local platform
platform="$(detect_platform)"
local os="${platform%-*}"
local arch="${platform#*-}"
info "Detected platform: ${os} / ${arch}"
# 2. Determine download URL
local archive_name="zvm-${arch}-${os}.tar.gz"
local url="https://github.com/${REPO}/releases/latest/download/${archive_name}"
# 3. Create temp directory
TMP_DIR="$(mktemp -d)"
local archive_path="${TMP_DIR}/${archive_name}"
# 4. Download
info "Downloading ${archive_name}..."
download "$url" "$archive_path"
# 5. Extract
info "Extracting..."
tar xzf "$archive_path" -C "$TMP_DIR"
# Find the zvm binary inside the extracted directory
local zvm_binary=""
for f in "${TMP_DIR}"/zvm-*/zvm "${TMP_DIR}"/zvm/zvm "${TMP_DIR}"/zvm; do
if [ -f "$f" ] && [ -x "$f" ]; then
zvm_binary="$f"
break
fi
done
if [ -z "$zvm_binary" ]; then
# Fallback: search for any zvm binary
zvm_binary="$(find "$TMP_DIR" -name zvm -type f -perm -u+x | head -n 1)"
fi
if [ -z "$zvm_binary" ] || [ ! -f "$zvm_binary" ]; then
err "Could not find zvm binary in the archive"
exit 1
fi
# 6. Install binary
mkdir -p "$INSTALL_DIR"
cp "$zvm_binary" "${INSTALL_DIR}/zvm"
chmod +x "${INSTALL_DIR}/zvm"
ok "Installed zvm to ${INSTALL_DIR}/zvm"
# 7. Ensure paths are in shell rc
local shell_rc
shell_rc="$(detect_shell_rc)"
local needs_local_bin=true
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) needs_local_bin=false ;;
esac
local needs_zvm_bin=true
case ":${PATH}:" in
*":${ZVM_DIR}/bin:"*) needs_zvm_bin=false ;;
esac
if [ "$needs_local_bin" = false ]; then
ok "Install directory already in PATH"
fi
if [ "$needs_local_bin" = true ] || [ "$needs_zvm_bin" = true ]; then
if [ -n "$shell_rc" ]; then
echo ""
info "The following PATH entries will be added to ${shell_rc}:"
[ "$needs_local_bin" = true ] && echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
[ "$needs_zvm_bin" = true ] && echo " export PATH=\"\${ZVM_PATH:-\${XDG_DATA_HOME:-\$HOME/.local/share}/zvm}/bin:\$PATH\""
echo ""
local do_modify="n"
if [ -t 0 ] || [ -e /dev/tty ]; then
printf "${CYAN}>>>${RESET} Add to shell config? [Y/n] "
read -r answer < /dev/tty 2>/dev/null || answer="Y"
case "${answer:-Y}" in
[Yy]|[Yy][Ee][Ss]|"") do_modify="y" ;;
*) do_modify="n" ;;
esac
else
# Non-interactive (piped): auto-add and inform the user
do_modify="y"
fi
if [ "$do_modify" = "y" ]; then
if [ "$needs_local_bin" = true ]; then
append_once "$shell_rc" "# >>> zvm >>>" \
"# >>> zvm >>>
export PATH=\"\${HOME}/.local/bin:\$PATH\""
fi
if [ "$needs_zvm_bin" = true ]; then
append_once "$shell_rc" "# <<< zvm <<<" \
"export PATH=\"\${ZVM_PATH:-\${XDG_DATA_HOME:-\$HOME/.local/share}/zvm}/bin:\$PATH\"
# <<< zvm <<<"
fi
ok "Added PATH entries to ${shell_rc}"
else
warn "Skipped shell config modification."
echo ""
echo " Add these lines to your shell config manually:"
echo ""
echo ' export PATH="$HOME/.local/bin:$PATH"'
echo ' export PATH="${XDG_DATA_HOME:-$HOME/.local/share}/zvm/bin:$PATH"'
echo ""
fi
else
warn "Could not detect shell config file."
warn "Add these lines to your shell config manually:"
echo ""
echo ' export PATH="$HOME/.local/bin:$PATH"'
echo ' export PATH="${XDG_DATA_HOME:-$HOME/.local/share}/zvm/bin:$PATH"'
echo ""
fi
fi
# 8. Set up shell completion
if [ -n "$shell_rc" ]; then
local do_completion="n"
if [ -t 0 ] || [ -e /dev/tty ]; then
local comp_label=""
case "$shell_rc" in
*/.zshrc) comp_label="zsh" ;;
*/.bashrc) comp_label="bash" ;;
esac
if [ -n "$comp_label" ]; then
printf "${CYAN}>>>${RESET} Add ${comp_label} completion to ${shell_rc}? [Y/n] "
read -r answer < /dev/tty 2>/dev/null || answer="Y"
case "${answer:-Y}" in
[Yy]|[Yy][Ee][Ss]|"") do_completion="y" ;;
*) do_completion="n" ;;
esac
fi
else
do_completion="y"
fi
if [ "$do_completion" = "y" ]; then
case "$shell_rc" in
*/.zshrc)
append_once "$shell_rc" "# zvm completion" \
'eval "$(zvm completion zsh 2>/dev/null)"'
ok "Added zsh completion to ${shell_rc}"
;;
*/.bashrc)
append_once "$shell_rc" "# zvm completion" \
'eval "$(zvm completion bash 2>/dev/null)"'
ok "Added bash completion to ${shell_rc}"
;;
esac
else
warn "Skipped shell completion setup."
fi
fi
# 9. Done
echo ""
printf "${GREEN}${BOLD} zvm installed successfully!${RESET}"
echo ""
echo ""
if [ "$needs_local_bin" = true ] || [ "$needs_zvm_bin" = true ]; then
printf "${YELLOW} Restart your shell or run:${RESET}"
echo ""
echo ""
if [ -n "$shell_rc" ]; then
echo " source ${shell_rc}"
else
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
echo " export PATH=\"\${XDG_DATA_HOME:-\$HOME/.local/share}/zvm/bin:\$PATH\""
fi
echo ""
fi
echo " Quick start:"
echo ""
echo " zvm install master"
echo " zvm use master"
echo " zig version"
echo ""
}
main "$@"