This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
duties.py
142 lines (113 loc) · 4.49 KB
/
duties.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import shutil
import tomli
import tomli_w
from duty import duty
from duty.context import Context
__all__ = ()
@duty
def docs(ctx: Context, host="127.0.0.1", port=8000, *, serve=False, clean=False) -> None:
"""
Generate documentation for the project.
Args:
ctx: The context of this duty
host: the host to use for serving
port: the port to use for serving
serve: whether to serve the documentation
clean: whether to clean temporary files before building
"""
if clean:
def clean() -> None:
shutil.rmtree("site")
shutil.rmtree(".cache")
ctx.run(clean, title="Cleaning temporary files")
if serve:
ctx.run(
"mkdocs serve",
args=["-a", f"{host}:{port}"],
title="Serving docs...",
capture=False,
)
else:
ctx.run("mkdocs build", title="Building docs...", capture=False)
@duty
def bump(ctx: Context, bump_type: str, *labels, commit=False) -> None:
"""
Bump the version of the project.
Args:
ctx: The context of this duty
bump_type: The type of bump, can be an explicit value, or "major", "minor", "patch", "label" or "strip"
commit: Whether to commit the changes
"""
old_labels: list[str] = []
with open("pyproject.toml", "rb") as f:
pyproject = tomli.load(f)
version = pyproject["tool"]["poetry"]["version"]
if not version:
version = "0.0.0"
if "-" in version:
version, *old_labels = version.split("-")
major, minor, patch = map(int, version.split("."))
match bump_type.lower():
case "major":
# Bump the major version
major += 1
case "minor":
# Bump the minor version
minor += 1
case "patch":
# Bump the patch version
patch += 1
case "strip":
# Strip the labels
old_labels = []
case "label":
# Add a label
pass
case _:
# Explicit version
try:
bump_type = list(map(int, bump_type.split(".")))
major, minor, patch = bump_type + [0] * (3 - len(bump_type))
except ValueError:
raise ValueError("Invalid bump type. Expected values are 'major', 'minor' or 'patch'.") from None
labels = [*old_labels, *labels]
formatted_labels = "-".join(labels)
new_version = f"{major}.{minor}.{patch}" + (f"-{formatted_labels}" if labels else "")
def version_bump(_bump_type: str) -> None:
# this didnt need to be a function, but i wanted the ctx output
pyproject["tool"]["poetry"]["version"] = new_version
with open("pyproject.toml", "wb") as f:
tomli_w.dump(pyproject, f)
ctx.run(
version_bump,
args=[bump_type],
title=f"Bumping version {pyproject['tool']['poetry']['version']} -> {new_version}",
)
if commit:
# commit the changes
ctx.run(["git", "add", "pyproject.toml"], title="Staging Commit...")
ctx.run(["git", "commit", "-m", "chore: Version bump"], title="Committing changes...")
@duty
def build(ctx: Context) -> None:
ctx.run(shutil.rmtree, args=["dist", "ignore_errors=True"], title="Clearing dist directory...")
ctx.run(shutil.rmtree, args=["build", "ignore_errors=True"], title="Clearing build directory...")
ctx.run("python -m pip install --upgrade pip", title="Updating pip...")
ctx.run("python -m pip install setuptools wheel twine tomli -U", title="Installing build tools...")
ctx.run("python -m pip install -e .[all] -U", title="Updating dependencies...")
ctx.run("python setup.py sdist bdist_wheel", title="Building distribution...")
@duty(pre=["build"])
def release(ctx: Context, *, test=False) -> None:
pypi_cmd = ["python", "-m", "twine", "upload", "dist/*"]
if test:
pypi_cmd += ["--repository", "testpypi"]
with open("pyproject.toml", "rb") as f:
pyproject = tomli.load(f)
version = pyproject["tool"]["poetry"]["version"]
# this assumes you use pypirc to authenticate
pypi_out = ctx.run(pypi_cmd, title=f"Uploading {version} to {'Test' if test else ''}PyPI...")
release_url = ctx.run(
["gh", "release", "create", f"NAFF-{version}", "-d", "-t", f"NAFF {version}", "--generate-notes"],
title=f"Creating release {version} on GitHub...",
)
print(f"PyPi URL : {pypi_out.split()[-1]}") # noqa: T201
print(f"Release URL: {release_url}") # noqa: T201