Skip to content

Instantly share code, notes, and snippets.

@filipeherculano
Created July 2, 2020 00:54
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 filipeherculano/844fd001e7b03213c32bc6ca5574cbee to your computer and use it in GitHub Desktop.
Save filipeherculano/844fd001e7b03213c32bc6ca5574cbee to your computer and use it in GitHub Desktop.
Version bumper
#!/usr/bin/env python3
import os
import sys
import re
import fileinput
def update_branch_and_checkout(version):
os.system("git checkout master")
os.system("git pull")
os.system(f"git checkout -b chore/prepare-to-v{version}")
return f"chore/prepare-to-v{version}"
def commit_and_push(branch_name, version):
os.system(f"git commit -a -m \"chore: Prepare to v{version}\"")
os.system(f"git push origin {branch_name}")
def create_pull_request():
os.system("hub pull-request")
def update_changelog(version, filename = "CHANGELOG.md"):
with fileinput.FileInput(filename, inplace=True) as file:
for line in file:
if bool(re.match(r".*\[unreleased\]:.*", line, re.IGNORECASE)):
current_version = get_current_version_as_string()
new_line = re.sub(r"HEAD", f"v{version}", line)
new_line = re.sub(r"\[.nreleased\]", f"[{version}]", new_line)
print(re.sub("{}".format(current_version), f"v{version}", line), end='')
print(new_line, end='')
else:
print(re.sub(r"## \[Unreleased\]", f"## [Unreleased]\n\n## [{version}]", line), end='')
def update_version(version, version_file_path = "__version__.py"):
os.system(f"echo \"__version__ = \"{version}\"\" > __version__.py")
def get_current_version(version_file_path = "__version__.py"):
with open(version_file_path) as fp:
line = fp.readline()
version = line.split(" = ")[1]
[major, minor, patch] = version.split(".")
return [major[1:], minor, patch[:-2]]
def get_current_version_as_string(version_file_path = "__version__.py"):
[major, minor, patch] = get_current_version(version_file_path)
return f"v{major}.{minor}.{patch}"
def update_if_match(value, context, new_context):
return int(value) + (1 if context == new_context else 0)
def zero_rest(major, minor, patch, context):
if context == "major":
return [major, 0, 0]
if context == "minor":
return [major, minor, 0]
return [major, minor, patch]
def get_new_version(context):
[major, minor, patch] = get_current_version()
major = update_if_match(major, "major", context)
minor = update_if_match(minor, "minor", context)
patch = update_if_match(patch, "patch", context)
[major, minor, patch] = zero_rest(major, minor, patch, context)
return f"{major}.{minor}.{patch}"
def bump_version():
version = get_new_version(sys.argv[1])
branch_name = update_branch_and_checkout(version)
update_changelog(version)
update_version(version)
commit_and_push(branch_name, version)
create_pull_request()
def main():
bump_version()
if __name__ == "__main__":
main()
@filipeherculano
Copy link
Author

Usage:

python3 bump_version.py major
python3 bump_version.py minor
python3 bump_version.py patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment