Created
September 30, 2023 10:41
-
-
Save salmanmorshed/91d1a1f7e6870e45bb96ac221b9ab9c5 to your computer and use it in GitHub Desktop.
Handy script for bumping semvers in pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import sys | |
| import semver | |
| import tomlkit | |
| def read_version_from_pyproject() -> semver.Version: | |
| with open("pyproject.toml", "r") as file: | |
| pyproject = tomlkit.load(file) | |
| return semver.Version.parse(pyproject["project"]["version"]) | |
| def write_version_to_pyproject(version: semver.Version): | |
| with open("pyproject.toml", "r") as file: | |
| pyproject = tomlkit.load(file) | |
| pyproject["project"]["version"] = str(version) | |
| with open("pyproject.toml", "w") as file: | |
| tomlkit.dump(pyproject, file) | |
| def main() -> int: | |
| parser = argparse.ArgumentParser(description="Bump package version") | |
| subparsers = parser.add_subparsers(title="Commands", dest="command") | |
| subparsers.add_parser("print", help="Print current version") | |
| subparsers.add_parser("major", help="Bump MAJOR version") | |
| subparsers.add_parser("minor", help="Bump MINOR version") | |
| subparsers.add_parser("patch", help="Bump PATCH version") | |
| args = parser.parse_args() | |
| version = read_version_from_pyproject() | |
| if args.command == "print": | |
| print(f"Current version: {version}") | |
| return 0 | |
| elif args.command == "major": | |
| new_version = version.bump_major() | |
| elif args.command == "minor": | |
| new_version = version.bump_minor() | |
| elif args.command == "patch": | |
| new_version = version.bump_patch() | |
| else: | |
| parser.print_help() | |
| return 1 | |
| write_version_to_pyproject(new_version) | |
| print(f"Bumped version from {version} to {new_version}") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment