Skip to content

Instantly share code, notes, and snippets.

@kaar
Last active October 3, 2023 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaar/dc5514218fc5ecfdb608a32cfe021eb7 to your computer and use it in GitHub Desktop.
Save kaar/dc5514218fc5ecfdb608a32cfe021eb7 to your computer and use it in GitHub Desktop.
Bump pyproject.toml version, create release by pushing git tag
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
PROJECT_FILE=pyproject.toml
# Usage:
# ./pyversion.sh # Show current version
# ./pyversion.sh major # Increase major version
# ./pyversion.sh minor # Increase minor version
# ./pyversion.sh patch # Increase patch version
# ./pyversion.sh --release # Create a release for the current version
# ./pyversion.sh --help # Show help
#
# Example:
# ./pyversion.sh major # 1.0.0 -> 2.0.0
# ./pyversion.sh minor # 1.0.0 -> 1.1.0
# ./pyversion.sh patch # 1.0.0 -> 1.0.1
# ./pyversion.sh # 1.0.0
#
# https://gist.github.com/pete-otaqui/4188238
# I got this link generated by GitHub copilot. It was not the inspiration
# for this script but got generated when the script was finished.
# Left a comment as a tribute:
# https://gist.github.com/pete-otaqui/4188238?permalink_comment_id=4711436#gistcomment-4711436
function create_release {
current_version=$(grep -oP '^version = "\K[^"]+' pyproject.toml)
read -p "Do you want to create a release for v$current_version? (y/n) " -n 1 -r
echo
# Check that master is checked out
if [[ $(git rev-parse --abbrev-ref HEAD) != "master" ]]; then
echo "Error: You must be on master branch"
exit 1
fi
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Creating release v$current_version"
git add $PROJECT_FILE
git commit -m "Bump version from $current_version to $current_version"
git tag -a "v$current_version" -m "v$current_version"
git push origin "v$current_version"
fi
}
# Check if the version file exists
if [ ! -f $PROJECT_FILE ]; then
echo "Error: $PROJECT_FILE not found."
exit 1
fi
current_version=$(grep -oP '^version = "\K[^"]+' pyproject.toml)
major_version=$(echo "$current_version" | cut -d '.' -f 1)
minor_version=$(echo "$current_version" | cut -d '.' -f 2)
patch_version=$(echo "$current_version" | cut -d '.' -f 3)
latest_tag=$(git describe --tags --abbrev=0 2>/dev/null || true)
current_tagged_version=$(echo "$latest_tag" | cut -d 'v' -f 2)
if [ $# -eq 0 ]; then
if [ "$current_tagged_version" == "$current_version" ]; then
echo "$current_version" "(Released)"
else
echo "$current_version" "(Not released)"
fi
exit 0
fi
case "$1" in
major)
major_version=$((major_version + 1))
minor_version=0
patch_version=0
;;
minor)
minor_version=$((minor_version + 1))
patch_version=0
;;
patch)
patch_version=$((patch_version + 1))
;;
--help)
echo "Usage:"
echo "$0 # Show current version"
echo "$0 major # Increase major version"
echo "$0 minor # Increase minor version"
echo "$0 patch # Increase patch version"
echo "$0 --release # Create a release for the current version"
echo ""
exit 0
;;
--release)
if [ "$current_tagged_version" == "$current_version" ]; then
echo "Error: Version $current_version already released"
exit 1
fi
create_release
exit 0
;;
*)
echo "Error: Invalid argument"
help
exit 1
esac
new_version="$major_version.$minor_version.$patch_version"
sed -i "s/^version = \".*\"/version = \"$new_version\"/" $PROJECT_FILE
echo "Bumped version from $current_version to $new_version"
create_release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment