-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
97 lines (76 loc) · 2.94 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
import logging
import os
from inspect import cleandoc
from pathlib import Path
from shutil import which
from invoke import task
logger = logging.getLogger(__name__)
PKG_NAME = "pelimoji"
PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}")
ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs"))
VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME.expanduser() / PKG_NAME)
VENV = str(VENV_PATH.expanduser())
BIN_DIR = "bin" if os.name != "nt" else "Scripts"
VENV_BIN = Path(VENV) / Path(BIN_DIR)
TOOLS = ("poetry", "pre-commit")
POETRY = which("poetry") if which("poetry") else (VENV_BIN / "poetry")
CMD_PREFIX = f"{VENV_BIN}/" if ACTIVE_VENV else f"{POETRY} run "
PRECOMMIT = which("pre-commit") if which("pre-commit") else f"{CMD_PREFIX}pre-commit"
PTY = os.name != "nt"
@task
def tests(c, deprecations=False):
"""Run the test suite, optionally with `--deprecations`."""
deprecations_flag = "" if deprecations else "-W ignore::DeprecationWarning"
c.run(f"{CMD_PREFIX}pytest {deprecations_flag}", pty=PTY)
@task
def black(c, check=False, diff=False):
"""Run Black auto-formatter, optionally with `--check` or `--diff`."""
check_flag, diff_flag = "", ""
if check:
check_flag = "--check"
if diff:
diff_flag = "--diff"
c.run(f"{CMD_PREFIX}black {check_flag} {diff_flag} {PKG_PATH} tasks.py")
@task
def ruff(c, fix=False, diff=False):
"""Run Ruff to ensure code meets project standards."""
diff_flag, fix_flag = "", ""
if fix:
fix_flag = "--fix"
if diff:
diff_flag = "--diff"
c.run(f"{CMD_PREFIX}ruff check {diff_flag} {fix_flag} .")
@task
def lint(c, fix=False, diff=False):
"""Check code style via linting tools."""
ruff(c, fix=fix, diff=diff)
black(c, check=(not fix), diff=diff)
@task
def tools(c):
"""Install development tools in the virtual environment if not already on PATH."""
for tool in TOOLS:
if not which(tool):
c.run(f"{CMD_PREFIX}pip install {tool}")
@task
def precommit(c):
"""Install pre-commit hooks to .git/hooks/pre-commit."""
c.run(f"{PRECOMMIT} install")
@task
def setup(c):
"""Set up the development environment."""
if which("poetry") or ACTIVE_VENV:
tools(c)
c.run(f"{CMD_PREFIX}python -m pip install --upgrade pip")
c.run(f"{POETRY} install")
precommit(c)
logger.info("\nDevelopment environment should now be set up and ready!\n")
else:
error_message = """
Poetry is not installed, and there is no active virtual environment available.
You can either manually create and activate a virtual environment, or you can
install Poetry via:
curl -sSL https://install.python-poetry.org | python3 -
Once you have taken one of the above two steps, run `invoke setup` again.
""" # noqa: E501
raise SystemExit(cleandoc(error_message))