-
Notifications
You must be signed in to change notification settings - Fork 4
/
bump_version.sh
executable file
·81 lines (73 loc) · 2.27 KB
/
bump_version.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
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
# Usage:
# bump_version.sh (show|major|minor|patch|finalize)
# bump_version.sh (build|prerelease) [token]
# Notes:
# - If you specify a token it will only be used if the current version is
# tokenless or if the provided token matches the token used in the current
# version.
set -o nounset
set -o errexit
set -o pipefail
VERSION_FILE=src/version.txt
README_FILE=README.md
function usage {
cat << HELP
Usage:
${0##*/} (show|major|minor|patch|finalize)
${0##*/} (build|prerelease) [token]
Notes:
- If you specify a token it will only be used if the current version is
tokenless or if the provided token matches the token used in the current
version.
HELP
exit 1
}
function update_version {
# Comment out periods so they are interpreted as periods and don't
# just match any character
old_version_regex=${1//\./\\\.}
echo Changing version from "$1" to "$2"
tmp_file=/tmp/version.$$
sed "s/$old_version_regex/$2/" $VERSION_FILE > $tmp_file
mv $tmp_file $VERSION_FILE
sed "s/$old_version_regex/$2/" $README_FILE > $tmp_file
mv $tmp_file $README_FILE
git add $VERSION_FILE $README_FILE
git commit --message "$3"
}
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
usage
else
old_version=$(sed -n "s/^__version__ = \"\(.*\)\"$/\1/p" $VERSION_FILE)
case $1 in
major | minor | patch)
if [ $# -ne 1 ]; then
usage
fi
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version'))")
update_version "$old_version" "$new_version" "Bump version from $old_version to $new_version"
;;
build | prerelease)
if [ $# -eq 2 ]; then
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version', token='$2'))")
else
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version'))")
fi
update_version "$old_version" "$new_version" "Bump version from $old_version to $new_version"
;;
finalize)
if [ $# -ne 1 ]; then
usage
fi
new_version=$(python -c "import semver; print(semver.finalize_version('$old_version'))")
update_version "$old_version" "$new_version" "Finalize version from $old_version to $new_version"
;;
show)
echo "$old_version"
;;
*)
usage
;;
esac
fi