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.

Blame · scripts/autobump.sh

Line attribution on main · 131 line(s)

ab3f4cba
keenan
7d ago
1 #! /bin/sh
8b2c3ebf
keenan
7d ago
2 # Refresh one project's formula to a published release.
ab3f4cba
keenan
7d ago
3 #
8b2c3ebf
keenan
7d ago
4 # This is a shared tap: every Rodent Software project ships its formula here.
ab3f4cba
keenan
7d ago
5 # The formula's version and checksums must agree with the release's own
100ec144
keenan
7d ago
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.
ab3f4cba
keenan
7d ago
10 #
11 # Usage:
8b2c3ebf
keenan
7d ago
12 # scripts/autobump.sh vole # the newest tag reachable from the homepage
13 # scripts/autobump.sh vole v0.2.0 # a specific tag
ab3f4cba
keenan
7d ago
14 #
100ec144
keenan
7d ago
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.
ab3f4cba
keenan
7d ago
18 set -eu
19
20 ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
21 cd "$ROOT"
22
8b2c3ebf
keenan
7d ago
23 project=${1:?usage: scripts/autobump.sh <project> [<tag>]}
24 tag=${2:-}
25
100ec144
keenan
7d ago
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).
8b2c3ebf
keenan
7d ago
29 BASE_URL=${BASE_URL:-https://rodent.software/$project}
30 FORMULA=Formula/$project.rb
ab3f4cba
keenan
7d ago
31
32 fail() {
33 printf 'autobump: %s\n' "$*" >&2
34 exit 1
35 }
36
8b2c3ebf
keenan
7d ago
37 [ -f "$FORMULA" ] || fail "no formula for $project at $FORMULA"
38
ab3f4cba
keenan
7d ago
39 sha256() {
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
49 curl_with_retry() {
50 curl -fsSL --retry 3 --retry-delay 1 "$@"
51 }
52
100ec144
keenan
7d ago
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.
56 assets=$(sed -n 's|.*artifacts/\([^"/][^"]*\)\".*|\1|p' "$FORMULA")
57 [ -n "$assets" ] || fail "$FORMULA has no artifact url lines"
58
ab3f4cba
keenan
7d ago
59 if [ -z "$tag" ]; then
8b2c3ebf
keenan
7d ago
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.
c7d7b6e4
Keenan Nemetz
3d ago
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)
ab3f4cba
keenan
7d ago
70 [ -n "$tag" ] || fail "no release tag found on $BASE_URL; pass one explicitly"
8b2c3ebf
keenan
7d ago
71 printf 'autobump: %s: using latest release %s\n' "$project" "$tag"
ab3f4cba
keenan
7d ago
72 fi
73
74 sums=$(mktemp)
75 trap 'rm -f "$sums" "${sums}.dl"' 0
76
77 curl_with_retry "$BASE_URL/releases/$tag/artifacts/SHA256SUMS" -o "$sums" ||
78 fail "cannot read SHA256SUMS for $tag"
79
100ec144
keenan
7d ago
80 verify() { # 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"
ab3f4cba
keenan
7d ago
86 got=$(sha256 "${sums}.dl")
100ec144
keenan
7d ago
87 [ "$got" = "$checksum" ] || fail "$name does not match SHA256SUMS (got $got, want $checksum)"
88 printf '%s\n' "$checksum"
ab3f4cba
keenan
7d ago
89 }
90
100ec144
keenan
7d ago
91 # Verify every asset and collect its checksum in formula order.
92 checksums=""
93 for asset in $assets; do
94 sha_line=$(verify "$asset")
95 checksums="$checksums $sha_line"
96 done
ab3f4cba
keenan
7d ago
97
98 version=${tag#v}
99
100ec144
keenan
7d ago
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).
102 python3 - "$FORMULA" "$version" $checksums <<'PY'
ab3f4cba
keenan
7d ago
103 import re, sys
100ec144
keenan
7d ago
104 path = sys.argv[1]
105 version = sys.argv[2]
106 checksums = sys.argv[3:]
ab3f4cba
keenan
7d ago
107 with open(path) as f:
108 lines = f.readlines()
109
110 out = []
111 sha_index = 0
112 for 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:
100ec144
keenan
7d ago
118 sha = checksums[sha_index]
ab3f4cba
keenan
7d ago
119 sha_index += 1
120 out.append(f'{ms.group(1)}sha256 "{sha}"\n')
121 else:
122 out.append(line)
123
100ec144
keenan
7d ago
124 if sha_index != len(checksums):
125 sys.exit(f'formula has {sha_index} sha256 lines but {len(checksums)} assets were verified')
126
ab3f4cba
keenan
7d ago
127 with open(path, 'w') as f:
128 f.writelines(out)
129 PY
130
8b2c3ebf
keenan
7d ago
131 printf 'autobump: wrote %s for %s (version %s)\n' "$FORMULA" "$tag" "$version"