-
Notifications
You must be signed in to change notification settings - Fork 0
/
zvm
executable file
·562 lines (457 loc) · 13.3 KB
/
zvm
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/bin/sh
readonly install_dir="$HOME/.local/share/zvm"
readonly bin_dir="$HOME/.local/bin"
ZVM_HOME=${ZVM_HOME:-$install_dir}
ZVM_BIN=${ZVM_BIN:-$bin_dir}
readonly url="https://ziglang.org/download/index.json"
readonly required_progs="curl,jq,tar,unzip"
readonly supported_extensions=".tar.xz"
readonly os="$(uname | tr '[:upper:]' '[:lower:]')"
readonly arch="$(uname -m)"
readonly native_target="${arch}-${os}"
readonly bin_name="zig"
readonly self_name="$(basename "$0")"
readonly self_version="0.1.0"
readonly versions_file="${ZVM_HOME}/versions.json"
# Global options
o_verbose=0
o_extra_verbose=0
o_target="$native_target"
################################################################################
# Logging functions #
################################################################################
vrb() {
[ $o_verbose -eq 1 ] && echo "vrb: ${1}" >&2
}
vrb2() {
[ $o_extra_verbose -eq 1 ] && echo "vrb ${1}" >&2
}
warn() {
echo "warn: ${1}" >&2
}
err() {
echo "err: ${1}" >&2
}
die() {
echo "fatal: ${1}" >&2 && exit 1
}
panic_missing_prog() {
command -v "$1" > /dev/null 2>&1 || die "'${1}' is required to run this script"
}
################################################################################
# Index functions #
################################################################################
# Initialize the local index file
index_init() {
mkdir -p "$ZVM_HOME"
versions=$(jq . "$versions_file" 2> /dev/null)
if ! [ -e "${versions_file}" ] || [ -z "${versions}" ] || [ "${versions}" = 'null' ]; then
echo '{"releases": {}, "active": null}' | jq > "$versions_file"
else
if ! [ -f "$versions_file" ]; then
die "Version manifest file is not a regular file"
fi
fi
}
# Read the local index
index_fetch_local() {
vrb "reading local index from ${versions_file}"
jq . "$versions_file"
}
# Read the remote index
index_fetch_remote() {
vrb "fetching remote index from ${url}"
curl_output=$(curl -s "${url}")
if [ $? -ne 0 ]; then
err "failed fetching index"
err "curl: $curl_output"
else
vrb "successfully fetched index"
echo "$curl_output"
fi
}
# List versions from the local index
index_list_local() {
index_fetch_local | jq -r '.releases|keys[]'
}
# List versions from the remote index
index_list_remote() {
index_fetch_remote | jq -r 'keys_unsorted|.[]'
}
# Add a release to the local index
# args:
# $1 Release name
# $2 Release JSON object
index_add_release() {
_version=$1
_json=$2
vrb "adding release '$_version' to local index"
current=$(cat "$versions_file")
echo "$current" | jq --argjson r "$_json" ".releases.\"$_version\"=\$r" > "$versions_file"
}
# Set release active
# args:
# $1 Release name
index_set_active() {
_version=$1
vrb "marking '$_version' installed"
current=$(cat "$versions_file")
echo "$current" | jq ".active=\"$_version\"" > "$versions_file"
}
# Get the active version
index_get_active() {
version=$(jq -r '.active' "$versions_file")
if [ "$version" = 'null' ]; then
version=""
fi
echo "$version"
}
# Remove active version (sets active field to null, doesn't remove anything)
index_remove_active() {
current=$(cat "$versions_file")
echo "$current" | jq ".active=null" > "$versions_file"
}
# Convert the master tag to a semver by reading remote index
index_get_master_version() {
vrb "fetching current master version from remote index"
master_version=$(index_fetch_remote | jq -r ".master.version")
if [ -z "$master_version" ] || [ "$master_version" = 'null' ]; then
err "failed retrieving master version from remote index"
exit 1
fi
echo "$master_version"
}
################################################################################
# File functions #
################################################################################
# Download a file
# args:
# $1 release name
# $2 release JSON object
file_download() {
_version=$1
_json=$2
info=$(echo "$_json" | jq ".\"$o_target\"")
if [ "$info" = 'null' ]; then
die "target '$o_target' not supported"
fi
tarball=$(echo "$info" | jq -r '.tarball')
extension=''
case "$tarball" in
*.tar.xz)
extension='.tar.xz'
;;
*.zip)
extension='.zip'
;;
*)
die "unsupported extension '$extension'"
;;
esac
shasum=$(echo "$info" | jq -r '.shasum')
filename="${_version}${extension}"
path="$ZVM_HOME/$_version/$filename"
dir=$(dirname "$path")
mkdir -p "$dir"
vrb "downloading $tarball ($extension) to $dir"
echo "$shasum *$path" > "$path.shasum"
curl -s --output "$path" "$tarball" > /dev/null 2>&1
vrb "verifying shasum"
shasum -sc "$path.shasum"
sha_result=$?
if [ $sha_result -ne 0 ]; then
err "shasum failed for $path"
exit 1
fi
vrb "shasum verified"
vrb "extracting $filename"
if [ "$extension" = '.tar.xz' ]; then
tar --strip-components=1 -xf "$path" -C "$dir"
elif [ "$extension" = '.zip' ]; then
unzip -q "$path" -d "$dir"
mv $dir/zig-*/* "$dir"
rm -r $dir/zig-*
fi
vrb "cleaning up"
rm "$path"
rm "$path.shasum"
}
################################################################################
# Command functions #
################################################################################
# command: list
# args: none
# options:
# -i, --installed List versions from the local index instead of remote
o_remote=1
cmd_list() {
for arg in "$@"; do
case $1 in
-i|--installed)
o_remote=0
shift
;;
*)
err "unrecognized argument: '$1'"
exit 1
esac
done
if [ $o_remote -eq 1 ]; then
index_list_remote
else
index_list_local
fi
}
# command: use
# args:
# $1 Version name from index
# options: none
cmd_use() {
_version=$1
if [ -z "$_version" ]; then
err "expected 1 positional argument"
exit 1
fi
if [ "$_version" = 'master' ]; then
_version=$(index_get_master_version)
fi
installed=$(index_fetch_local | jq -r ".releases.\"$_version\"")
if [ "$installed" = 'null' ]; then
err "version '$_version' is not installed; install it with '$self_name install $_version'"
exit 1
fi
src="$ZVM_HOME/$_version/$bin_name"
dst="$ZVM_BIN/$bin_name"
case "$o_target" in
*windows*)
src="$src.exe"
dst="$dst.exe"
;;
esac
# TODO: get the path to the dir
if ! [ -f "$src" ]; then
err "file not found: $src"
fi
ln -sf "$src" "$dst"
index_set_active "$_version"
}
# command: install
# args:
# $1 Version name from index
# options:
# -u, --use Make active immediately after install
o_use=0
cmd_install() {
for arg in "$@"; do
case $1 in
-u|--use)
o_use=1
shift
;;
esac
done
_version=$1
index_version=$_version
if [ "${_version}" = 'master' ]; then
_version=$(index_get_master_version)
fi
vrb "installing '$_version'"
installed=$(index_fetch_local | jq -r ".releases.\"$_version\"")
if [ "$installed" != 'null' ]; then
err "version '$_version' is already installed; see '$self_name use $_version'"
exit 1
fi
json=$(index_fetch_remote | jq -r ".\"$index_version\"")
if [ "${json}" = 'null' ]; then
err "version '$index_version' not found in remote index"
exit 1
fi
file_download "$_version" "$json"
if [ $? -ne 0 ]; then
err "failed installing $_version"
exit 1
fi
index_add_release "$_version" "$json"
if [ $o_use -eq 1 ]; then
cmd_use "$_version"
fi
}
# command: prune
# args: none
# options: none
cmd_prune() {
active=$(jq -r '.active' "$versions_file")
installed=$(index_list_local)
vrb "pruning unused versions"
for version in $installed; do
if [ "$active" = "$version" ];then
vrb "skipping active version '$active'"
continue
fi
vrb "uninstalling '$version'"
cmd_uninstall "$version"
done
}
# command: uninstall
# args:
# $1: Version to uninstall
# options: none
cmd_uninstall() {
_version=$1
installed=$(jq ".releases.\"$_version\"" "$versions_file")
if [ -z "$installed" ] || [ "$installed" = 'null' ]; then
err "version '$_version' is not installed"
exit 1
fi
vrb "removing files from disk"
dir="$ZVM_HOME/$_version"
rm -rf "$dir"
if [ -e "$ZVM_BIN/$bin_name" ]; then
unlink "$ZVM_BIN/$bin_name"
else
vrb "skipping non-existent symlink"
fi
vrb "removing from index"
json=$(jq . "$versions_file")
echo "$json" | jq "del(.releases.\"$_version\")" > "$versions_file"
current_install=$(jq -r '.active' "$versions_file")
if [ "$current_install" = "$_version" ]; then
warn "active version was uninstalled; use '$self_name use <version>' to switch to an available version"
index_remove_active
fi
}
# Print usage and exit
usage() {
cat >&2 <<usage_text
$self_name - Zig Version Manager
Usage: zvm [OPTIONS] [command] [...]
Options:
-t, --target Specify a custom target to use
-v Enable verbose script output
-vv Enable even more verbose output
--version Print the version and exit
Commands:
list [OPTIONS] List Zig versions available in the remote index
-i, --installed List installed Zig versions
install [OPTIONS] <version> Install a Zig version
-u, --use Make version active immediately
use <version> Use an installed Zig version
uninstall <version> Uninstall a Zig version
prune Uninstall all but the currently active version
help Show this help page
Aliases:
list: ls
install: i
uninstall: rm
help: h
Environment:
ZVM_HOME Path where zvm downloads and extracts zig releases
Default: \$HOME/.local/share/zvm/
ZVM_BIN Path where zvm symlinks zig binaries
Default: \$HOME/.local/bin/
usage_text
}
# Ensure all required programs are installed, and panic otherwise
ensure_required_progs() {
(
IFS=','; for prog in $required_progs; do
panic_missing_prog "$prog"
done
)
}
# Parse global options (options before command)
args_parse_global() {
for arg in "$@"; do
case $1 in
# Parse global options
-h | --help | h | help)
usage
exit 0
;;
-t | --target)
shift
o_target="$1"
shift
;;
-vv)
o_extra_verbose=1
o_verbose=1
shift
;;
-v | --verbose)
o_verbose=1
shift
;;
--version)
echo "$self_version" >&2
exit 0
;;
*)
break
;;
esac
done
echo $@
}
# Parse command and call its function
args_parse_command() {
if [ $# -eq 0 ]; then
index_get_active
exit 0
fi
for arg in "$@"; do
case $1 in
# Parse commands
ls|list)
shift
cmd_list $@
exit $?
;;
i|install)
shift
cmd_install $@
exit $?
;;
prune)
shift
cmd_prune $@
exit $?
;;
rm|uninstall)
shift
cmd_uninstall $@
exit $?
;;
use)
shift
cmd_use $@
exit $?
;;
*)
echo "unrecognized command '$1'"
;;
esac
done
}
################################################################################
# Script start #
################################################################################
! [ -d "$ZVM_HOME" ] && mkdir -p "$ZVM_HOME" > /dev/null
! [ -d "$ZVM_BIN" ] && mkdir -p "$ZVM_BIN" > /dev/null
if [ "$(basename "$ZVM_HOME")" != "zvm" ]; then
die "'ZVM_HOME' is invalid (must be named 'zvm')"
exit 1
fi
ensure_required_progs
index_init
# TODO(cleanup): have to call args_parse_global twice: first to set global
# options, then to get command without global options
args_parse_global $@ > /dev/null
# TODO: clean this up... we don't know which options, if any, are provided
# before this line
vrb2 "zvm version: $self_version"
vrb2 "ZVM_HOME: $ZVM_HOME"
vrb2 "ZVM_BIN: $ZVM_BIN"
vrb2 "native_target: $native_target"
vrb2 "target: $o_target"
args_parse_command $(args_parse_global $@)