A Custom Updater for Zig

So I’ve been playing with Zig lately, and one of the fun caveats of using a programming language thats in rapid development is that you need to update a lot, often using the latest nightly build due to some bugfix or new experimental feature. It makes it very entertaining to use and one of these days I should write down my thoughts in general on Zig… but I digress.

I needed a quick way to make sure that I can update both Zig and zls, the Zig language server. There is a tool out there called zigup but it doesn’t work well cross platform and I jump between Arch Linux, macOS, and OpenBSD regularly.

This zsh script is what I use on Linux and macOS to keep Zig updated, and I’ll update it to work on OpenBSD’s ksh when I have time (ha yeah right). It requires curl, wget, and jq. I keep all my local executables in $HOME/.local/bin, so modify that to fit your environment as needed

#!/usr/bin/env zsh

set -e

# Cleanup
rm -rf /tmp/zig
rm -rf /tmp/zls
mkdir -p /tmp/zls
mkdir -p /tmp/zig

if [[ $(uname) == "Darwin" ]]; then
	url="`curl https://ziglang.org/download/index.json | jq -r '.["master"]["aarch64-macos"]["tarball"]'`"
	wget -P /tmp/zls https://zig.pm/zls/downloads/aarch64-macos/bin/zls
	fname="zig-macos*"
else
	url="`curl https://ziglang.org/download/index.json | jq -r '.["master"]["x86_64-linux"]["tarball"]'`"
	wget -P /tmp/zls https://zig.pm/zls/downloads/x86_64-linux/bin/zls
	fname="zig-linux*"
fi

wget -P /tmp/zig $url
tar xf /tmp/zig/*.tar.xz -C /tmp/zig
rm -f /tmp/zig/*.tar.xz

ZIG_FOLDER=$(find /tmp/zig/ -type d -name "$fname" -print -quit)
if [ -z "$ZIG_FOLDER" ]; then
  echo "No folder starting with 'zig' found."
  exit 1
fi

# Zig
rm -rf $HOME/.local/zig
mkdir -p $HOME/.local/zig
cp -r "$ZIG_FOLDER"/* $HOME/.local/zig/
rm -f $HOME/.local/bin/zig
chmod u+x $HOME/.local/zig/zig
ln -sf $HOME/.local/zig/zig $HOME/.local/bin/zig

# ZLS
rm -rf $HOME/.local/bin/zls
mv /tmp/zls/zls $HOME/.local/bin/zls
chmod u+x $HOME/.local/bin/zls

fputs.com


2023-11-27