-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtasks.py
More file actions
186 lines (161 loc) · 4.83 KB
/
Copy pathtasks.py
File metadata and controls
186 lines (161 loc) · 4.83 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
This module demonstrates creating a TaskCollection, including:
- multiple definitions for some tasks, which definition is used depends on order and the
included/excluded tags.
- using generators to lazily define tasks based on tags and arbitrary logic.
"""
from .resources import get_path
from .tags import TagEvaluator
from .task_collection import TaskCollection
RUFF_TOML_PATH = str(get_path("ruff.toml"))
def _files_arg(default="src"):
return {
"name": "files",
"positional": True,
"multiple": True,
"help": "The files to format",
"default": default,
}
tasks = TaskCollection()
tasks.add(
task_name="lint",
task_config={
"help": "Run ruff linter on code base",
"cmd": 'ruff check --config="${RUFF_CONFIG:-' + RUFF_TOML_PATH + '}" $files',
"args": [_files_arg(default="src tests")],
},
tags=["lint", "test", "ruff"],
)
tasks.add(
task_name="lint",
task_config={
"help": "Run ruff linter on code base",
"cmd": 'ruff check --config="${RUFF_CONFIG:-' + RUFF_TOML_PATH + '}" $files',
"args": [_files_arg()],
},
tags=["lint", "ruff"],
)
tasks.add(
task_name="style",
task_config={
"help": "Validate black code style",
"cmd": "black $files --check --diff",
"args": [_files_arg(default="src tests")],
},
tags=["style", "test", "black"],
)
tasks.add(
task_name="style",
task_config={
"help": "Validate black code style",
"cmd": "black $files --check --diff",
"args": [_files_arg()],
},
tags=["style", "black"],
)
tasks.add(
task_name="types",
task_config={
"help": "Run the type checker",
"cmd": "mypy --ignore-missing-imports $files",
"args": [_files_arg(default="src tests")],
},
tags=["test", "mypy"],
)
tasks.add(
task_name="types",
task_config={
"help": "Run the type checker",
"cmd": "mypy --ignore-missing-imports $files",
"args": [_files_arg()],
},
tags=["mypy"],
)
tasks.add(
task_name="test",
task_config={
"help": "Run tests",
"cmd": "pytest",
},
tags=["pytest"],
)
tasks.add(
task_name="clean",
task_config={
"script": """
poethepoet.scripts:rm(
".coverage",
".ruff_cache",
".mypy_cache",
".pytest_cache",
"./**/__pycache__",
"dist",
"htmlcov",
verbosity=environ.get('POE_VERBOSITY'),
dry_run=_dry_run
)
""",
"help": "Remove generated files",
},
)
@tasks.generate
def generate_format_tasks(requested_tags: TagEvaluator):
"""
Generate tasks for code formatting based on whether ruff or black are
"""
files_arg = _files_arg(
default="src tests" if requested_tags.evaluate("test") else "src"
)
ruff_cmd = (
'ruff check --config="${RUFF_CONFIG:-' + RUFF_TOML_PATH + '}" --fix-only $files'
)
black_cmd = "black $files"
if requested_tags.all("ruff", "black"):
yield "format", {
"sequence": ["format-ruff $files", "format-black $files"],
"help": "Run ruff and black formatters on the code base",
"args": [files_arg],
}
yield "format-ruff", {
"cmd": ruff_cmd,
"help": "Run ruff formatter on the code base",
"args": [files_arg],
}
yield "format-black", {
"cmd": black_cmd,
"help": "Run black formatter on the code base",
"args": [files_arg],
}
elif requested_tags.evaluate("ruff"):
yield "format", {
"cmd": ruff_cmd,
"help": "Run ruff formatter on the code base",
"args": [files_arg],
}
elif requested_tags.evaluate("black"):
yield "format", {
"cmd": black_cmd,
"help": "Run black formatter on the code base",
"args": [files_arg],
}
@tasks.generate
def generate_check_task(requested_tags: TagEvaluator):
"""
Generates `check` task as a sequence including `lint`, `style`, `types`, and `test`
tasks in that order, excluding any tasks that are explicitly excluded a
`task-<taskname>` tag.
If all referenced tasks are excluded then no check task is generated.
"""
task_name = "check"
doc = "Run all checks on the code base"
sequence = []
if not requested_tags.excluded("task-lint"):
sequence.append("lint")
if not requested_tags.excluded("task-style"):
sequence.append("style")
if not requested_tags.excluded("task-types"):
sequence.append("types")
if not requested_tags.excluded("task-test") and not requested_tags.excluded("test"):
sequence.append("test")
if sequence:
yield task_name, {"help": doc, "sequence": sequence}