-
Notifications
You must be signed in to change notification settings - Fork 16
/
valid-semver-bump.sh
executable file
·71 lines (58 loc) · 1.38 KB
/
valid-semver-bump.sh
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
#!/bin/sh
# This script tests whether a given version bump is valid.
die() {
echo "$*" >&2
exit 1
}
succeed() {
echo "$*" >&2
exit 0
}
major() {
major=${1%%.*}
test -n "$major" || die "Invalid SemVer: $1"
echo $major
}
minor() {
tmp=${1#*.}
minor=${tmp%%.*}
test -n "$minor" || die "Invalid SemVer: $1"
echo $minor
}
patch() {
patch=${1##*.}
test -n "$patch" || die "Invalid SemVer: $1"
echo $patch
}
SNAPSHOT="${1%-SNAPSHOT}"
RELEASE="$2"
test -n "$RELEASE" -a -n "$SNAPSHOT" ||
die "Usage: valid-semver-bump.sh previous-snapshot-version new-release-version"
test "$RELEASE" = "$SNAPSHOT" &&
succeed "Detected DEFAULT version bump"
NEW_MAJOR=$(major $RELEASE)
NEW_MINOR=$(minor $RELEASE)
NEW_PATCH=$(patch $RELEASE)
OLD_MAJOR=$(major $SNAPSHOT)
OLD_MINOR=$(minor $SNAPSHOT)
OLD_PATCH=$(patch $SNAPSHOT)
# check for MINOR version bump
# e.g. 1.0.1-SNAPSHOT -> 1.1.0
if [ "$OLD_PATCH" -gt 0 ]
then
test "$NEW_MAJOR" -eq "$OLD_MAJOR" \
-a "$NEW_MINOR" -eq "$((OLD_MINOR+1))" \
-a "$NEW_PATCH" -eq 0 &&
succeed "Detected MINOR version bump"
fi
# check for MAJOR version bump
# e.g. 1.1.0-SNAPSHOT -> 2.0.0
# e.g. 1.0.1-SNAPSHOT -> 2.0.0
if [ "$OLD_PATCH" -gt 0 -o "$OLD_MINOR" -gt 0 ]
then
test "$NEW_MAJOR" -eq "$((OLD_MAJOR+1))" \
-a "$NEW_MINOR" -eq 0 \
-a "$NEW_PATCH" -eq 0 &&
succeed "Detected MAJOR version bump"
fi
die "Invalid version bump: $SNAPSHOT -> $RELEASE"