|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + cat <<'EOF' |
| 6 | +usage: release-homebrew.sh <version> [tap-path] |
| 7 | +
|
| 8 | +Prepare a release for the rsworktree Homebrew tap by updating the formula |
| 9 | +URL, version, and checksum. The tap path defaults to $HOMEBREW_TAP_PATH or |
| 10 | +../homebrew-tap relative to this repository. |
| 11 | +EOF |
| 12 | +} |
| 13 | + |
| 14 | +if [[ $# -lt 1 ]]; then |
| 15 | + usage >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +require() { |
| 20 | + if ! command -v "$1" >/dev/null 2>&1; then |
| 21 | + echo "error: required command '$1' not found" >&2 |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +require git |
| 27 | +require curl |
| 28 | +require perl |
| 29 | + |
| 30 | +checksum_cmd="" |
| 31 | +if command -v shasum >/dev/null 2>&1; then |
| 32 | + checksum_cmd="shasum -a 256" |
| 33 | +elif command -v sha256sum >/dev/null 2>&1; then |
| 34 | + checksum_cmd="sha256sum" |
| 35 | +else |
| 36 | + echo "error: neither shasum nor sha256sum found" >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) |
| 41 | +VERSION=$1 |
| 42 | +TAP_PATH=${2:-${HOMEBREW_TAP_PATH:-"$REPO_ROOT/../homebrew-tap"}} |
| 43 | +FORMULA_PATH="$TAP_PATH/Formula/rsworktree.rb" |
| 44 | + |
| 45 | +if [[ ! -d "$TAP_PATH" ]]; then |
| 46 | + echo "error: tap path '$TAP_PATH' not found" >&2 |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +if [[ ! -f "$FORMULA_PATH" ]]; then |
| 51 | + echo "error: formula '$FORMULA_PATH' not found" >&2 |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +if [[ -n $(git -C "$TAP_PATH" status --porcelain) ]]; then |
| 56 | + echo "error: tap repository has uncommitted changes. Commit or stash them first." >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 61 | + echo "error: version '$VERSION' is not valid semver (expected X.Y.Z)" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +TARBALL_URL=${HOMEBREW_TARBALL_URL:-"https://github.com/ozan/rust-git-worktree/archive/refs/tags/v${VERSION}.tar.gz"} |
| 66 | + |
| 67 | +tmpdir=$(mktemp -d) |
| 68 | +trap 'rm -rf "$tmpdir"' EXIT |
| 69 | +tarball="$tmpdir/rsworktree-${VERSION}.tar.gz" |
| 70 | + |
| 71 | +echo "Downloading release tarball to compute checksum..." |
| 72 | +curl -sSL --fail "$TARBALL_URL" -o "$tarball" |
| 73 | + |
| 74 | +SHA256=$($checksum_cmd "$tarball" | awk '{print $1}') |
| 75 | + |
| 76 | +echo "Updating formula at $FORMULA_PATH" |
| 77 | + |
| 78 | +perl -0pi -e 's/^ url "[^"]+"/ url "'"$TARBALL_URL"'"/' "$FORMULA_PATH" |
| 79 | +perl -0pi -e 's/^ sha256 "[^"]+"/ sha256 "'"$SHA256"'"/' "$FORMULA_PATH" |
| 80 | +perl -0pi -e 's/^ version "[^"]+"/ version "'"$VERSION"'"/' "$FORMULA_PATH" 2>/dev/null || true |
| 81 | + |
| 82 | +# Remove stale bottle block so new bottles can be generated after the update. |
| 83 | +perl -0pi -e 's/^bottle do\n.*?^end\n\n//ms' "$FORMULA_PATH" || true |
| 84 | + |
| 85 | +git -C "$TAP_PATH" add "$FORMULA_PATH" |
| 86 | + |
| 87 | +tap_remote=$(git -C "$TAP_PATH" remote get-url origin 2>/dev/null || true) |
| 88 | +tap_name="" |
| 89 | +if [[ -n "$tap_remote" ]]; then |
| 90 | + case "$tap_remote" in |
| 91 | + git@github.com:*) tap_name=${tap_remote#git@github.com:}; tap_name=${tap_name%.git} ;; |
| 92 | + https://github.com/*) tap_name=${tap_remote#https://github.com/}; tap_name=${tap_name%.git} ;; |
| 93 | + *) tap_name="" ;; |
| 94 | + esac |
| 95 | +fi |
| 96 | + |
| 97 | +echo "Formula updated. Next steps:" |
| 98 | +echo " 1. cd $TAP_PATH" |
| 99 | +echo " 2. git commit -m \"rsworktree $VERSION\"" |
| 100 | +echo " 3. (optional) brew install --build-from-source Formula/rsworktree.rb" |
| 101 | +if [[ -n "$tap_name" ]]; then |
| 102 | + echo " 4. (optional) brew audit --tap $tap_name rsworktree --strict" |
| 103 | +else |
| 104 | + echo " 4. (optional) brew audit --strict Formula/rsworktree.rb" |
| 105 | +fi |
| 106 | +echo " 5. git push / open PR" |
0 commit comments