Skip to content

Instantly share code, notes, and snippets.

@antonvasin
Last active August 8, 2024 11:59
Show Gist options
  • Select an option

  • Save antonvasin/e2c6f879df55505d1fe3a93b8786bfbb to your computer and use it in GitHub Desktop.

Select an option

Save antonvasin/e2c6f879df55505d1fe3a93b8786bfbb to your computer and use it in GitHub Desktop.
Simple script to update zig and zls to latest versions
#!/usr/bin/env bash
set -e
# Define base directory for installations
BASE_DIR="$HOME/.zig"
ZLS_DIR="$BASE_DIR/zls"
ZIG_URL="https://ziglang.org/download/index.json"
ZLS_URL="https://github.com/zigtools/zls.git"
ZLS_COMMIT_FILE="$BASE_DIR/.last_zls_commit"
mkdir -p $BASE_DIR
# Function to check and download the latest Zig version
update_zig() {
# Fetch the latest nightly version information
NIGHTLY_VERSION=$(curl -s $ZIG_URL | jq -r '.master."aarch64-macos".tarball' | sed 's|.*/||')
NIGHTLY_DIR_NAME=$(echo $NIGHTLY_VERSION | sed 's|.tar.xz||')
# Check if the folder for the latest version already exists
if [ -d "$BASE_DIR/$NIGHTLY_DIR_NAME" ]; then
echo "Latest Zig nightly ($NIGHTLY_DIR_NAME) is already downloaded."
return
fi
# Download and extract if not present
echo "Downloading Zig nightly version: $NIGHTLY_DIR_NAME"
curl -L "https://ziglang.org/builds/$NIGHTLY_VERSION" -o "$BASE_DIR/$NIGHTLY_VERSION"
mkdir -p "$BASE_DIR/$NIGHTLY_DIR_NAME"
tar -xJf "$BASE_DIR/$NIGHTLY_VERSION" -C "$BASE_DIR/$NIGHTLY_DIR_NAME" --strip-components=1
rm "$BASE_DIR/$NIGHTLY_VERSION"
# Update the symlink to point to the new version
ln -sfn "$BASE_DIR/$NIGHTLY_DIR_NAME" "$BASE_DIR/latest"
echo "Updated symlink to latest Zig version."
}
latest_commit_hash() {
git -C "$1" rev-parse HEAD
}
update_zls() {
# Clone or update ZLS repository
if [ -d "$ZLS_DIR/.git" ]; then
echo "Updating ZLS repository..."
git -C "$ZLS_DIR" pull
else
echo "Cloning ZLS repository..."
git clone $ZLS_REPO_URL $ZLS_DIR
fi
# Get the current latest commit hash
current_commit=$(latest_commit_hash $ZLS_DIR)
# Read last built commit hash if file exists
last_built_commit=$(cat $ZLS_COMMIT_FILE 2>/dev/null || echo "")
# Check if we need to rebuild ZLS
if [[ "$current_commit" == "$last_built_commit" ]]; then
echo "ZLS is already up-to-date with the latest commit. Skipping build."
return
else
# Build ZLS
echo "Building ZLS from source..."
cd $ZLS_DIR
zig build -Doptimize=ReleaseSafe
# Ensure the binary is executable (assuming binary path)
ZLS_BINARY="$ZLS_DIR/zig-out/bin/zls"
chmod +x $ZLS_BINARY
# Update the symlink to point to the new binary
ln -sfn $ZLS_BINARY "$BASE_DIR/zls-latest"
echo "ZLS is ready and symlinked to '$BASE_DIR/zls-latest'."
# Store the current commit hash as the last built
echo $current_commit > $ZLS_COMMIT_FILE
echo "Updated last built commit hash."
fi
}
update_zig
update_zls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment