|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Task execution tool & library |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | +import re |
| 8 | +import sys |
| 9 | +from datetime import datetime |
| 10 | +from logging import basicConfig, getLogger |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import git |
| 14 | +from bumpversion.cli import main as bumpversion |
| 15 | +from invoke import task |
| 16 | + |
| 17 | +LOG_FORMAT = json.dumps( |
| 18 | + { |
| 19 | + "timestamp": "%(asctime)s", |
| 20 | + "namespace": "%(name)s", |
| 21 | + "loglevel": "%(levelname)s", |
| 22 | + "message": "%(message)s", |
| 23 | + } |
| 24 | +) |
| 25 | +basicConfig(level="INFO", format=LOG_FORMAT) |
| 26 | +LOG = getLogger("cookiecutter-python") |
| 27 | + |
| 28 | +CWD = Path(".").absolute() |
| 29 | +REPO = git.Repo(CWD) |
| 30 | + |
| 31 | + |
| 32 | +# Tasks |
| 33 | +@task |
| 34 | +def test(c): # pylint: disable=unused-argument |
| 35 | + """Test cookiecutter-python""" |
| 36 | + LOG.warning("TODO: Implement tests") |
| 37 | + |
| 38 | + |
| 39 | +@task(pre=[test]) |
| 40 | +def release(c): # pylint: disable=unused-argument |
| 41 | + """Make a new release of cookiecutter-python""" |
| 42 | + if REPO.head.is_detached: |
| 43 | + LOG.error("In detached HEAD state, refusing to release") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + # Get the current date info |
| 47 | + date_info = datetime.now().strftime("%Y.%m") |
| 48 | + |
| 49 | + # Our CalVer pattern which works until year 2200, up to 100 releases a |
| 50 | + # month (purposefully excludes builds) |
| 51 | + pattern = re.compile(r"v2[0-1][0-9]{2}.(0[0-9]|1[0-2]).[0-9]{2}") |
| 52 | + |
| 53 | + # Identify and set the increment |
| 54 | + for tag in reversed(REPO.tags): |
| 55 | + if pattern.fullmatch(tag.name): |
| 56 | + latest_release = tag.name |
| 57 | + break |
| 58 | + else: |
| 59 | + latest_release = None |
| 60 | + |
| 61 | + if latest_release and date_info == latest_release[:7]: |
| 62 | + increment = str(int(latest_release[8:]) + 1).zfill(2) |
| 63 | + else: |
| 64 | + increment = "01" |
| 65 | + |
| 66 | + new_version = date_info + "." + increment |
| 67 | + |
| 68 | + bumpversion(["--new-version", new_version, "unusedpart"]) |
0 commit comments