Skip to content

Commit

Permalink
Script to bump project version
Browse files Browse the repository at this point in the history
  • Loading branch information
irskep committed Aug 27, 2024
1 parent 1ae73cc commit 4b500ae
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions scripts/version
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
import argparse
import json
import pathlib
import re

PACKAGE_JSON = pathlib.Path("package.json").absolute()
DJOCKEY_YAML = (pathlib.Path("docs") / "djockey.yaml").absolute()


SLOTS = {
"major": 0,
"minor": 1,
"patch": 2,
}


def main():
p = argparse.ArgumentParser()
g = p.add_subparsers(required=True)
sp_major = g.add_parser("bump-major")
sp_major.set_defaults(action="major")
sp_minor = g.add_parser("bump-minor")
sp_minor.set_defaults(action="minor")
sp_patch = g.add_parser("bump-patch")
sp_patch.set_defaults(action="patch")

for sp in [sp_major, sp_minor, sp_patch]:
sp.add_argument("--dry-run", action="store_true")

args = p.parse_args()

current_version = json.loads(PACKAGE_JSON.read_text())["version"]
print("Current version:", current_version)

new_version = bump_semver_minor(current_version, args.action)
print("New version:", new_version)

if not args.dry_run:
replace_line(
PACKAGE_JSON,
re.compile(r' "version": ".*",'),
f' "version": "{new_version}",',
)
replace_line(
DJOCKEY_YAML, re.compile(r" version: 0.0.1"), f" version: {new_version}"
)


def bump_semver_minor(frm: str, slot: str):
parts = [int(p) for p in frm.split(".")]
slot = SLOTS[slot]
parts[slot] += 1

for i in range(slot + 1, 3):
parts[i] = 0

return ".".join([str(p) for p in parts])


def replace_line(path: pathlib.Path, regex, new_value: str):
old_lines = path.read_text().splitlines()
new_lines = [new_value if regex.match(line) else line for line in old_lines]
path.write_text("\n".join(new_lines) + "\n")


if __name__ == "__main__":
main()

0 comments on commit 4b500ae

Please sign in to comment.