forked from cisagov/cyhy_amis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump-version
More file actions
executable file
·119 lines (103 loc) · 2.58 KB
/
bump-version
File metadata and controls
executable file
·119 lines (103 loc) · 2.58 KB
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
#!/usr/bin/env bash
# bump-version [--push] (bump | show)
# bump-version --list-files
set -o nounset
set -o errexit
set -o pipefail
# Stores the canonical version for the project.
VERSION_FILE=version.txt
# Files that should be updated with the new version.
VERSION_FILES=("$VERSION_FILE")
USAGE=$(
cat << END_OF_LINE
Update the version of the project.
Usage:
${0##*/} [--push] (bump | show)
${0##*/} --list-files
${0##*/} (-h | --help)
Options:
-h | --help Show this message.
--push Perform a \`git push\` after updating the version.
--list-files List the files that will be updated when the version is bumped.
END_OF_LINE
)
old_version=$(< "$VERSION_FILE")
# Comment out periods so they are interpreted as periods and don't
# just match any character
old_version_regex=${old_version//\./\\\.}
new_version="$old_version"
commit_prefix="Bump"
with_push=false
#######################################
# Display an error message, the help information, and exit with a non-zero status.
# Arguments:
# Error message.
#######################################
function invalid_option() {
echo "$1"
echo "$USAGE"
exit 1
}
#######################################
# Bump the version.
# Arguments:
# None
# Returns:
# The new version.
#######################################
function bump_version() {
local temp_version
# Set it to the current date in UTC. We must use the -u short option to maintain
# support with the BSD date command which is used on macOS.
temp_version=$(date -u +%Y.%m.%d)
echo "$temp_version"
}
if [ $# -eq 0 ]; then
echo "$USAGE"
exit 1
else
while [ $# -gt 0 ]; do
case $1 in
--push)
if [ "$with_push" = true ]; then
invalid_option "Push has already been set."
fi
with_push=true
shift
;;
bump)
shift
;;
show)
echo "$old_version"
exit 0
;;
-h | --help)
echo "$USAGE"
exit 0
;;
--list-files)
printf '%s\n' "${VERSION_FILES[@]}"
exit 0
;;
*)
invalid_option "Invalid option: $1"
;;
esac
done
fi
new_version=$(bump_version)
tmp_file=/tmp/version.$$
for version_file in "${VERSION_FILES[@]}"; do
if [ ! -f "$version_file" ]; then
echo Missing expected file: "$version_file"
exit 1
fi
sed "s/$old_version_regex/$new_version/" "$version_file" > $tmp_file
mv $tmp_file "$version_file"
done
git add "${VERSION_FILES[@]}"
git commit --message "$commit_prefix version from $old_version to $new_version"
if [ "$with_push" = true ]; then
git push
fi