|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Update the Homebrew formula with checksums from a GitHub release. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# ./scripts/update-homebrew-formula.sh [--dry-run] <version> [tap-path] |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# ./scripts/update-homebrew-formula.sh 1.0.0 |
| 11 | +# ./scripts/update-homebrew-formula.sh --dry-run 0.1.4 |
| 12 | +# ./scripts/update-homebrew-formula.sh 1.0.0 ~/Code/homebrew-allium |
| 13 | + |
| 14 | +REPO="juxt/allium-tools" |
| 15 | +TARGETS=( |
| 16 | + aarch64-apple-darwin |
| 17 | + x86_64-apple-darwin |
| 18 | + aarch64-unknown-linux-gnu |
| 19 | + x86_64-unknown-linux-gnu |
| 20 | +) |
| 21 | + |
| 22 | +DRY_RUN=false |
| 23 | +VERSION="" |
| 24 | +TAP_PATH="" |
| 25 | + |
| 26 | +for arg in "$@"; do |
| 27 | + case "$arg" in |
| 28 | + --dry-run) DRY_RUN=true ;; |
| 29 | + -*) echo "Unknown flag: $arg" >&2; exit 1 ;; |
| 30 | + *) |
| 31 | + if [[ -z "$VERSION" ]]; then |
| 32 | + VERSION="$arg" |
| 33 | + else |
| 34 | + TAP_PATH="$arg" |
| 35 | + fi |
| 36 | + ;; |
| 37 | + esac |
| 38 | +done |
| 39 | + |
| 40 | +if [[ -z "$VERSION" ]]; then |
| 41 | + echo "Usage: update-homebrew-formula.sh [--dry-run] <version> [tap-path]" >&2 |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 46 | + echo "Error: version must be in semver format (e.g. 1.0.0)" >&2 |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +# Default tap path: sibling directory to this repo |
| 51 | +if [[ -z "$TAP_PATH" ]]; then |
| 52 | + TAP_PATH="$(cd "$(dirname "$0")/../.." && pwd)/homebrew-allium" |
| 53 | +fi |
| 54 | + |
| 55 | +FORMULA="$TAP_PATH/Formula/allium.rb" |
| 56 | + |
| 57 | +if [[ ! -f "$FORMULA" ]]; then |
| 58 | + echo "Error: formula not found at $FORMULA" >&2 |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +echo "Fetching checksums for v$VERSION..." |
| 63 | + |
| 64 | +declare -A CHECKSUMS |
| 65 | +for target in "${TARGETS[@]}"; do |
| 66 | + url="https://github.com/$REPO/releases/download/v$VERSION/allium-$target.tar.gz" |
| 67 | + echo " $target" |
| 68 | + sha=$(curl -fsSL "$url" | shasum -a 256 | awk '{print $1}') |
| 69 | + CHECKSUMS[$target]="$sha" |
| 70 | +done |
| 71 | + |
| 72 | +echo "" |
| 73 | +echo "Checksums:" |
| 74 | +for target in "${TARGETS[@]}"; do |
| 75 | + echo " $target: ${CHECKSUMS[$target]}" |
| 76 | +done |
| 77 | +echo "" |
| 78 | + |
| 79 | +if $DRY_RUN; then |
| 80 | + echo "(dry run — formula not modified)" |
| 81 | + exit 0 |
| 82 | +fi |
| 83 | + |
| 84 | +# Update version |
| 85 | +sed -i '' "s/^ version \".*\"/ version \"$VERSION\"/" "$FORMULA" |
| 86 | + |
| 87 | +# Update checksums using the placeholder/previous values |
| 88 | +for target in "${TARGETS[@]}"; do |
| 89 | + # Convert target to the placeholder name: hyphens to underscores, uppercase |
| 90 | + placeholder=$(echo "$target" | tr '-' '_' | tr '[:lower:]' '[:upper:]') |
| 91 | + sha="${CHECKSUMS[$target]}" |
| 92 | + # Replace either a placeholder or a previous 64-char hex hash on the line following the matching URL |
| 93 | + sed -i '' "/allium-${target}\.tar\.gz/{ n; s/sha256 \"[^\"]*\"/sha256 \"${sha}\"/; }" "$FORMULA" |
| 94 | +done |
| 95 | + |
| 96 | +echo "Updated $FORMULA to v$VERSION" |
0 commit comments