-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump-version.sh
executable file
·70 lines (60 loc) · 1.59 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
#!/usr/bin/env bash -euo pipefail
#default values
version_file=./version
version_upgrade=patch
minor=false
patch=true
write=false
#usage explanation
usage="$(basename "$0") [-f filepath] [-h] [-v version] [-w] -- this script returns the increased semversion stored in a file
where:
-f path to version file containing a single semversion default: $version_file
-h show this help text
-v version to bump default: $version_upgrade
-w overwrite the the original version file with the bumped version"
# reading flags
while getopts ':f:hv:w' option; do
case "$option" in
f) version_file="$OPTARG"
;;
h) echo "$usage"
exit 0
;;
v) version_upgrade="$OPTARG"
;;
w) write=true
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
#reading version from file
version=$(cat $version_file)
#parsing seperate parts of semversion
major=$(echo $version | awk -F'.' '{print $1}')
minor=$(echo $version | awk -F'.' '{print $2}')
patch=$(echo $version | awk -F'.' '{print $3}')
#increasing version according to flags
case "$version_upgrade" in
major) ((major++))
minor=0
patch=0
;;
minor) ((minor++))
patch=0
;;
patch) ((patch++))
;;
esac
# write new version to file, only if write flag is set
if [ "$write" = "true" ]; then
echo $major.$minor.$patch > version
fi
# output bumped version
echo $major.$minor.$patch