-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinstall_zig.sh
executable file
·92 lines (79 loc) · 3.18 KB
/
install_zig.sh
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
#!/usr/bin/env sh
set -eu
ZIG_RELEASE_DEFAULT="latest"
# Default to the release build, or allow the latest dev build, or an explicit release version:
ZIG_RELEASE=${1:-$ZIG_RELEASE_DEFAULT}
if [ "$ZIG_RELEASE" = "latest" ]; then
ZIG_RELEASE="builds"
fi
# Validate the release version explicitly:
if echo "$ZIG_RELEASE" | grep -q '^builds$'; then
echo "Installing Zig latest build..."
elif echo "$ZIG_RELEASE" | grep -q '^[0-9]\+.[0-9]\+.[0-9]\+$'; then
echo "Installing Zig $ZIG_RELEASE release build..."
else
echo "Release version invalid"
exit 1
fi
# Determine the architecture:
if [ "$(uname -m)" = 'arm64' ] || [ "$(uname -m)" = 'aarch64' ]; then
ZIG_ARCH="aarch64"
else
ZIG_ARCH="x86_64"
fi
# Determine the operating system:
if [ "$(uname)" = "Linux" ]; then
ZIG_OS="linux"
else
ZIG_OS="macos"
fi
ZIG_TARGET="zig-$ZIG_OS-$ZIG_ARCH"
# Determine the build, split the JSON line on whitespace and extract the 2nd field, then remove quotes and commas:
if command -v wget; then
# -4 forces `wget` to connect to ipv4 addresses, as ipv6 fails to resolve on certain distros.
# Only A records (for ipv4) are used in DNS:
ipv4="-4"
# But Alpine doesn't support this argument
if [ -f /etc/alpine-release ]; then
ipv4=""
fi
# shellcheck disable=SC2086 # We control ipv4 and it'll always either be empty or -4
ZIG_URL=$(wget $ipv4 --quiet -O - https://ziglang.org/download/index.json | grep -F "$ZIG_TARGET" | grep -F "$ZIG_RELEASE" | awk '{print $2}' | sed 's/[",]//g')
else
ZIG_URL=$(curl --silent https://ziglang.org/download/index.json | grep -F "$ZIG_TARGET" | grep -F "$ZIG_RELEASE" | awk '{print $2}' | sed 's/[",]//g')
fi
# Ensure that the release is actually hosted on the ziglang.org website:
if [ -z "$ZIG_URL" ]; then
echo "Release not found on ziglang.org"
exit 1
fi
# Work out the filename from the URL, as well as the directory without the ".tar.xz" file extension:
ZIG_TARBALL=$(basename "$ZIG_URL")
ZIG_DIRECTORY=$(basename "$ZIG_TARBALL" .tar.xz)
# Download, making sure we download to the same output document, without wget adding "-1" etc. if the file was previously partially downloaded:
echo "Downloading $ZIG_URL..."
if command -v wget; then
# -4 forces `wget` to connect to ipv4 addresses, as ipv6 fails to resolve on certain distros.
# Only A records (for ipv4) are used in DNS:
ipv4="-4"
# But Alpine doesn't support this argument
if [ -f /etc/alpine-release ]; then
ipv4=""
fi
# shellcheck disable=SC2086 # We control ipv4 and it'll always either be empty or -4
wget $ipv4 --quiet --output-document="$ZIG_TARBALL" "$ZIG_URL"
else
curl --silent --output "$ZIG_TARBALL" "$ZIG_URL"
fi
# Extract and then remove the downloaded tarball:
echo "Extracting $ZIG_TARBALL..."
tar -xf "$ZIG_TARBALL"
rm "$ZIG_TARBALL"
# Replace any existing Zig installation so that we can install or upgrade:
echo "Installing $ZIG_DIRECTORY to 'zig' in current working directory..."
rm -rf zig
mv "$ZIG_DIRECTORY" zig
# It's up to the user to add this to their path if they want to:
ZIG_BIN="$(pwd)/zig/zig"
ZIG_VERSION=$($ZIG_BIN version)
echo "Congratulations, you have successfully installed Zig $ZIG_VERSION to $ZIG_BIN. Enjoy!"