-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
executable file
·318 lines (231 loc) · 7.03 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os, pathlib, platform, subprocess
from invoke import task
from invoke.collection import Collection
from invoke.config import Config
from invoke.parser.argument import Argument
from scripts import config
from scripts import linters
from scripts.lib import string as xstring
# Get the current working directory for the root of the project.
rootdir = pathlib.Path.cwd()
# ==============
# === Config ===
# ==============
cfgpath = os.path.join(rootdir, ".env.yaml")
if not os.path.isfile(cfgpath):
config.generate_config(rootdir)
cfg = Config(defaults={"stage": "development"})
cfg.set_runtime_path(cfgpath)
cfg.load_runtime()
ns = Collection()
ns.configure(cfg)
@task(name="refresh")
def _pre(context):
# Set the current project stage
os.environ["PROJECT_STAGE"] = context.stage
# Set the project commit hash.
os.environ["PROJECT_COMMIT"] = xstring.normalize(
subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
)
# Set the current operating system & CPU architecture of the current
# development environment
os.environ["PROJECT_SYSTEM"] = platform.system().lower()
os.environ["PROJECT_ARCH"] = platform.machine().lower()
generate(context)
ns.add_task(_pre)
# ===================
# === Collections ===
# ===================
# === Generate ===
@task(default=True, name="all")
def generate(context):
"""
Trigger all generators.
"""
generate_config(context)
generate_linters(context)
@task(
name="config",
help={"stage": "Indicate project environment (default 'development')"},
)
def generate_config(context, stage="development"):
"""
Generate all root level config files.
"""
config.generate_config(rootdir, context.stage or stage)
@task(name="linters")
def generate_linters(context):
"""
Copy the linters found in the `./linters` directory to the `root`,
and `./.github/linters` directories.
"""
linters.generate_linters(rootdir)
generate_col = Collection("generate", generate)
generate_col.add_task(generate_config)
generate_col.add_task(generate_linters)
ns.add_collection(generate_col)
# === Dry ===
@task(pre=[_pre], default=True, name="all")
def dry(context):
"""
Run all `dry` tasks.
"""
dry_release(context)
dry_act(context)
@task(pre=[_pre], name="release")
def dry_release(context):
"""
Trigger a new git dry run release via `semantic-release`.
"""
context.run("npm run release-dry-run")
@task(pre=[_pre], default=True, name="all")
def dry_act(context):
"""
Trigger all Github Actions dry-runs.
"""
dry_act_pull_request(context)
dry_act_push(context)
@task(
pre=[_pre],
name="pull-request",
aliases=["pr"],
help={
"dryrun": "Enable dryrun mode",
"job": "Run specified job",
"list": "List available jobs",
"verbose": "Enable verbose output",
},
optional=["job"],
)
def dry_act_pull_request(context, dryrun=False, job=None, list=False, verbose=False):
"""
Trigger all `pull_request` Github Action workflows on the current branch.
"""
flags = [
f"--dryrun={str(dryrun).lower()}",
f"--env DEFAULT_WORKSPACE={rootdir}",
f"--env MEGALINTER_VOLUME_ROOT={rootdir}",
f"--list={str(list).lower()}",
f"--verbose={str(verbose).lower()}",
f"--secret GITHUB_TOKEN={os.environ['GITHUB_TOKEN']}",
]
if job:
context.run(f"act pull_request --job={job} {' '.join(flags)}")
else:
context.run(f"act pull_request {' '.join(flags)}")
@task(
pre=[_pre],
name="push",
help={
"dryrun": "Enable dryrun mode",
"job": "Run specified job",
"list": "List available jobs",
"verbose": "Enable verbose output",
},
optional=["job"],
)
def dry_act_push(context, dryrun=False, job=None, list=False, verbose=False):
"""
Trigger all `push` Github Action workflows on the current branch.
"""
flags = [
f"--dryrun={str(dryrun).lower()}",
f"--env DEFAULT_WORKSPACE={rootdir}",
f"--env MEGALINTER_VOLUME_ROOT={rootdir}",
f"--list={str(list).lower()}",
f"--verbose={str(verbose).lower()}",
f"--secret GITHUB_TOKEN={os.environ['GITHUB_TOKEN']}",
]
if job:
context.run(f"act push --job={job} {' '.join(flags)}")
else:
context.run(f"act push {' '.join(flags)}")
dry_col = Collection("dry", dry)
dry_col.add_task(dry_release)
dry_act_col = Collection("act", dry_act)
dry_act_col.add_task(dry_act_pull_request)
dry_act_col.add_task(dry_act_push)
dry_col.add_collection(dry_act_col)
ns.add_collection(dry_col)
# === Update ===
@task(pre=[_pre], default=True, name="all")
def update(context):
"""
Run all `update` tasks.
"""
update_niv(context)
update_npm(context)
update_poetry(context)
@task(pre=[_pre], name="niv")
def update_niv(context):
"""
Update niv dependencies.
"""
context.run("niv update niv; niv update nixpkgs")
@task(pre=[_pre], name="npm")
def update_npm(context):
"""
Update npm packages.
"""
context.run("npm run update")
@task(pre=[_pre], name="poetry")
def update_poetry(context):
"""
Update python packages
"""
context.run("poetry update")
context.run("poetry install")
update_col = Collection("update", update)
update_col.add_task(update_niv)
update_col.add_task(update_npm)
update_col.add_task(update_poetry)
ns.add_collection(update_col)
# =============
# === Tasks ===
# =============
# === Clean ===
@task()
def clean(context):
"""
Remove build artifacts, downloaded dependencies,
and generated files.
"""
context.run("git clean -Xdf")
context.run("rm -rf ./.github/linters/*")
ns.add_task(clean)
# === Code ===
@task()
def code(context):
"""
Launch Visual Studio Code.
"""
context.run("code .")
ns.add_task(code)
# === Init ===
@task()
def init(context):
"""
Initialize build dependencies.
"""
context.run("npm install")
generate(context)
ns.add_task(init)
# === Lint ===
@task(pre=[_pre], help={"format": "Apply formatters and fixes in linted sources"})
def lint(context, format=False):
"""
Run all `mega-linter` linters. Apply fixes via
corresponding formatters via the `format` flag.
"""
context.run("rm -rf ./report")
context.run("set +e")
context.run(
f"npm run lint -- -e 'MEGALINTER_VOLUME_ROOT={rootdir}' --fix={str(format).lower()}"
)
context.run("set -e")
# Detached head state in git after running MegaLinter
# https://github.com/nvuillam/mega-linter/issues/604
commit = os.environ["PROJECT_COMMIT"]
context.run(f"git checkout -m {commit}")
context.run("sudo chown -R $(whoami) ./report")
ns.add_task(lint)