Skip to content

Instantly share code, notes, and snippets.

@tamadamas
Last active November 13, 2025 18:06
Show Gist options
  • Select an option

  • Save tamadamas/b70c7d2eb59cc9a4c2b0052529f8c558 to your computer and use it in GitHub Desktop.

Select an option

Save tamadamas/b70c7d2eb59cc9a4c2b0052529f8c558 to your computer and use it in GitHub Desktop.
anyzig wrapper with default zig version (It's writen with AI, so free to suggest ANY improments)
#!/usr/bin/env sh
# Anyzig wrapper script with default version support
# Compatible with: Bash, Zsh, Fish, and other POSIX shells
#
# Installation:
# 1. Save this script as 'zig' (or any name you prefer)
# 2. Make it executable: chmod +x zig
# 3. Place it in your PATH before the actual zig executable
# 4. Set ZIG_DEFAULT_VERSION environment variable (optional)
#
# Usage:
# export ZIG_DEFAULT_VERSION="0.15.1" # Set your default version
# zig build # Uses version from build.zig.zon or default
# zig 0.15.0 build # Explicitly specify version
# Configuration: Set your default Zig version here or via environment variable
# You can set this in your shell rc file (.bashrc, .zshrc, config.fish)
DEFAULT_VERSION="${ZIG_DEFAULT_VERSION:-0.15.1}"
# Path to the actual anyzig executable
# Adjust this if anyzig is installed in a different location
ANYZIG_CMD="anyzig"
# Check if anyzig is available
if ! command -v "$ANYZIG_CMD" >/dev/null 2>&1; then
echo "Error: anyzig not found in PATH" >&2
echo "Please install anyzig first" >&2
exit 1
fi
# Function to check if first argument looks like a version number
is_version() {
case "$1" in
[0-9]*.[0-9]*.[0-9]*|[0-9]*.[0-9]*.[0-9]*-dev.*)
return 0
;;
*)
return 1
;;
esac
}
# Check if first argument is already a version
if [ $# -gt 0 ] && is_version "$1"; then
# Version already specified, pass through to anyzig
exec "$ANYZIG_CMD" "$@"
fi
# Try to run anyzig without version first
# If it fails with the "no build.zig" error, retry with default version
"$ANYZIG_CMD" "$@" 2>&1 | {
output=""
has_error=0
while IFS= read -r line; do
output="${output}${line}
"
if echo "$line" | grep -q "no build.zig to pull a zig version from"; then
has_error=1
fi
done
if [ $has_error -eq 1 ]; then
# Rerun with default version
exec "$ANYZIG_CMD" "$DEFAULT_VERSION" "$@"
else
# Print captured output and exit with anyzig's exit code
printf "%s" "$output"
exit $?
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment