zignew and zigswitch (Bash scripts)
Created: 2023-03-04If you’ve been using Zig for a while in its pre-1.0 state, then you have probably downloaded a new binary build of Zig quite a few times from: https://ziglang.org/download/
My usual process to upgrade was always something like:
$ wget https://ziglang.org/builds/zig-linux-x86-<very long build version>.tar.xz $ tar xf zig-linux-x86-<very long build version>.tar.xz $ ln -s zig-linux-x86-<very long build version> zig
(I have the symlink zig above in my $PATH, so I can immediately run the new
executable.)
I eventually developed an extremely crude shell script that automated at least a little bit of that.
But the other thing that kept happening is that I would need to run different release versions of Zig in addition to the dev build. (Some Zig software authors wisely pick a release version for their applications. So you need to use those release versions to build that application.)
So now I’ve got two scripts:
zignew extracts a new Zig build tarball and updates my symlink.
zigswitch uses fuzzy matching (so cool!) to let me pick from a menu of installed Zig versions and updates the symlink.
The fuzzy matcher, zf, is https://github.com/natecraddock/zf
Here’s my scripts. I’m not making these a repo or anything (they’re already part of my personal $HOME/bin repo) because they will probably require some tailoring for anyone else to use. But maybe they’ll inspire someone else to do something similar:
zignew
Running it:
$ wget https://ziglang.org/builds/zig-linux-x86_64-0.11.0-dev.1842+6be5946ed.tar.xz $ zignew Unpacking /home/dave/zig-linux-x86_64-0.11.0-dev.1842+6be5946ed.tar.xz... Deleting the archive file '/home/dave/zig-linux-x86_64-0.11.0-dev.1842+6be5946ed.tar.xz'... Replacing zig symlink to point to new version Zig version is now: 0.11.0-dev.1842+6be5946ed
The source:
#!/usr/bin/bash
# Extract new Zig version tarball.
# Then switch zig versions via symlink.
#
# This script assumes the following:
#
# 1. Zig releases are in user $HOME dir and named zig-*
# 2. $HOME/zig is in $PATH so zig executable can be found
# 3. The new Zig tarball is also in $HOME
# Find zig archives (and there must be exactly one!)
zigzip=$(find $HOME -maxdepth 1 -name zig*.tar.xz)
# there is probably a better way to do this...
zipcount=0
for z in $zigzip
do
zipcount=$(($zipcount + 1))
done
if [[ $zipcount -lt 1 ]]
then
echo "Sorry, didn't find any zig archives in $HOME"
exit 1
fi
if [[ $zipcount -gt 1 ]]
then
echo "Sorry, found MORE than one zig archive in $HOME:"
echo $zigzip
exit 1
fi
echo "Unpacking $zigzip..."
tar -xf "$zigzip"
if [[ $? -ne 0 ]]
then
echo "Sorry, looks like that failed."
exit 1
fi
echo "Deleting the archive file '$zigzip'..."
rm "$zigzip"
# decompressed archive should be same name minus the .tar.xz
echo "Replacing zig symlink to point to new version"
rm -f $HOME/zig
new_zig_dir="${zigzip%.tar.xz}"
ln -s "$new_zig_dir" "$HOME/zig"
printf "\nZig version is now: "
zig version
zigswitch
Running it:
$ zigswitch > /home/dave/zig-0.10.0 /home/dave/zig-linux-x86_64-0.11.0-dev.1842+6be5946ed /home/dave/zig-0.11.0-dev /home/dave/zig-0.10.1 # I select the zig-0.10.0 version from the menu above... Replacing zig symlink to point to /home/dave/zig-0.10.0 Zig version is now: 0.10.0
The source:
#!/usr/bin/bash # Switch zig versions via symlink. This script assumes two things: # # 1. Zig releases are in user $HOME dir and named zig-* # 2. $HOME/zig is in $PATH so zig executable can be found # zf is a fuzzy finder # https://github.com/natecraddock/zf switchversion=$(find $HOME -type d -maxdepth 1 -name 'zig-*' | zf) echo "Replacing zig symlink to point to $switchversion" rm -f $HOME/zig ln -s "$switchversion" "$HOME/zig" printf "\nZig version is now: " zig version