-
Notifications
You must be signed in to change notification settings - Fork 3
/
version-private
executable file
·213 lines (183 loc) · 4.27 KB
/
version-private
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# Initialize a string with dashes.
printf -v fill "%80s" ""
fill="${fill// /-}"
# Function for printing headers.
printfmt() { printf '%s%0.'$(( 80 - ${#1} - ${#2} ))'s%s\n' "$1" "$fill" "$2"; }
# Common header formatting.
header() {
echo ""; printfmt "+" "[ $1 ]"
}
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd "$@" > /dev/null
}
# Function to display error message, usage, and exit with code 1.
die() { echo -e "$*\n"; usage; exit 1; } >&2
# Will die if file does not exist.
assertFile() {
local file="$1"
shift 1
if [[ ! -f "${file}" ]]; then
if [[ "$#" -ne 0 ]]; then
die "$@"
else
die "Missing file: ${file}"
fi
else
echo " [OK] File: ${file}"
fi
}
# Function to test for element in array.
containsElement() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
file="project.version"
com="get"
val=""
push=true
usage() {
echo "usage: version [get|set <version>] [--file=<filename>] [--help] [--no-push]"
echo ""
echo " Commands:"
echo " get displays the project version"
echo " set <version> sets the project version"
echo ""
echo " Options:"
echo " --file=<filename> specify version file, default=project.version"
echo " --help display usage"
echo " --no-push suppress push at each phase"
}
verbose() {
if $verbose; then
echo "$@"
fi
}
assertExitCode() {
if [ $? -ne 0 ]; then
echo " [!!] Aborting on non-zero exit code."
exit $?
fi
}
displayVerify() {
printf " %-16s %-29s %-29s\n" "$1" "$2" "$3"
}
apply() {
pushd "."
if [[ "${com}" == "get" ]]; then
version get --file="${file}"
elif [[ "${com}" == "set" ]]; then
if $push; then
version set "${val}" --file="${file}"
else
version set "${val}" --file="${file}" --no-push
fi
fi
popd
}
if [[ "$1" == "--help" ]]; then
usage
exit 0
fi
if [[ "$#" -eq 0 || "$1" == "get" ]]; then
com="get"
shift 1
elif [[ "$1" == "set" ]]; then
com="set"
val="$2"
shift 2
else
die "Unknown command: [$1]"
fi
while [[ $# > 0 ]]; do
key="$1"
case $key in
--file=*)
file="${key#*=}"
shift 1
;;
--help)
usage
exit 0
;;
--no-push)
push=false
shift 1
;;
*)
die "Unknown command: [${key}]"
esac
done
if [[ "${com}" == "get" ]]; then
if [[ -f "${file}" ]]; then
fileContents=`cat "${file}"`
else
fileContents="0.0.0"
fi
displayVerify "[--] Tag:" `git describe --tags`
displayVerify "[--] File:" "${fileContents}"
elif [[ "${com}" == "set" ]]; then
header "Verification"
if [[ -f "${file}" ]]; then
fileContents=`cat "${file}"`
else
fileContents="0.0.0"
fi
echo ""
displayVerify "" "Old" "New"
displayVerify "" "-----------------------------" "-----------------------------"
displayVerify "[--] Tag:" `git describe --tags` "${val}"
displayVerify "[--] File:" "${fileContents}" "${val}"
echo ""
echo " [--] File:"
echo " git add ${file}"
echo " git commit -m \"Bump version to ${val}\""
$push && echo " git push"
echo ""
echo " [--] Tag Version:"
echo " git tag -a ${val} -m \"version ${val}\""
$push && echo " git push private refs/tags/${val}"
echo ""
read -p " [??] Does this look right to you [y/N]? " -n 1 -r
echo ""
case $REPLY in
y)
echo " [OK] Process validated by user"
;;
*)
echo " [!!] Process halted by user"
exit 1
esac
header "Write File"
echo -n "${val}" > "${file}"
result=`cat "${file}"`
if [[ ! "${result}" == "${val}" ]]; then
die " [!!] Unable to write [${val}] to [${file}]"
else
echo " [OK] [${result}] saved to [${file}]"
fi
header "File"
git add "${file}"
assertExitCode
git commit -m "Bump version to ${val}"
assertExitCode
git push
assertExitCode
header "Tag Version"
git tag -a "${val}" -m "version ${val}"
assertExitCode
git push private refs/tags/"${val}"
assertExitCode
fi
# Notes:
#
# Lists and counts all revisions.
# revision=`git rev-list HEAD | wc -l | tr -d " "`
#
# Lists and counts all revisions since last tag.
# tag=`git describe --tags --abbrev=0`
# revision=`git log "${tag}"..HEAD --oneline | wc -l | tr -d " "`