-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_version.sh
More file actions
executable file
·155 lines (125 loc) · 3.56 KB
/
update_version.sh
File metadata and controls
executable file
·155 lines (125 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env bash
set -euo pipefail
readonly POM_FILE="pom.xml"
readonly VELOCITY_FILE="src/main/java/nl/hauntedmc/dataregistry/platform/velocity/VelocityDataRegistry.java"
die() {
echo "Error: $*" >&2
exit 1
}
usage() {
cat >&2 <<'USAGE'
Usage: ./update_version.sh <major|minor|patch>
Bumps the Maven project version in pom.xml and keeps release metadata in sync.
Then creates a local commit and a local git tag vX.Y.Z.
USAGE
}
require_file() {
local path="$1"
[[ -f "$path" ]] || die "${path} not found."
}
require_clean_worktree() {
[[ -z "$(git status --porcelain)" ]] || die "Working tree is not clean. Commit or stash changes first."
}
resolve_maven_version() {
local version
version="$(
mvn -q -ntp -DforceStdout help:evaluate -Dexpression=project.version \
| awk '/^[0-9]+\.[0-9]+\.[0-9]+$/ { print; exit }'
)"
[[ -n "$version" ]] || die "Unable to resolve a release semantic version from Maven."
echo "$version"
}
bump_semver() {
local semver="$1"
local bump_type="$2"
local major minor patch
[[ "$semver" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || die "Current version must be semantic (X.Y.Z), got '${semver}'."
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
case "$bump_type" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
*)
usage
exit 1
;;
esac
echo "${major}.${minor}.${patch}"
}
update_velocity_plugin_annotation() {
local new_version="$1"
local tmp_file
tmp_file="$(mktemp)"
awk -v v="$new_version" '
BEGIN { replaced = 0 }
{
if (!replaced && $0 ~ /version = "[^"]+"/) {
sub(/version = "[^"]+"/, "version = \"" v "\"")
replaced = 1
}
print
}
END {
if (!replaced) {
exit 2
}
}
' "$VELOCITY_FILE" > "$tmp_file" || {
rm -f "$tmp_file"
die "Could not update Velocity @Plugin version in ${VELOCITY_FILE}."
}
mv "$tmp_file" "$VELOCITY_FILE"
}
if [[ $# -eq 1 && ( "$1" == "--help" || "$1" == "-h" ) ]]; then
usage
exit 0
fi
if [[ $# -ne 1 ]]; then
usage
exit 1
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
die "This script must be run inside a git repository."
fi
command -v mvn >/dev/null 2>&1 || die "Maven (mvn) is required."
repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"
require_file "$POM_FILE"
require_file "$VELOCITY_FILE"
require_clean_worktree
bump_type="$1"
[[ "$bump_type" == "major" || "$bump_type" == "minor" || "$bump_type" == "patch" ]] || {
usage
exit 1
}
current_version="$(resolve_maven_version)"
new_version="$(bump_semver "$current_version" "$bump_type")"
new_tag="v${new_version}"
if git rev-parse -q --verify "refs/tags/${new_tag}" >/dev/null 2>&1; then
die "Tag ${new_tag} already exists."
fi
echo "Current version: ${current_version}"
echo "Bumping to: ${new_version}"
# Use Maven's versions plugin so pom.xml remains the single source of truth.
mvn -B -ntp versions:set -DnewVersion="${new_version}" -DgenerateBackupPoms=false -DprocessAllModules=true
resolved_after_bump="$(resolve_maven_version)"
[[ "$resolved_after_bump" == "$new_version" ]] || {
die "Maven version after bump is '${resolved_after_bump}', expected '${new_version}'."
}
update_velocity_plugin_annotation "$new_version"
git add "$POM_FILE" "$VELOCITY_FILE"
git commit -m "Bump version to ${new_tag} for release"
git tag "$new_tag"
echo "Version updated locally."
echo "Next step: git push && git push origin ${new_tag}"