-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathset-version.sh
More file actions
executable file
·175 lines (156 loc) · 6.86 KB
/
Copy pathset-version.sh
File metadata and controls
executable file
·175 lines (156 loc) · 6.86 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env bash
set -euo pipefail
# This script has a regular and a `--release` mode.
#
# In regular mode this script will:
# - update `versionNumber` in `/build.gradle.kts`
# - in `CHANGELOG.md`, if it doesn't exist, add a next release heading
# - on confirmation, commit the changes
# - suggest a git command to push the changes
#
# In release mode:
# - ask to confirm `versionNumber` in `/build.gradle.kts`
# - in `BoxStore.java` update `JNI_VERSION` to match `VERSION`
# - in `README.md` update version numbers in known locations
# - in `CHANGELOG.md` change the next version header to the new version and date
# - on confirmation, commit the changes, create a release tag
# - suggest git commands to push the changes
# Parse optional --release flag
releaseFlag=false
for arg in "$@"; do
case "$arg" in
--release) releaseFlag=true ;;
esac
done
# macOS includes BSD versions of sed and grep, which have different options and syntax than the
# expected GNU versions. So require users of this script to install gsed and ggrep.
if [[ "$OSTYPE" == "darwin"* ]]; then
if ! command -v gsed &>/dev/null; then
echo "Error: gsed is required but not installed. Install it, for example using 'brew install gnu-sed'."
exit 1
fi
if ! command -v ggrep &>/dev/null; then
echo "Error: ggrep is required but not installed. Install it, for example using 'brew install grep'."
exit 1
fi
sed="gsed"
grep="ggrep"
else
sed="sed"
grep="grep"
fi
scriptDir="$(dirname "$0")"
propVersionNumber="versionNumber"
buildScriptFile="../build.gradle.kts"
buildScriptPath="$scriptDir/$buildScriptFile"
boxStorePath="$scriptDir/../objectbox-java/src/main/java/io/objectbox/BoxStore.java"
readmePath="$scriptDir/../README.md"
changelogPath="$scriptDir/../CHANGELOG.md"
nextReleaseHeading="## Next release"
# Extract the value of `versionNumber` in build.gradle.kts and store it in the versionCurrent variable
buildScriptPath="$(dirname "$0")/$buildScriptFile"
# Regex matches 'versionNumber = "..."', \K to only capture the version string inside the quotes
versionCurrent=$($grep --only-matching --perl-regexp "$propVersionNumber"' = "\K[^"]+' "$buildScriptPath" || true)
if [[ -z "$versionCurrent" ]]; then
echo "Error: could not find '$propVersionNumber' in '$buildScriptFile'"
exit 1
fi
echo ""
echo "Maven artifacts version ($propVersionNumber in $buildScriptFile)"
echo "Current: $versionCurrent"
if $releaseFlag; then
# Release: Confirm the current version
read -r -p "Press enter to confirm the current version, or enter a custom one: " versionInput
if [[ -n "$versionInput" ]]; then
versionNew="$versionInput"
else
versionNew="$versionCurrent"
fi
else
# Suggest a next version
# Increment the last number in the version string (like 5.4.2 -> 5.4.3, 5.4.2-preview1 -> 5.4.2-preview2)
# Regex: captures the trailing digits and replaces them with a value increased by 1
[[ "$versionCurrent" =~ ^(.*[^0-9])([0-9]+)$ ]] || { echo "Error: $propVersionNumber '$versionCurrent' does not end with a number"; exit 1; }
versionSuggested="${BASH_REMATCH[1]}$(( BASH_REMATCH[2] + 1 ))"
echo "Suggested: $versionSuggested"
read -r -p "Press enter to use the suggested version, or enter a custom one: " versionInput
if [[ -n "$versionInput" ]]; then
versionNew="$versionInput"
else
versionNew="$versionSuggested"
fi
fi
echo "Version will be $versionNew"
# Change the value of `versionNumber` in build.gradle.kts to the value of versionNew
$sed --in-place "s/$propVersionNumber = \"$versionCurrent\"/$propVersionNumber = \"$versionNew\"/" "$buildScriptPath"
if $releaseFlag; then
# In BoxStore.java set JNI_VERSION to the value of VERSION (not using versionNew!) and print the used value
# Regex matches 'String VERSION = "...", \K to only capture the version string inside the quotes
versionJni=$($grep --only-matching --perl-regexp 'String VERSION = "\K[^"]+' "$boxStorePath" || true)
if [[ -z "$versionJni" ]]; then
echo "Error: could not find VERSION in BoxStore.java"
exit 1
fi
echo "Release: setting BoxStore.JNI_VERSION to $versionJni"
$sed --in-place "s/String JNI_VERSION = \".*\"/String JNI_VERSION = \"$versionJni\"/" "$boxStorePath"
# Change version strings in README.md to versionNew
echo "Release: updating README.md version strings"
$sed --in-place "s/objectbox = \".*\"/objectbox = \"$versionNew\"/g" "$readmePath"
$sed --in-place "s/id(\"io.objectbox\") version \".*\"/id(\"io.objectbox\") version \"$versionNew\"/g" "$readmePath"
$sed --in-place "s/val objectboxVersion by extra(\".*\")/val objectboxVersion by extra(\"$versionNew\")/g" "$readmePath"
$sed --in-place "s/ext.objectboxVersion = \".*\"/ext.objectboxVersion = \"$versionNew\"/g" "$readmePath"
# Change header "Next release" in CHANGELOG.md to "versionNew - YYYY-MM-DD"
echo "Release: updating CHANGELOG.md heading"
today=$(date +"%Y-%m-%d")
if $grep --quiet "^$nextReleaseHeading" "$changelogPath"; then
$sed --in-place "s/^$nextReleaseHeading/## $versionNew - $today/" "$changelogPath"
else
echo "⚠️ '$nextReleaseHeading' heading not found in CHANGELOG.md, check it contains changes for this release!"
fi
else
# In CHANGELOG.md, if the first secondary heading is not "## Next release", add it
firstHeading=$($grep --max-count=1 "^## " "$changelogPath" || true)
if [[ "$firstHeading" != "$nextReleaseHeading" ]]; then
echo "Adding '$nextReleaseHeading' to CHANGELOG.md"
$sed --in-place "0,/^## /{s/^## /$nextReleaseHeading\n\n## /}" "$changelogPath"
fi
fi
# After confirmation commit any changes and for a release create a tag
# Example for a release:
# git commit --all --message="Prepare release 5.4.0"
# git tag V5.4.0
# For a regular run:
# git commit --all --message="Publishing: increase version 5.4.0 -> 5.4.1"
if $releaseFlag; then
commitMessage="Prepare release $versionNew"
else
commitMessage="Publishing: increase version $versionCurrent -> $versionNew"
fi
tagRelease="V$versionNew"
echo ""
echo "About to run git commands:"
echo " git commit --all --message=\"$commitMessage\""
if $releaseFlag; then
echo " git tag $tagRelease"
fi
read -r -p "Reviewed changes? Proceed? [y/N] " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "Aborted."
exit 1
fi
git commit --all --message="$commitMessage"
if $releaseFlag; then
git tag "$tagRelease"
fi
# Print suggested git commands to push the changes
# Note: add `--push-option=ci.skip` to avoid running CI
# Examples:
# git push origin publish --push-option=ci.skip
# git push origin V5.4.0 --push-option=ci.skip
currentBranch=$(git rev-parse --abbrev-ref HEAD)
echo ""
echo "Suggested commands to push these changes:"
echo " git push origin $currentBranch --push-option=ci.skip"
if $releaseFlag; then
echo " git push origin $tagRelease --push-option=ci.skip"
fi