Skip to content

Commit e87840c

Browse files
committed
Add versioning script and version file
These were copied from cisagov/skeleton-packer. Also add semver as a dependency since it is required for the bump-version script.
1 parent f794ee8 commit e87840c

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

bump-version

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env bash
2+
3+
# bump-version [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show)
4+
# bump-version --list-files
5+
6+
set -o nounset
7+
set -o errexit
8+
set -o pipefail
9+
10+
# Stores the canonical version for the project.
11+
VERSION_FILE=version.txt
12+
# Files that should be updated with the new version.
13+
VERSION_FILES=("$VERSION_FILE")
14+
15+
USAGE=$(
16+
cat << END_OF_LINE
17+
Update the version of the project.
18+
19+
Usage:
20+
${0##*/} [--push] [--label LABEL] (major | minor | patch | prerelease | build | finalize | show)
21+
${0##*/} --list-files
22+
${0##*/} (-h | --help)
23+
24+
Options:
25+
-h | --help Show this message.
26+
--push Perform a \`git push\` after updating the version.
27+
--label LABEL Specify the label to use when updating the build or prerelease version.
28+
--list-files List the files that will be updated when the version is bumped.
29+
END_OF_LINE
30+
)
31+
32+
old_version=$(< "$VERSION_FILE")
33+
# Comment out periods so they are interpreted as periods and don't
34+
# just match any character
35+
old_version_regex=${old_version//\./\\\.}
36+
new_version="$old_version"
37+
38+
bump_part=""
39+
label=""
40+
commit_prefix="Bump"
41+
with_push=false
42+
commands_with_label=("build" "prerelease")
43+
commands_with_prerelease=("major" "minor" "patch")
44+
with_prerelease=false
45+
46+
#######################################
47+
# Display an error message, the help information, and exit with a non-zero status.
48+
# Arguments:
49+
# Error message.
50+
#######################################
51+
function invalid_option() {
52+
echo "$1"
53+
echo "$USAGE"
54+
exit 1
55+
}
56+
57+
#######################################
58+
# Bump the version using the provided command.
59+
# Arguments:
60+
# The version to bump.
61+
# The command to bump the version.
62+
# Returns:
63+
# The new version.
64+
#######################################
65+
function bump_version() {
66+
local temp_version
67+
temp_version=$(python -c "import semver; print(semver.parse_version_info('$1').${2})")
68+
echo "$temp_version"
69+
}
70+
71+
if [ $# -eq 0 ]; then
72+
echo "$USAGE"
73+
exit 1
74+
else
75+
while [ $# -gt 0 ]; do
76+
case $1 in
77+
--push)
78+
if [ "$with_push" = true ]; then
79+
invalid_option "Push has already been set."
80+
fi
81+
82+
with_push=true
83+
shift
84+
;;
85+
--label)
86+
if [ -n "$label" ]; then
87+
invalid_option "Label has already been set."
88+
fi
89+
90+
label="$2"
91+
shift 2
92+
;;
93+
build | finalize | major | minor | patch)
94+
if [ -n "$bump_part" ]; then
95+
invalid_option "Only one version part should be bumped at a time."
96+
fi
97+
98+
bump_part="$1"
99+
shift
100+
;;
101+
prerelease)
102+
with_prerelease=true
103+
shift
104+
;;
105+
show)
106+
echo "$old_version"
107+
exit 0
108+
;;
109+
-h | --help)
110+
echo "$USAGE"
111+
exit 0
112+
;;
113+
--list-files)
114+
printf '%s\n' "${VERSION_FILES[@]}"
115+
exit 0
116+
;;
117+
*)
118+
invalid_option "Invalid option: $1"
119+
;;
120+
esac
121+
done
122+
fi
123+
124+
if [ -n "$label" ] && [ "$with_prerelease" = false ] && [[ ! " ${commands_with_label[*]} " =~ [[:space:]]${bump_part}[[:space:]] ]]; then
125+
invalid_option "Setting the label is only allowed for the following commands: ${commands_with_label[*]}"
126+
fi
127+
128+
if [ "$with_prerelease" = true ] && [ -n "$bump_part" ] && [[ ! " ${commands_with_prerelease[*]} " =~ [[:space:]]${bump_part}[[:space:]] ]]; then
129+
invalid_option "Changing the prerelease is only allowed in conjunction with the following commands: ${commands_with_prerelease[*]}"
130+
fi
131+
132+
label_option=""
133+
if [ -n "$label" ]; then
134+
label_option="token='$label'"
135+
fi
136+
137+
if [ -n "$bump_part" ]; then
138+
if [ "$bump_part" = "finalize" ]; then
139+
commit_prefix="Finalize"
140+
bump_command="finalize_version()"
141+
elif [ "$bump_part" = "build" ]; then
142+
bump_command="bump_${bump_part}($label_option)"
143+
else
144+
bump_command="bump_${bump_part}()"
145+
fi
146+
new_version=$(bump_version "$old_version" "$bump_command")
147+
echo Changing version from "$old_version" to "$new_version"
148+
fi
149+
150+
if [ "$with_prerelease" = true ]; then
151+
bump_command="bump_prerelease($label_option)"
152+
temp_version=$(bump_version "$new_version" "$bump_command")
153+
echo Changing version from "$new_version" to "$temp_version"
154+
new_version="$temp_version"
155+
fi
156+
157+
tmp_file=/tmp/version.$$
158+
for version_file in "${VERSION_FILES[@]}"; do
159+
if [ ! -f "$version_file" ]; then
160+
echo Missing expected file: "$version_file"
161+
exit 1
162+
fi
163+
sed "s/$old_version_regex/$new_version/" "$version_file" > $tmp_file
164+
mv $tmp_file "$version_file"
165+
done
166+
167+
git add "${VERSION_FILES[@]}"
168+
git commit --message "$commit_prefix version from $old_version to $new_version"
169+
170+
if [ "$with_push" = true ]; then
171+
git push
172+
fi

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# The bump-version script requires at least version 3 of semver.
2+
semver>=3
13
setuptools
24
wheel

version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1

0 commit comments

Comments
 (0)