Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

homebrew-tap
Clone

Read only, no credentials:

https://rodent.software/homebrew-tap.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/homebrew-tap.git

Homebrew tap for Rodent Software.

Code

9 commit(s) on main

update readme Keenan Nemetz · 3d ago c55615f1
history blame raw 4.6 KiB
1#! /bin/sh
2# Refresh one project's formula to a published release.
3#
4# This is a shared tap: every Rodent Software project ships its formula here.
5# The formula's version and checksums must agree with the release's own
6# SHA256SUMS, so this script reads that list for a tag, verifies each asset the
7# formula names against it, and rewrites the version and checksums. It refuses to
8# write a checksum that does not match the downloaded asset, so a bad release or
9# a typo fails the bump rather than the install.
10#
11# Usage:
12# scripts/autobump.sh vole # the newest tag reachable from the homepage
13# scripts/autobump.sh vole v0.2.0 # a specific tag
14#
15# It only edits Formula/<project>.rb; commit and push are the caller's job. The
16# asset set is read from the formula's own `url` lines, so a macOS-only project
17# works the same as one that also ships Linux binaries.
18set -eu
19
20ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
21cd "$ROOT"
22
23project=${1:?usage: scripts/autobump.sh <project> [<tag>]}
24tag=${2:-}
25
26# A project's homepage is https://rodent.software/<project>, and its formula is
27# Formula/<project>.rb. Projects whose URL differs can override BASE_URL with the
28# full project root (no trailing slash).
29BASE_URL=${BASE_URL:-https://rodent.software/$project}
30FORMULA=Formula/$project.rb
31
32fail() {
33 printf 'autobump: %s\n' "$*" >&2
34 exit 1
35}
36
37[ -f "$FORMULA" ] || fail "no formula for $project at $FORMULA"
38
39sha256() {
40 if command -v sha256sum >/dev/null 2>&1; then
41 sha256sum "$1" | awk '{print $1}'
42 elif command -v shasum >/dev/null 2>&1; then
43 shasum -a 256 "$1" | awk '{print $1}'
44 else
45 fail "sha256sum or shasum is required"
46 fi
47}
48
49curl_with_retry() {
50 curl -fsSL --retry 3 --retry-delay 1 "$@"
51}
52
53# The formula carries one `url` line per asset and one `sha256` line per asset,
54# in the same order. Read the asset names from the URL basenames so a project
55# that ships only macOS, or one that adds another platform, needs no change here.
56assets=$(sed -n 's|.*artifacts/\([^"/][^"]*\)\".*|\1|p' "$FORMULA")
57[ -n "$assets" ] || fail "$FORMULA has no artifact url lines"
58
59if [ -z "$tag" ]; then
60 # The project homepage names its current stable release. This is a discovery
61 # step, so a page that moves does not brick a manual bump: say so and stop.
62 # `v\?` is a GNU extension: BSD sed (macOS's /bin/sed) has no `\?`, matches
63 # nothing, and the script stopped with "no release tag found" on a Mac. The
64 # BRE interval `\{0,1\}` means the same thing portably.
65 #
66 # The `v` stays inside the capture on purpose: the tag is used verbatim to
67 # build the artifact URL (/releases/<tag>/artifacts/...), and only `version`
68 # drops the prefix. Capturing the bare number sent that URL to a 404.
69 tag=$(curl_with_retry "$BASE_URL" | sed -n 's|.*/releases/\(v\{0,1\}[0-9][^/" ]*\).*|\1|p' | head -n 1)
70 [ -n "$tag" ] || fail "no release tag found on $BASE_URL; pass one explicitly"
71 printf 'autobump: %s: using latest release %s\n' "$project" "$tag"
72fi
73
74sums=$(mktemp)
75trap 'rm -f "$sums" "${sums}.dl"' 0
76
77curl_with_retry "$BASE_URL/releases/$tag/artifacts/SHA256SUMS" -o "$sums" ||
78 fail "cannot read SHA256SUMS for $tag"
79
80verify() { # asset
81 name=$1
82 checksum=$(awk -v a="$name" '$2 == a {print $1}' "$sums")
83 [ -n "$checksum" ] || fail "SHA256SUMS for $tag has no $name entry"
84 curl_with_retry "$BASE_URL/releases/$tag/artifacts/$name" -o "${sums}.dl" ||
85 fail "cannot download $name for $tag"
86 got=$(sha256 "${sums}.dl")
87 [ "$got" = "$checksum" ] || fail "$name does not match SHA256SUMS (got $got, want $checksum)"
88 printf '%s\n' "$checksum"
89}
90
91# Verify every asset and collect its checksum in formula order.
92checksums=""
93for asset in $assets; do
94 sha_line=$(verify "$asset")
95 checksums="$checksums $sha_line"
96done
97
98version=${tag#v}
99
100# Rewrite the version and the checksums in the formula, preserving the order the
101# `sha256` lines already appear in (which matches the `url` order).
102python3 - "$FORMULA" "$version" $checksums <<'PY'
103import re, sys
104path = sys.argv[1]
105version = sys.argv[2]
106checksums = sys.argv[3:]
107with open(path) as f:
108 lines = f.readlines()
109
110out = []
111sha_index = 0
112for line in lines:
113 mv = re.match(r'^( *)version ".*"', line)
114 ms = re.match(r'^( *)sha256 ".*"', line)
115 if mv:
116 out.append(f'{mv.group(1)}version "{version}"\n')
117 elif ms:
118 sha = checksums[sha_index]
119 sha_index += 1
120 out.append(f'{ms.group(1)}sha256 "{sha}"\n')
121 else:
122 out.append(line)
123
124if sha_index != len(checksums):
125 sys.exit(f'formula has {sha_index} sha256 lines but {len(checksums)} assets were verified')
126
127with open(path, 'w') as f:
128 f.writelines(out)
129PY
130
131printf 'autobump: wrote %s for %s (version %s)\n' "$FORMULA" "$tag" "$version"
132