forked from 8go/matrix-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-1-version.sh
executable file
·151 lines (138 loc) · 4.18 KB
/
update-1-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
#!/usr/bin/env bash
# A version number is defined by: MAJOR.MINOR.PATCH
# where
# MAJOR version when you make incompatible API changes
# MINOR version when you add functionality in a backwards-compatible manner, and
# PATCH version when you make backwards-compatible bug fixes.
# See also Issue #109
#
# bumps version number of project
# change 2 lines (VERSION and VERSIONNR) in "matrix_commander/matrix_commander.py"
# changes 1 line in "setup.cfg", the PyPi set-up file
# creates 1-line `VERSION` file
#
# the script must take 1 argument: either `--mayor`, `--minor` or `--patch`
# NO default is set on purpose.
FN="matrix_commander/matrix_commander.py"
VERSION_FILE="VERSION"
UPDATE="invalid"
if ! [ -f "$FN" ]; then
FN="../$FN"
if ! [ -f "$FN" ]; then
echo -n "ERROR: $(basename -- "$FN") not found. "
echo "Neither in local nor in parent directory."
exit 1
fi
fi
if ! [ -f "$FN" ]; then
echo "ERROR: File \"$FN\" not found."
exit 1
fi
if [ "${1,,}" == "-ma" ] || [ "${1,,}" == "--mayor" ]; then
UPDATE="MAYOR"
fi
if [ "${1,,}" == "-m" ] || [ "${1,,}" == "--minor" ]; then
UPDATE="MINOR"
fi
if [ "${1,,}" == "-p" ] || [ "${1,,}" == "--patch" ]; then
UPDATE="PATCH"
fi
if [ "${UPDATE,,}" == "invalid" ]; then
echo "Error: No version type specified: specify either '--mayor', '--minor' or '--patch'."
exit 2
fi
echo "Doing a $UPDATE version increment."
PREFIX="VERSION = "
REGEX="^${PREFIX}\"20[0-9][0-9]-[0-9][0-9]-[0-9][0-9].*\""
COUNT=$(grep --count -e "$REGEX" $FN)
if [ "$COUNT" == "1" ]; then
# NEWVERSION="$PREFIX\"$(date +%Y-%m-%d-%H%M%S)\""
NEWVERSION="$PREFIX\"$(date +%Y-%m-%d)\""
sed -i "s/$REGEX/$NEWVERSION/" $FN
RETURN=$?
if [ "$RETURN" == "0" ]; then
echo "SUCCESS: Modified file $FN by setting version to $NEWVERSION."
else
echo "ERROR: could not change version to $NEWVERSION in $FN."
exit 1
fi
else
echo "Error while searching for $REGEX"
grep -e "$PREFIX" $FN
if [ "$COUNT" == "1" ]; then
echo "ERROR: Version not found, expected 1 occurance."
else
echo "ERROR: Version found $COUNT times, expected 1 occurance."
fi
exit 1
fi
PREFIX="VERSIONNR = "
REGEX="^${PREFIX}\"[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\""
COUNT=$(grep --count -e "$REGEX" $FN)
if [ "$COUNT" == "1" ]; then
NR=$(grep -e "$REGEX" $FN | cut -d'"' -f2)
A=$(echo $NR | cut -d'.' -f1)
M=$(echo $NR | cut -d'.' -f2)
Z=$(echo $NR | cut -d'.' -f3)
if [ "$UPDATE" == "MAYOR" ]; then
A=$((A + 1))
M="0"
Z="0"
elif [ "$UPDATE" == "MINOR" ]; then
M=$((M + 1))
Z="0"
else
Z=$((Z + 1))
fi
NEWVERSIONNR="${A}.${M}.${Z}"
echo $NEWVERSIONNR >"$VERSION_FILE"
NEWVERSION="$PREFIX\"${A}.${M}.${Z}\""
sed -i "s/$REGEX/$NEWVERSION/" $FN
RETURN=$?
if [ "$RETURN" == "0" ]; then
echo "SUCCESS: Modified file $FN by setting version to $NEWVERSION."
else
echo "ERROR: could not change version to $NEWVERSION in $FN."
exit 1
fi
else
echo "Error while searching for $REGEX"
grep -e "$PREFIX" $FN
if [ "$COUNT" == "1" ]; then
echo "ERROR: Version not found in $FN, expected 1 occurance."
else
echo "ERROR: Version found $COUNT times in $FN, expected 1 occurance."
fi
exit 1
fi
# update PyPi setup file
# version = 2.1.0
FN="setup.cfg"
if ! [ -f "$FN" ]; then
echo "ERROR: File \"$FN\" not found."
exit 1
fi
PREFIX="version = "
REGEX="^${PREFIX}[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
COUNT=$(grep --count -e "$REGEX" $FN)
if [ "$COUNT" == "1" ]; then
NEWVERSION="${PREFIX}${NEWVERSIONNR}"
sed -i "s/$REGEX/$NEWVERSION/" $FN
RETURN=$?
if [ "$RETURN" == "0" ]; then
echo "SUCCESS: Modified file $FN by setting version to $NEWVERSION."
else
echo "ERROR: could not change version to $NEWVERSION in $FN."
exit 1
fi
else
echo "Error while searching for $REGEX"
grep -e "$PREFIX" $FN
if [ "$COUNT" == "1" ]; then
echo "ERROR: Version not found in $FN, expected 1 occurance."
else
echo "ERROR: Version found $COUNT times in $FN, expected 1 occurance."
fi
exit 1
fi
exit 0