Skip to content

Instantly share code, notes, and snippets.

@JCGoran
Last active November 7, 2022 09:25
Show Gist options
  • Save JCGoran/2dc14ddfa0fae1f0ba382cc7bae1b3f9 to your computer and use it in GitHub Desktop.
Save JCGoran/2dc14ddfa0fae1f0ba382cc7bae1b3f9 to your computer and use it in GitHub Desktop.
Script for bumping the version of a Python package using Poetry and a VERSION.txt file
#!/usr/bin/env sh
# script for bumping the version of a Python package (major, minor, or patch)
# to keep both poetry and the file containing the version number in sync
# requirements:
# - Python 3 with venv and Poetry
# - any POSIX-compatible shell
# - awk
show_usage(){
cat << EOM
USAGE: ./bump_version.sh [-M|-m|-p]
OPTIONS:
-M bump the major version
-m bump the minor version
-p bump the patch version
EOM
}
update_version(){
if [ -z "${VIRTUAL_ENV}" ]
then
printf "ERROR: you are not in a virtual environment, aborting...\n"
return 1
fi
# relative path of the file containing the version
VERSION_PATH="$(python3 -m poetry version --no-ansi | awk '{print $1}')/VERSION.txt"
package_version="$(python3 -m poetry version --short --no-ansi)"
major="$(printf '%s' "${package_version}" | awk -F'.' '{print $1}')"
minor="$(printf '%s' "${package_version}" | awk -F'.' '{print $2}')"
patch="$(printf '%s' "${package_version}" | awk -F'.' '{print $3}')"
current_version="${major}.${minor}.${patch}"
printf "Current version: %s\n" "${current_version}"
case $1 in
'-M')
major=$(( major + 1 ))
minor='0'
patch='0'
;;
'-m')
minor=$(( minor + 1 ))
patch='0'
;;
'-p')
patch=$(( patch + 1 ))
;;
*)
show_usage
return 255
;;
esac
version="${major}.${minor}.${patch}"
printf "Proposed change: %s -> %s, are you sure? " "${current_version}" "${version}"
read -r REPLY
if [ "${REPLY}" = 'y' ] || [ "${REPLY}" = 'Y' ]
then
printf "%s\n" "${version}" > "${VERSION_PATH}"
python3 -m poetry version "${version}"
return 0
fi
printf "ERROR: response not 'y' or 'Y', aborting...\n"
return 2
}
update_version "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment