Skip to content

Commit 35958fb

Browse files
authored
Merge pull request #43 from cisagov/improvement/enhance_bump_version_script
Enhance the `bump_version.sh` script
2 parents 140e21f + dd12011 commit 35958fb

File tree

5 files changed

+158
-54
lines changed

5 files changed

+158
-54
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ against any directory that houses `.pkr.hcl` files.
3030
```yaml
3131
repos:
3232
- repo: https://github.com/cisagov/pre-commit-packer
33-
rev: v0.0.2
33+
rev: v0.1.0
3434
hooks:
3535
- id: packer_fmt
3636
- id: packer_validate

bump-version

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

bump_version.sh

Lines changed: 0 additions & 51 deletions
This file was deleted.

config/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.1.0

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
--requirement requirements-test.txt
22
ipython
3-
semver
3+
semver>=3

0 commit comments

Comments
 (0)