-
Notifications
You must be signed in to change notification settings - Fork 16
/
tasks.py
113 lines (83 loc) · 2.89 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
from datetime import date
from shlex import quote
import invoke
PROJECT_DIR = "postal_address"
CBLUE = "\33[34m"
CEND = "\33[0m"
CBOLD = "\33[1m"
def log(msg: str, section_name: str = "release") -> None:
print(f"{CBLUE}[{section_name}]{CEND} {msg}")
def log_section(section_name: str) -> None:
print(f"{CBLUE}{CBOLD}[{section_name}]{CEND}")
@invoke.task
def lint(ctx, changes=False):
path = PROJECT_DIR
if changes:
path = f"$(git diff --name-only --diff-filter=d {PROJECT_DIR})"
log_section("mypy")
ctx.run(f"poetry run mypy {path}", pty=True)
log_section("flakeheaven")
ctx.run(f"poetry run flakeheaven lint {path}", pty=True)
@invoke.task
def format(ctx, changes=False):
path = PROJECT_DIR
if changes:
path = f"$(git diff --name-only --diff-filter=d {PROJECT_DIR})"
log_section("autoflake")
ctx.run(
f"poetry run autoflake -ri --remove-all-unused-imports {path}",
pty=True,
)
log_section("isort")
ctx.run(f"poetry run isort --atomic {path}", pty=True)
log_section("black")
ctx.run(f"poetry run black {path}", pty=True)
@invoke.task
def test(ctx, scope=""):
if not scope:
cov = "--cov"
else:
cov = f"--cov={scope}"
ctx.run(f"poetry run pytest --color=yes {scope} {cov}")
@invoke.task
def get_current_version(ctx):
return ctx.run("poetry version --short", hide=True).stdout.strip()
@invoke.task
def bump_version(ctx, dry_run=False):
"""Bump the project's version."""
current_version = get_current_version(ctx)
current_version_tuple = tuple(current_version.split("."))
today = date.today()
new_version_tuple = (str(today.year)[2:], str(today.month), str(today.day))
if current_version_tuple[:3] == new_version_tuple:
if len(current_version_tuple) == 3:
new_version_tuple = (*new_version_tuple, "1")
else:
new_version_tuple = (*new_version_tuple, str(int(current_version_tuple[3]) + 1))
new_version = ".".join(new_version_tuple)
cmd = f"poetry version {new_version}"
if dry_run:
log(f"Would run '{cmd}' and stage pyproject.toml")
else:
ctx.run(cmd)
ctx.run("git add pyproject.toml")
return new_version
@invoke.task
def release(ctx, dry_run=False):
version = bump_version(ctx, dry_run=dry_run)
tag = f"{version}"
tag_message = f"Version {tag}"
commit_message = quote(f"Release {tag}")
# Commit, tag and push
if dry_run:
log(f"Would commit with message: {commit_message}")
log(f"Generated tag content:\n{tag_message}")
log("Would push tags")
else:
log("Commit all staged files")
ctx.run(f"git commit -m {commit_message}")
log("Tag commit")
ctx.run(f"git tag -a {tag} -m {quote(tag_message)}")
log("Push commit and tag")
ctx.run("git push origin HEAD:master")
ctx.run("git push --tags")