-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
update_version.sh
187 lines (142 loc) · 5.63 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
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
#!/usr/bin/env bash
#
# This file must be ran from the main repository folder. It updates the software version number
# indicated at "filePathToUpdate" and "versionFilePath". The versions at these two files must to be
# synchronized for a correct version update.
#
# You can also update the version manually, but you must to update both files:
# "$AUTO_VERSIONING_ROOT_FOLDER_NAME/VERSION.txt" and "./project_folder/project_file.txt" using
# the same version number.
#
# Program usage: update_version [major | minor | patch | build]
# Example: ./update_version build
#
#
# This script is run straight from the project's git root folder, as the current working directory.
# printf "Running the update_version.sh script...\n"
# Reliable way for a bash script to get the full path to itself?
# http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
pushd `dirname $0` > /dev/null
SCRIPT_FOLDER_PATH=`pwd`
popd > /dev/null
# Whether we are dealing with a git-submodule or not, this get the correct git file path for the
# project root folder if run on it directory, or for the sub-module folder if run on its directory.
cd $SCRIPT_FOLDER_PATH
cd ..
GIT_DIR_="$(git rev-parse --git-dir)"
configuration_file=$1
# Initialize the configuration file provided
if ! $SCRIPT_FOLDER_PATH/scripts/utilities.sh $configuration_file
then
exit 1
fi
# Get the submodule (if any) or the main's repository root directory
PROJECT_ROOT_DIRECTORY="$(git rev-parse --show-toplevel)"
# Read the configurations file.
configurationFilePath="$PROJECT_ROOT_DIRECTORY/$configuration_file"
gitHooksConfiguration=$(cat $configurationFilePath)
# $versionFilePath example: $PROJECT_ROOT_DIRECTORY/GALILEO_SMA_VERSION.txt
versionFilePath="$PROJECT_ROOT_DIRECTORY/$(echo $gitHooksConfiguration | cut -d',' -f 1 | tr -d ' ')"
# $filePathToUpdate example: $PROJECT_ROOT_DIRECTORY/scripting/galileo.sma
filePathToUpdate="$PROJECT_ROOT_DIRECTORY/$(echo $gitHooksConfiguration | cut -d',' -f 2 | tr -d ' ')"
# Load the bit wise options to customize the program behavior.
bitWiseOptions=$(echo $gitHooksConfiguration | cut -d',' -f 5 | tr -d ' ')
# Get the current version from the dedicated versioning file.
if [ -f $versionFilePath ]
then
currentVersion=$(cat $versionFilePath)
else
printf "Creating the configuration file '$versionFilePath'...\n"
printf "1.0.0-1" > $versionFilePath
exit 1
fi
originalVersion=$currentVersion
component=$2
# 'cut' Print selected parts of lines from each FILE to standard output
#
# '-d' use another delimiter instead of TAB for field delimiter.
# '-f' select only these fields.
#
major=$(echo $currentVersion | cut -d'.' -f 1)
minor=$(echo $currentVersion | cut -d'.' -f 2)
patch=$(echo $currentVersion | cut -d'.' -f 3 | cut -d'-' -f 1)
build=$(echo $currentVersion | cut -d'-' -f 2)
if [ -z "${major}" ] || [ -z "${minor}" ] || [ -z "${patch}" ] || [ -z "${build}" ]
then
printf "VAR <$major>.<$minor>.<$patch>-<$build> is bad set or set to the empty string\n"
exit 1
fi
case "$component" in
major )
major=$(expr $major + 1)
minor=0
patch=0
;;
minor )
minor=$(expr $minor + 1)
patch=0
;;
patch )
patch=$(expr $patch + 1)
;;
build )
build=$(expr $build + 1)
;;
* )
printf "Error - argument must be 'major', 'minor', 'patch' or 'build'\n"
printf "Usage: update_version [major | minor | patch | build]\n"
printf "\n"
printf "Semantic Versioning 2.0.0\n"
printf "\n"
printf "Given a version number MAJOR.MINOR.PATCH, increment the:\n"
printf "\n"
printf "MAJOR version when you make incompatible API changes,\n"
printf "MINOR version when you add functionality in a backwards-compatible manner, and\n"
printf "PATCH version when you make backwards-compatible bug fixes.\n"
printf "Additional labels for pre-release and build metadata are available as extensions to\n"
printf "the MAJOR.MINOR.PATCH format.\n"
printf "\n"
exit 1
;;
esac
currentVersion=$major.$minor.$patch-$build
# To prints a error message when it does not find the version number on the file.
#
# 'F' affects how PATTERN is interpreted (fixed string instead of a regex).
# 'q' shhhhh... minimal printing.
#
if ! grep -Fq "v$originalVersion" "$filePathToUpdate"
then
printf "Error! Could not find v$originalVersion and update the file '$filePathToUpdate'.\n"
printf "The current version number on this file must be v$originalVersion!\n"
printf "Or fix the file '$versionFilePath' to the correct value.\n"
exit 1
fi
if sed -i -- "s/v$originalVersion/v$currentVersion/g" $filePathToUpdate
then
printf "Replacing the version v$originalVersion -> v$currentVersion in '$filePathToUpdate'\n"
# Replace the file with the $versionFilePath with the $currentVersion.
echo $currentVersion > $versionFilePath
else
printf "ERROR! Could not replace the version v$originalVersion -> v$currentVersion in '$filePathToUpdate'\n"
exit 1
fi
# Fix line endings
if (( ($bitWiseOptions & 1) != 0 ))
then
if awk '/\r$/{exit 0;} 1{exit 1;}' $filePathToUpdate
then
# printf "( CRLF ) The file $filePathToUpdate is uses CRLF.\n"
dos2unix $filePathToUpdate
else
# printf "( LF ) The file $filePathToUpdate is uses LF.\n"
unix2dos $filePathToUpdate
fi
fi
# To add the recent updated files to the commit
# printf "Staging '$versionFilePath'...\n"
# printf "Staging '$filePathToUpdate'...\n"
git add $versionFilePath
git add $filePathToUpdate
# Exits the program using a successful exit status code.
exit 0