diff --git a/{{cookiecutter.project_slug}}/tasks.py b/{{cookiecutter.project_slug}}/tasks.py index be12127..1ad7083 100644 --- a/{{cookiecutter.project_slug}}/tasks.py +++ b/{{cookiecutter.project_slug}}/tasks.py @@ -1,6 +1,11 @@ # type: ignore +import re +from pathlib import Path + from invoke import task +PYPROJECT = Path(__file__).parent / "pyproject.toml" + @task def venv(c): @@ -42,6 +47,38 @@ def ci(c): c.run(f"docker run --rm {image} sh -c '{cmd}'") +@task +def bump(c, part): + """Bump version (patch/minor/major), commit, and tag.""" + if part not in ("patch", "minor", "major"): + raise SystemExit("Usage: inv bump ") + + result = c.run("git status --porcelain", hide=True) + if result.stdout.strip(): + raise SystemExit("Working tree is dirty. Commit or stash changes first.") + + text = PYPROJECT.read_text() + m = re.search(r'version = "(\d+)\.(\d+)\.(\d+)"', text) + if not m: + raise SystemExit("Could not find version in pyproject.toml") + + major, minor, patch = int(m[1]), int(m[2]), int(m[3]) + if part == "major": + major, minor, patch = major + 1, 0, 0 + elif part == "minor": + minor, patch = minor + 1, 0 + else: + patch += 1 + + new_version = f"{major}.{minor}.{patch}" + new_text = re.sub(r'(version = ")\d+\.\d+\.\d+(")', rf"\g<1>{new_version}\2", text) + PYPROJECT.write_text(new_text) + + c.run(f'git add pyproject.toml && git commit -m "bump version to {new_version}"') + c.run(f"git tag v{new_version}") + print(f"Bumped to {new_version}") + + @task def clean(c): """Preview files to delete (safe mode)."""