/
tasks.py
122 lines (97 loc) · 3.7 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
from pathlib import Path # noqa: F401
import tomllib
from colorama import Fore, init
from invoke import task
@task(help={"fix": "let black and isort format your files"})
def lint(c, fix=False):
"""flake8, black and isort"""
if fix:
c.run("black .", pty=True)
c.run("isort --profile black .", pty=True)
else:
c.run("black . --check", pty=True)
c.run("isort --check-only --profile black .", pty=True)
c.run("flake8 .", pty=True)
# TODO: create a "clean" collection comprising the next two tasks below
@task
def clean_pyc(c):
"""remove Python file artifacts"""
c.run("find . -name '*.pyc' -exec rm -f {} +", pty=True)
c.run("find . -name '*.pyo' -exec rm -f {} +", pty=True)
c.run("find . -name '*~' -exec rm -f {} +", pty=True)
c.run("find . -name '__pycache__' -exec rm -fr {} +", pty=True)
@task
def clean_test(c):
"""remove test and coverage artifacts"""
c.run("rm -fr .tox/", pty=True)
c.run("rm -f .coverage", pty=True)
c.run("rm -f coverage.xml", pty=True)
c.run("rm -fr htmlcov/", pty=True)
c.run("rm -fr .pytest_cache", pty=True)
@task(
help={
"branch": "The branch against which you wanna bump",
"push": "Push to origin after bumping",
}
)
def bump(c, branch="main", push=False):
"""Use BumpVer & standard-version to bump version and generate changelog
Run this task when you want to prepare a release.
First we check that there are no unstaged files before running
"""
init()
unstaged_str = "not staged for commit"
uncommitted_str = "to be committed"
check = c.run("git status", pty=True)
if unstaged_str not in check.stdout or uncommitted_str not in check.stdout:
get_current_tag = c.run(
"git describe --abbrev=0 --tags `git rev-list --tags --skip=0 --max-count=1`",
pty=True,
)
previous_tag = get_current_tag.stdout.rstrip()
c.run("bumpver update", pty=True)
with open("pyproject.toml", "rb") as f:
toml_dict = tomllib.load(f)
version_files = toml_dict["bumpver"]["file_patterns"].keys()
files_to_add = " ".join(list(version_files))
c.run(
f"git add {files_to_add}",
pty=True,
)
c.run(
f'npm run release -- --commit-all --skip.bump --releaseCommitMessageFormat "bump: ✈️ {previous_tag} → v{{{{currentTag}}}}"',
pty=True,
)
if push:
# push to origin
c.run(f"git push --follow-tags origin {branch}", pty=True)
else:
print(
f"{Fore.RED}Sorry mate, please ensure there are no unstaged files before creating a release{Fore.RESET}"
)
@task
def get_release_notes(c):
"""extract content from CHANGELOG.md for use in Github/Gitlab Releases
we read the file and loop through line by line
we wanna extract content beginning from the first Heading 2 text
to the last line before the next Heading 2 text
"""
pattern_to_match = "## [v"
count = 0
lines = []
heading_text = "## What's changed in this release\n"
lines.append(heading_text)
with open("CHANGELOG.md", "r") as c:
for line in c:
if pattern_to_match in line and count == 0:
count += 1
elif pattern_to_match not in line and count == 1:
lines.append(line)
elif pattern_to_match in line and count == 1:
break
# home = str(Path.home())
# release_notes = os.path.join(home, "LATEST_RELEASE_NOTES.md")
release_notes = os.path.join("../", "LATEST_RELEASE_NOTES.md")
with open(release_notes, "w") as f:
print("".join(lines), file=f, end="")