Last active
December 21, 2022 22:57
-
-
Save maxmilton/dc4a88cb100bda7b0ee42ce582b47bfe to your computer and use it in GitHub Desktop.
Zig master branch build version updater bash script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -Eeuo pipefail | |
trap cleanup SIGINT SIGTERM ERR EXIT | |
# Zig Update; Check and download the latest zig master branch build | |
# Inspired by: https://gist.github.com/MasterQ32/1f27081d8a7dd109fc290bd6729d97a8 | |
ROOT_DIR=/usr/local/bin | |
TMP_DIR=/tmp/zig-up | |
REPO_URL=https://ziglang.org/download/index.json | |
REPO_ARCH=x86_64-linux | |
cleanup() { | |
trap - SIGINT SIGTERM ERR EXIT | |
rm -r "${TMP_DIR}" | |
} | |
msg() { echo >&2 -e "${1-}"; } | |
die() { msg "$@"; exit 1; } | |
mkdir -p "${TMP_DIR}" | |
test -d "${ROOT_DIR}" || die "Root directory \"${ROOT_DIR}\" does not exist!" | |
type curl >/dev/null 2>&1 || die "curl is required but not available!" | |
type jq >/dev/null 2>&1 || die "jq is required but not available!" | |
type shasum >/dev/null 2>&1 || die "shasum is required but not available!" | |
msg "Getting version info..." | |
repo=$(curl -s "${REPO_URL}" | jq ".master[\"${REPO_ARCH}\"]") || die "Failed to aquire version info!" | |
tarball=$(echo "${repo}" | jq --raw-output '.tarball') | |
shasum=$(echo "${repo}" | jq --raw-output '.shasum') | |
size=$(echo "${repo}" | jq --raw-output '.size') | |
filename=${tarball##*/} # basename | |
version=${filename%.tar.xz} | |
test "${tarball:0:27}" == "https://ziglang.org/builds/" || die "Unexpected download path!" | |
test -z "${version}" && die "Failed to extract version info!" | |
if test "${ROOT_DIR}/zig-latest/zig" -ef "${ROOT_DIR}/${version}/zig"; then | |
msg "${version} is already the current version!" | |
else | |
msg "Updating to ${version}" | |
curl "${tarball}" -o "${TMP_DIR}/${filename}" || die "Failed to download!" | |
echo "${shasum} ${TMP_DIR}/${filename}" | shasum -c - || die "Failed to verify checksum!" | |
test "${size}" -eq "$(stat -c '%s' "${TMP_DIR}/${filename}")" || die "Download size mismatch!" | |
tar -xJC "${ROOT_DIR}" -f "${TMP_DIR}/${filename}" || die "Failed to extract!" | |
rm -rf "${ROOT_DIR}/zig-latest" "${ROOT_DIR}/zig_" || die "Failed to remove old symlinks!" | |
ln -s "${version}" "${ROOT_DIR}/zig-latest" || die "Failed to set new symlink!" | |
ln -s zig-latest/zig "${ROOT_DIR}/zig_" || die "Failed to set new symlink!" | |
# Clean up old versions; keep the last 5 downloads | |
find "${ROOT_DIR}"/zig-linux-x86_64-* -prune | head -n -5 | while IFS='' read -r path; do | |
rm -r "$path" | |
done | |
fi | |
msg "Current version: $("${ROOT_DIR}/zig-latest/zig" version)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment