| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Run code formatters |
| 19 |
~~~~~~~~~~~~~~~~~~~ |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
import os as _os |
| 24 |
|
| 25 |
import invoke as _invoke |
| 26 |
|
| 27 |
from . import _features |
| 28 |
from ._inv import tasks as _tasks |
| 29 |
|
| 30 |
_FORMATTERS = [] |
| 31 |
|
| 32 |
|
| 33 |
@_tasks.optional(_FORMATTERS, _features.isort) |
| 34 |
@_invoke.task() |
| 35 |
def isort(ctx, diff=False): |
| 36 |
""" |
| 37 |
Format python code using isort formatter |
| 38 |
|
| 39 |
Parameters: |
| 40 |
diff (bool): |
| 41 |
Just emit a diff instead of changing files inline? Default: false |
| 42 |
""" |
| 43 |
cmd = [ctx.which("isort")] |
| 44 |
if diff: |
| 45 |
cmd += ctx.s("--diff --color") |
| 46 |
cmd += ctx.s("--settings-path pyproject.toml") |
| 47 |
cmd += ["tasks"] |
| 48 |
if ctx.get("package"): |
| 49 |
cmd += [ctx.package] |
| 50 |
if _os.path.exists(ctx.shell.native("tests")): |
| 51 |
cmd += ["tests"] |
| 52 |
|
| 53 |
with ctx.shell.root_dir(): |
| 54 |
ctx.run(ctx.c(cmd), echo=True) |
| 55 |
|
| 56 |
|
| 57 |
@_tasks.optional(_FORMATTERS, _features.black) |
| 58 |
@_invoke.task() |
| 59 |
def black(ctx, diff=False): |
| 60 |
""" |
| 61 |
Format python code using Black formatter |
| 62 |
|
| 63 |
Parameters: |
| 64 |
diff (bool): |
| 65 |
Just emit a diff instead of changing files inline? Default: false |
| 66 |
""" |
| 67 |
cmd = [ctx.which("black")] |
| 68 |
if diff: |
| 69 |
cmd += ctx.s("--diff --color") |
| 70 |
cmd += ctx.s("--config pyproject.toml .") |
| 71 |
|
| 72 |
with ctx.shell.root_dir(): |
| 73 |
ctx.run(ctx.c(cmd), echo=True) |
| 74 |
|
| 75 |
|
| 76 |
@_tasks.optional(_FORMATTERS, _features.terraform) |
| 77 |
@_invoke.task(name="tf") |
| 78 |
def terraform(ctx): |
| 79 |
"""Format terraform code""" |
| 80 |
base_cmd = [ctx.which("terraform"), "-chdir=" + ctx.paths.terraform] |
| 81 |
with ctx.shell.root_dir(): |
| 82 |
ctx.run(ctx.c(base_cmd + ctx.s("fmt -recursive")), echo=True) |
| 83 |
|
| 84 |
|
| 85 |
@_tasks.optional(_FORMATTERS) |
| 86 |
@_invoke.task(name="all", default=True) |
| 87 |
def all_(ctx): |
| 88 |
"""Run all formatters""" |
| 89 |
_tasks.execute(ctx, *["format." + formatter for formatter in _FORMATTERS]) |