Get Zig

On Mac OSX, there is no automatic way to keep up with the latest zig compiler. On Ubuntu Linux, once the snap is installed with the –edge flag, the snap will keep up to date with the edge of development. This can sometimes be ‘fun’ as things within the compiler change from underneath you. However, on OSX there is no such joy.

For example, homebrew can install zig, it is only follows the last full release. At the time of writing this is 0.11, which has a number of bugs in it that I’d prefer not to deal with.

Therefore, I wrote my own…

get_zig.py

I wrote this in python, my go-to-hack-something-together programming language that doesn’t need any performance.

import urllib.request
from bs4 import BeautifulSoup
import os
import sys

path = os.path.realpath (__file__)
directory = os.path.dirname(path)

os.chdir (directory)

url = r'https://ziglang.org/download/'
print (url)
content = urllib.request.urlopen (url).read ()
soup = BeautifulSoup (content, 'html.parser')
for link in soup.find_all ('a'):
    url = link.get('href')
    if 'macos-aarch64-0.12' in url and url[-6:] == 'tar.xz':
        print (url)
        index = url.rfind ('/')
        filename = url[index+1:]
        partname = url[index+1:-7]
        data = urllib.request.urlopen (url).read ()
        with open(filename, 'wb') as fp:
            fp.write (data)
        print (f"{filename}")
        os.system (f"tar xf {filename}")
        os.system ("rm -rf ~/Library/zig")
        os.system ("mkdir ~/Library/zig")
        os.system (f"mv {partname}/* ~/Library/zig")
        os.system (f"rm -rf {partname}")
        os.system (f"rm -f {filename}")
        print ("")
        os.system ("zig version")
        os.system ("zig zen")
        sys.exit (0)
sys.exit (1)

The first thing this does is import a number of useful little libraries:

  • urllib.request - to allow the opening and reading of webpages.
  • BeautifulSoup - to parse webpages locally to extract useful information.
  • os - to allow us to execute system commands
  • sys - to allow us to exit with an error code (or not)

We grab path of the executable file and change to the directory that it is in. This allows us to place all temporary files in that directory.

Then we open and download the ziglang.org/download/ webpage and pass that into BeautifulSoup, parsing it as an html file.

We then loop through all the links in that file, getting the target href. We only care about a file that starts with macos-aarch64-0.12 and ending in .tar.xz. This is the file we want.

Once we have the url, we can determine the filename by finding the index of the last / so that we can just use all the characters in the URL after that. We also determine the directory name of the future extracted tar file by ignoring the extension.

We then read the URL, writing it out the data to the filename. We then do a whole sequence of system commands:

  • extract the information using tar
  • remove the old ~/Library/zig directory and all the files within
  • recreate the ~/Library/zig directory again
  • mv everything from the extracted tar file into the ~/Library/zig directory
  • remove the extracted files
  • remove the tar file
  • exit with success

If we never find the tar file in the original downloads webpage, then we exit with an error.

Get Zig zig python