-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrelease.sh
More file actions
executable file
·284 lines (245 loc) · 6.27 KB
/
release.sh
File metadata and controls
executable file
·284 lines (245 loc) · 6.27 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: release.sh <version> [--push] [--dry-run]
Creates a release by:
1. Validating <version> as semantic versioning (SemVer 2.0.0)
2. Ensuring <version> is greater than the current Cargo.toml package version
3. Ensuring tag v<version> does not already exist
4. Updating Cargo.toml [package].version
5. Committing Cargo.toml and creating tag v<version>
6. Optionally pushing the tag when --push is provided
7. With --dry-run, print planned actions without changing git state/files
USAGE
}
err() {
echo "Error: $*" >&2
exit 1
}
is_valid_semver() {
local version="$1"
local semver_regex='^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*)(\.(0|[1-9][0-9]*|[0-9A-Za-z-]*[A-Za-z-][0-9A-Za-z-]*))*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$'
printf '%s\n' "$version" | grep -Eq "$semver_regex"
}
split_semver() {
local version="$1"
local core="$version"
local pre=""
local major minor patch
if [[ "$core" == *+* ]]; then
core="${core%%+*}"
fi
if [[ "$core" == *-* ]]; then
pre="${core#*-}"
core="${core%%-*}"
fi
IFS='.' read -r major minor patch <<<"$core"
printf '%s\t%s\t%s\t%s\n' "$major" "$minor" "$patch" "$pre"
}
compare_prerelease() {
local left="$1"
local right="$2"
if [[ -z "$left" && -z "$right" ]]; then
echo 0
return
fi
if [[ -z "$left" ]]; then
echo 1
return
fi
if [[ -z "$right" ]]; then
echo -1
return
fi
local -a lparts=()
local -a rparts=()
IFS='.' read -r -a lparts <<<"$left"
IFS='.' read -r -a rparts <<<"$right"
local i
local lnum rnum
for ((i = 0; i < ${#lparts[@]} || i < ${#rparts[@]}; i++)); do
if ((i >= ${#lparts[@]})); then
echo -1
return
fi
if ((i >= ${#rparts[@]})); then
echo 1
return
fi
if [[ "${lparts[$i]}" == "${rparts[$i]}" ]]; then
continue
fi
if [[ "${lparts[$i]}" =~ ^(0|[1-9][0-9]*)$ ]]; then
lnum=1
else
lnum=0
fi
if [[ "${rparts[$i]}" =~ ^(0|[1-9][0-9]*)$ ]]; then
rnum=1
else
rnum=0
fi
if ((lnum == 1 && rnum == 1)); then
if ((10#${lparts[$i]} > 10#${rparts[$i]})); then
echo 1
else
echo -1
fi
return
fi
if ((lnum == 1 && rnum == 0)); then
echo -1
return
fi
if ((lnum == 0 && rnum == 1)); then
echo 1
return
fi
if [[ "${lparts[$i]}" > "${rparts[$i]}" ]]; then
echo 1
else
echo -1
fi
return
done
echo 0
}
compare_semver() {
local left="$1"
local right="$2"
local lmaj lmin lpat lpre
local rmaj rmin rpat rpre
IFS=$'\t' read -r lmaj lmin lpat lpre <<<"$(split_semver "$left")"
IFS=$'\t' read -r rmaj rmin rpat rpre <<<"$(split_semver "$right")"
if ((10#$lmaj > 10#$rmaj)); then
echo 1
return
fi
if ((10#$lmaj < 10#$rmaj)); then
echo -1
return
fi
if ((10#$lmin > 10#$rmin)); then
echo 1
return
fi
if ((10#$lmin < 10#$rmin)); then
echo -1
return
fi
if ((10#$lpat > 10#$rpat)); then
echo 1
return
fi
if ((10#$lpat < 10#$rpat)); then
echo -1
return
fi
compare_prerelease "$lpre" "$rpre"
}
push_tag=false
dry_run=false
version=""
while [[ $# -gt 0 ]]; do
case "$1" in
--push)
push_tag=true
shift
;;
--dry-run)
dry_run=true
shift
;;
-h|--help)
usage
exit 0
;;
-*)
err "Unknown option: $1"
;;
*)
if [[ -n "$version" ]]; then
err "Provide exactly one version argument."
fi
version="$1"
shift
;;
esac
done
[[ -n "$version" ]] || { usage >&2; exit 1; }
[[ -f "Cargo.toml" ]] || err "Cargo.toml not found in current directory."
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || err "Current directory is not a git repository."
is_valid_semver "$version" || err "Version '$version' is not valid SemVer 2.0.0."
current_version="$(
awk '
/^\[package\]/ { in_package=1; next }
in_package && /^\[/ { in_package=0 }
in_package && /^[[:space:]]*version[[:space:]]*=/ {
match($0, /"[^"]+"/)
if (RSTART > 0) {
print substr($0, RSTART + 1, RLENGTH - 2)
exit
}
}
' Cargo.toml
)"
[[ -n "$current_version" ]] || err "Could not find [package].version in Cargo.toml."
is_valid_semver "$current_version" || err "Current Cargo.toml version '$current_version' is not valid SemVer."
cmp="$(compare_semver "$version" "$current_version")"
if [[ "$cmp" != "1" ]]; then
err "Version '$version' must be greater than current version '$current_version'."
fi
tag="v${version}"
if git rev-parse -q --verify "refs/tags/${tag}" >/dev/null 2>&1; then
err "Tag '${tag}' already exists locally."
fi
if git remote get-url origin >/dev/null 2>&1; then
if git ls-remote --exit-code --tags origin "refs/tags/${tag}" >/dev/null 2>&1; then
err "Tag '${tag}' already exists on remote 'origin'."
fi
fi
tmp_file="$(mktemp)"
cleanup() {
rm -f "$tmp_file"
}
trap cleanup EXIT
awk -v new_version="$version" '
BEGIN { in_package=0; updated=0 }
/^\[package\]/ { in_package=1; print; next }
in_package && /^\[/ { in_package=0 }
in_package && !updated && /^[[:space:]]*version[[:space:]]*=/ {
print "version = \"" new_version "\""
updated=1
next
}
{ print }
END {
if (!updated) {
exit 2
}
}
' Cargo.toml > "$tmp_file" || err "Failed to update Cargo.toml."
if [[ "$dry_run" == true ]]; then
echo "Dry run successful:"
echo "- Current version: ${current_version}"
echo "- New version: ${version}"
echo "- Tag to create: ${tag}"
echo "- Would update Cargo.toml [package].version to ${version}"
echo "- Would commit: Release ${tag}"
echo "- Would create tag: ${tag}"
if [[ "$push_tag" == true ]]; then
git remote get-url origin >/dev/null 2>&1 || err "No 'origin' remote configured, cannot push tag."
echo "- Would push tag to origin: ${tag}"
fi
exit 0
fi
mv "$tmp_file" Cargo.toml
git add Cargo.toml
git commit -m "Release ${tag}" -- Cargo.toml
git tag "${tag}"
if [[ "$push_tag" == true ]]; then
git remote get-url origin >/dev/null 2>&1 || err "No 'origin' remote configured, cannot push tag."
git push origin "${tag}"
fi
echo "Released ${tag} (updated Cargo.toml from ${current_version} to ${version})."