Skip to content

Commit

Permalink
Merge pull request #169 from cisagov/improvement/enhance_bump_version…
Browse files Browse the repository at this point in the history
…_script

Enhance the functionality of the `bump_version.sh` script
  • Loading branch information
mcdonnnj authored Dec 6, 2023
2 parents c70e91e + d6715e7 commit bf95545
Showing 1 changed file with 55 additions and 28 deletions.
83 changes: 55 additions & 28 deletions bump_version.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/usr/bin/env bash

# bump_version.sh (show|major|minor|patch|prerelease|build)
# Usage:
# bump_version.sh (show|major|minor|patch|finalize)
# bump_version.sh (build|prerelease) [token]
# Notes:
# - If you specify a token it will only be used if the current version is
# tokenless or if the provided token matches the token used in the current
# version.

set -o nounset
set -o errexit
Expand All @@ -9,46 +15,67 @@ set -o pipefail
VERSION_FILE=src/version.txt
README_FILE=README.md

HELP_INFORMATION="bump_version.sh (show|major|minor|patch|prerelease|build|finalize)"
function usage {
cat << HELP
Usage:
${0##*/} (show|major|minor|patch|finalize)
${0##*/} (build|prerelease) [token]
old_version=$(sed -n "s/^__version__ = \"\(.*\)\"$/\1/p" $VERSION_FILE)
# Comment out periods so they are interpreted as periods and don't
# just match any character
old_version_regex=${old_version//\./\\\.}
Notes:
- If you specify a token it will only be used if the current version is
tokenless or if the provided token matches the token used in the current
version.
HELP
exit 1
}

if [ $# -ne 1 ]; then
echo "$HELP_INFORMATION"
function update_version {
# Comment out periods so they are interpreted as periods and don't
# just match any character
old_version_regex=${1//\./\\\.}

echo Changing version from "$1" to "$2"
tmp_file=/tmp/version.$$
sed "s/$old_version_regex/$2/" $VERSION_FILE > $tmp_file
mv $tmp_file $VERSION_FILE
sed "s/$old_version_regex/$2/" $README_FILE > $tmp_file
mv $tmp_file $README_FILE
git add $VERSION_FILE $README_FILE
git commit --message "$3"
}

if [ $# -lt 1 ] || [ $# -gt 2 ]; then
usage
else
old_version=$(sed -n "s/^__version__ = \"\(.*\)\"$/\1/p" $VERSION_FILE)
case $1 in
major | minor | patch | prerelease | build)
major | minor | patch)
if [ $# -ne 1 ]; then
usage
fi
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version'))")
echo Changing version from "$old_version" to "$new_version"
tmp_file=/tmp/version.$$
sed "s/$old_version_regex/$new_version/" $VERSION_FILE > $tmp_file
mv $tmp_file $VERSION_FILE
sed "s/$old_version_regex/$new_version/" $README_FILE > $tmp_file
mv $tmp_file $README_FILE
git add $VERSION_FILE $README_FILE
git commit -m"Bump version from $old_version to $new_version"
git push
update_version "$old_version" "$new_version" "Bump version from $old_version to $new_version"
;;
build | prerelease)
if [ $# -eq 2 ]; then
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version', token='$2'))")
else
new_version=$(python -c "import semver; print(semver.bump_$1('$old_version'))")
fi
update_version "$old_version" "$new_version" "Bump version from $old_version to $new_version"
;;
finalize)
if [ $# -ne 1 ]; then
usage
fi
new_version=$(python -c "import semver; print(semver.finalize_version('$old_version'))")
echo Changing version from "$old_version" to "$new_version"
tmp_file=/tmp/version.$$
sed "s/$old_version_regex/$new_version/" $VERSION_FILE > $tmp_file
mv $tmp_file $VERSION_FILE
sed "s/$old_version_regex/$new_version/" $README_FILE > $tmp_file
mv $tmp_file $README_FILE
git add $VERSION_FILE $README_FILE
git commit -m"Finalize version from $old_version to $new_version"
git push
update_version "$old_version" "$new_version" "Finalize version from $old_version to $new_version"
;;
show)
echo "$old_version"
;;
*)
echo "$HELP_INFORMATION"
usage
;;
esac
fi

0 comments on commit bf95545

Please sign in to comment.