Skip to content

Commit d9d1832

Browse files
committed
Add bump-snapshot mise task to automate version bumps
After a release, the snapshot version must be updated across ~57 files. This was previously a manual find-and-replace. The new `mise run bump-snapshot` task automates it by extracting the current version from the parent pom.xml and replacing it everywhere via git grep. Also makes set-version.sh and build-release.sh dynamic so they no longer hard-code the snapshot version.
1 parent 4e2d6b8 commit d9d1832

File tree

4 files changed

+113
-9
lines changed

4 files changed

+113
-9
lines changed

.mise/tasks/build-release.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#!/usr/bin/env bash
22

33
#MISE description="Build release package"
4-
#USAGE arg "<tag>" env="TAG" default="1.5.0-SNAPSHOT"
4+
#USAGE arg "[tag]" env="TAG"
55

66
set -euo pipefail
77

88
# shellcheck disable=SC2154 # is set by mise
9+
if [[ -z "${usage_tag:-}" ]]; then
10+
PARENT_POM="prometheus-metrics-parent/pom.xml"
11+
usage_tag=$(sed -n 's/.*<version>\(.*-SNAPSHOT\)<\/version>.*/\1/p' "$PARENT_POM" | head -1)
12+
if [[ -z "$usage_tag" ]]; then
13+
echo "ERROR: could not find SNAPSHOT version in $PARENT_POM" >&2
14+
exit 1
15+
fi
16+
fi
17+
918
VERSION=${usage_tag#v}
1019

1120
mise run set-version "$VERSION"

.mise/tasks/bump_snapshot.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
3+
# [MISE] description="Bump the snapshot version in all pom.xml files"
4+
# [MISE] alias="bump-snapshot"
5+
6+
"""
7+
Bump the SNAPSHOT version in all pom.xml files.
8+
9+
By default, increments the minor version (e.g. X.Y.0 -> X.(Y+1).0).
10+
An explicit version can be passed as argument:
11+
mise run bump-snapshot 2.0.0-SNAPSHOT
12+
"""
13+
14+
import re
15+
import sys
16+
from pathlib import Path
17+
18+
ROOT = Path(__file__).resolve().parents[2] # repo root
19+
PARENT_POM = ROOT / "prometheus-metrics-parent" / "pom.xml"
20+
21+
22+
def current_snapshot() -> str:
23+
"""Extract the current SNAPSHOT version from the parent pom.xml."""
24+
text = PARENT_POM.read_text(encoding="utf-8")
25+
m = re.search(
26+
r"<groupId>io\.prometheus</groupId>\s*"
27+
r"<artifactId>client_java_parent</artifactId>\s*"
28+
r"<version>(\S+)</version>",
29+
text,
30+
)
31+
if not m:
32+
sys.exit("Could not find version in " + str(PARENT_POM))
33+
version = m.group(1)
34+
if not version.endswith("-SNAPSHOT"):
35+
sys.exit(f"Current version '{version}' is not a SNAPSHOT version")
36+
return version
37+
38+
39+
def next_minor(version: str) -> str:
40+
"""Increment the minor version: 1.5.0-SNAPSHOT -> 1.6.0-SNAPSHOT."""
41+
base = version.removesuffix("-SNAPSHOT")
42+
parts = base.split(".")
43+
if len(parts) != 3:
44+
sys.exit(f"Expected three-part version, got '{base}'")
45+
parts[1] = str(int(parts[1]) + 1)
46+
parts[2] = "0"
47+
return ".".join(parts) + "-SNAPSHOT"
48+
49+
50+
def find_pom_files() -> list[Path]:
51+
"""Find all pom.xml files in the repository."""
52+
return sorted(ROOT.rglob("pom.xml"))
53+
54+
55+
def main() -> None:
56+
old_version = current_snapshot()
57+
58+
if len(sys.argv) > 1:
59+
new_version = sys.argv[1]
60+
if not new_version.endswith("-SNAPSHOT"):
61+
sys.exit(f"New version must end with -SNAPSHOT, got '{new_version}'")
62+
else:
63+
new_version = next_minor(old_version)
64+
65+
if old_version == new_version:
66+
sys.exit(f"Old and new version are the same: {old_version}")
67+
68+
print(f"Bumping {old_version} -> {new_version}")
69+
70+
updated_count = 0
71+
for pom in find_pom_files():
72+
content = pom.read_text(encoding="utf-8")
73+
updated = content.replace(old_version, new_version)
74+
if content != updated:
75+
pom.write_text(updated, encoding="utf-8")
76+
print(f" updated {pom.relative_to(ROOT)}")
77+
updated_count += 1
78+
79+
if updated_count == 0:
80+
sys.exit(f"No pom.xml files contain '{old_version}'")
81+
82+
print(f"\nDone. {updated_count} pom.xml file(s) updated to {new_version}.")
83+
84+
85+
if __name__ == "__main__":
86+
main()

.mise/tasks/set-version.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
set -euo pipefail
77

8-
# replace all occurrences '<version>1.5.0-SNAPSHOT</version>' with
9-
# '<version>$usage_version</version>' in all pom.xml files in the current directory and
10-
# subdirectories
8+
PARENT_POM="prometheus-metrics-parent/pom.xml"
9+
CURRENT_VERSION=$(sed -n 's/.*<version>\(.*-SNAPSHOT\)<\/version>.*/\1/p' "$PARENT_POM" | head -1)
10+
11+
if [[ -z "$CURRENT_VERSION" ]]; then
12+
echo "ERROR: could not find SNAPSHOT version in $PARENT_POM" >&2
13+
exit 1
14+
fi
1115

1216
# shellcheck disable=SC2154 # is set by mise
1317
find . -name 'pom.xml' -exec \
14-
sed -i "s/<version>1.5.0-SNAPSHOT<\/version>/<version>$usage_version<\/version>/g" {} +
18+
sed -i "s/<version>${CURRENT_VERSION}<\/version>/<version>$usage_version<\/version>/g" {} +

RELEASING.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ mise run update-benchmarks
1919

2020
## Major or minor release
2121

22-
After the release is created, do a text replace everywhere in the repository to update the
23-
snapshot version in the `pom.xml` files (and some other files) to the next version.
24-
For example, if the last release was `1.4.0`, the next snapshot version should be `1.5.0-SNAPSHOT`.
22+
After the release is created, bump the snapshot version across the
23+
repository:
2524

26-
Replace `1.4.0-SNAPSHOT` with `1.5.0-SNAPSHOT` in all following files.
25+
```shell
26+
# Auto-increment the minor version (e.g. 1.5.0-SNAPSHOT -> 1.6.0-SNAPSHOT)
27+
mise run bump-snapshot
28+
29+
# Or specify an explicit version (e.g. for a major bump)
30+
mise run bump-snapshot 2.0.0-SNAPSHOT
31+
```
2732

2833
## If the GPG key expired
2934

0 commit comments

Comments
 (0)