-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
executable file
·406 lines (294 loc) · 9.04 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import os, pathlib, platform, subprocess
from invoke import task
from invoke.collection import Collection
from invoke.config import Config
from scripts import config
from scripts import fingerprint
from scripts import linters
from scripts.lib import file as xfile
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) and os.environ.get("INIT") == "True":
config.generate_config(rootdir)
cfg = Config({"stage": "development"})
cfg.set_runtime_path(cfgpath)
cfg.load_runtime()
ns = Collection()
ns.configure(cfg)
@task(name="refresh")
def _refresh(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()
if fingerprint.has_file_changed(rootdir, "package.json"):
context.run("npm install")
if fingerprint.has_file_changed(rootdir, "pyproject.toml"):
context.run("poetry install")
generate(context)
ns.add_task(_refresh)
# ===================
# === Collections ===
# ===================
# === Generate ===
# generate.all (generate)
@task(default=True, name="all")
def generate(context):
"""
Trigger all generators.
"""
generate_config(context)
generate_linters(context)
generate_col = Collection("generate", generate)
ns.add_collection(generate_col)
# generate.config
@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)
generate_col.add_task(generate_config)
# generate.linters
@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.add_task(generate_linters)
# === Dry ===
# dry.all (dry)
@task(pre=[_refresh], default=True, name="all")
def dry(context):
"""
Run all `dry` tasks.
"""
dry_release(context)
dry_act(context)
dry_col = Collection("dry", dry)
ns.add_collection(dry_col)
# dry.release
@task(pre=[_refresh], name="release")
def dry_release(context):
"""
Trigger a new git dry run release via `semantic-release`.
"""
context.run("npm run release-dry-run")
dry_col.add_task(dry_release)
# dry.act.all (dry.act)
@task(pre=[_refresh], default=True, name="all")
def dry_act(context):
"""
Trigger all Github Actions dry-runs.
"""
dry_act_pull_request(context)
dry_act_push(context)
dry_act_col = Collection("act", dry_act)
dry_col.add_collection(dry_act_col)
# dry.act.pull-request
@task(
pre=[_refresh],
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)}")
dry_act_col.add_task(dry_act_pull_request)
# dry.act.push
@task(
pre=[_refresh],
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_act_col.add_task(dry_act_push)
# === Init ===
# init.all (init)
@task(pre=[_refresh], default=True, name="all")
def init(context):
"""
Run all `init` tasks.
"""
init_git(context)
init_tree(context)
init_col = Collection("init", init)
ns.add_collection(init_col)
# init.git
@task(pre=[_refresh], name="git")
def init_git(context):
"""
Initialize all `git` scope modifications.
"""
context.run("git flow init")
context.run("git config gitflow.path.hooks .husky")
init_col.add_task(init_git)
# init.tree
@task(post=[_refresh], name="tree")
def init_tree(context):
"""
Create any files / directories required prior to development.
"""
# Misc. directories
tmpdir = os.path.join(rootdir, "tmp")
fingerprintdir = os.path.join(tmpdir, "fingerprint")
reportdir = os.path.join(tmpdir, "report")
dirs = [
{"path": tmpdir},
{"path": fingerprintdir},
{"path": reportdir},
]
for dir in dirs:
pathlib.Path(dir["path"]).mkdir(parents=True, exist_ok=True)
# Settings files
development_settings = os.path.join(rootdir, "settings", "development.json")
staging_settings = os.path.join(rootdir, "settings", "staging.json")
production_settings = os.path.join(rootdir, "settings", "production.json")
settings_files = [
{"path": development_settings, "stage": "development"},
{"path": staging_settings, "stage": "staging"},
{"path": production_settings, "stage": "production"},
]
for file in settings_files:
xfile.overwrite(file["path"], f'{{"stage": "{file["stage"]}"}}')
init_col.add_task(init_tree)
# === Update ===
# update.all
@task(post=[_refresh], default=True, name="all")
def update(context):
"""
Run all `update` tasks.
"""
update_niv(context)
update_npm(context)
update_poetry(context)
update_col = Collection("update", update)
ns.add_collection(update_col)
# update.niv
@task(post=[_refresh], name="niv")
def update_niv(context):
"""
Update niv dependencies.
"""
context.run("niv update niv; niv update nixpkgs")
update_col.add_task(update_niv)
# update.npm
@task(pre=[_refresh], name="npm")
def update_npm(context):
"""
Update npm packages.
"""
context.run("npm run update")
update_col.add_task(update_npm)
# update.poetry
@task(pre=[_refresh], name="poetry")
def update_poetry(context):
"""
Update python packages
"""
context.run("poetry update")
context.run("poetry install")
update_col.add_task(update_poetry)
# =============
# === 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)
# === Lint ===
@task(pre=[_refresh], 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.
"""
reportdir = os.path.join(rootdir, "tmp", "report")
reportdir_remote = os.path.join("tmp", "lint", "tmp", "report")
context.run(f"rm -rf {reportdir}")
flags = [
f"--env COPYPASTE_JSCPD_ARGUMENTS='--output=./tmp/report/jscpd'",
f"--env MEGALINTER_VOLUME_ROOT={rootdir}",
f"--env REPORT_OUTPUT_FOLDER={reportdir_remote}",
f"--fix={str(format).lower()}",
]
# Run `mega-linter` in a sub-shell to prevent instances
# of exit failure (exit 1) from breaking subsequent commands.
context.run(f"npm run lint -- {' '.join(flags)} &")
# 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(f"sudo chown -R $(whoami) {reportdir}")
ns.add_task(lint)