-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_version.sh
executable file
·130 lines (106 loc) · 4 KB
/
update_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
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
#!/bin/bash
VERSION=$(<VERSION)
DIFF_TYPE=''
DECREMENT=false
DRY_RUN=false
COMMIT=false
HELP_MESSAGE="Usage: ./update_version.sh [OPTIONS]
Update the project version number by one increment.
For the version number in the format \"major.minor.patch\",
the patch number will be incremented by default.
Supplying additional options to the script will change
the behaviour, either to set a specific version, update
the major or minor numbers, or to decrement instead.
This script modifies the version in the following files:
./source/modulo_component_interfaces/package.xml
./source/modulo_components/package.xml
./source/modulo_core/package.xml
./doxygen/doxygen.conf
Options:
--major Update the major version number.
--minor Update the minor version number.
-v, --version <x.y.z> Set a specific version number.
-d, --downgrade Decrement instead of increment.
-c, --commit Automatically commit the change
to git source control.
-n, --dry-run Echo the new version but do not
write changes to any files.
-h, --help Show this help message."
while [ "$#" -gt 0 ]; do
case "$1" in
-v | --version) NEW_VERSION=$2; DIFF_TYPE=fixed; shift 2;;
--major) DIFF_TYPE=major; shift 1;;
--minor) DIFF_TYPE=minor; shift 1;;
-d | --downgrade) DECREMENT=true; shift 1;;
-c | --commit) COMMIT=true; shift 1;;
-n | --dry-run) DRY_RUN=true; shift 1;;
-h|--help) echo "${HELP_MESSAGE}"; exit 0;;
-*) echo "Unknown option: $1" >&2; echo "${HELP_MESSAGE}"; exit 1;;
esac
done
if [ "${COMMIT}" == true ]; then
STAGED_CHANGES=$(git diff --cached)
if [ "${STAGED_CHANGES}" ]; then
echo "Staged changes detected! Resolve to a clean working state before using the --commit option."
exit 1
fi
fi
function update_field() {
local FIELD=$1
if [ "${DECREMENT}" == true ]; then
if [ "${FIELD}" -ne "0" ]; then
FIELD=$(( FIELD - 1 ))
fi
else
FIELD=$(( FIELD + 1 ))
fi
echo "${FIELD}"
}
function update_version() {
# break down the version number into its components
regex="([0-9]+).([0-9]+).([0-9]+)"
if [[ "$VERSION" =~ ${regex} ]]; then
MAJOR="${BASH_REMATCH[1]}"
MINOR="${BASH_REMATCH[2]}"
BUILD="${BASH_REMATCH[3]}"
fi
# check parameter to see which number to increment
if [[ "${DIFF_TYPE}" == "major" ]]; then
MAJOR=$(update_field "${MAJOR}")
elif [[ "${DIFF_TYPE}" == "minor" ]]; then
MINOR=$(update_field "${MINOR}")
else
BUILD=$(update_field "${BUILD}")
fi
echo "${MAJOR}.${MINOR}.${BUILD}"
}
if [[ "${DIFF_TYPE}" != "fixed" ]]; then
NEW_VERSION=$(update_version)
fi
echo "Updating version from ${VERSION} to ${NEW_VERSION}"
if [ "${DRY_RUN}" == true ]; then
echo "Dry run complete. Exiting without changing any files"
exit 0
fi
SED_STR_VERSION="s/${VERSION}/${NEW_VERSION}/g"
SED_STR_PACKAGE="s/<version>${VERSION}<\/version>/<version>${NEW_VERSION}<\/version>/g"
SED_STR_DOXYGEN="s/PROJECT_NUMBER = ${VERSION}/PROJECT_NUMBER = ${NEW_VERSION}/g"
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "${SED_STR_VERSION}" ./VERSION
sed -i '' "${SED_STR_PACKAGE}" ./source/modulo_component_interfaces/package.xml
sed -i '' "${SED_STR_PACKAGE}" ./source/modulo_components/package.xml
sed -i '' "${SED_STR_PACKAGE}" ./source/modulo_core/package.xml
sed -i '' "${SED_STR_DOXYGEN}" ./doxygen/doxygen.conf
else
sed -i "${SED_STR_VERSION}" ./VERSION
sed -i "${SED_STR_PACKAGE}" ./source/modulo_component_interfaces/package.xml
sed -i "${SED_STR_PACKAGE}" ./source/modulo_components/package.xml
sed -i "${SED_STR_PACKAGE}" ./source/modulo_core/package.xml
sed -i "${SED_STR_DOXYGEN}" ./doxygen/doxygen.conf
fi
if [ "${COMMIT}" == true ]; then
echo "Committing changes to source control"
git add VERSION ./source/modulo_component_interfaces/package.xml ./source/modulo_components/package.xml \
./source/modulo_core/package.xml ./doxygen/doxygen.conf
git commit -m "${VERSION} -> ${NEW_VERSION}"
fi