Skip to content

Commit 613898b

Browse files
committed
feat: add hypermodern cookiecutter logic for installing pre-commit-hooks
1 parent 107564d commit 613898b

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

{{cookiecutter.project_name}}/noxfile.py

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"""Noxfile for the {{cookiecutter.project_name}} project."""
2+
3+
import os
4+
import shlex
5+
26
from pathlib import Path
7+
from textwrap import dedent
38
from typing import List
49

510
import nox
@@ -23,12 +28,88 @@
2328
PACKAGE_NAME: str = "{{cookiecutter.package_name}}"
2429

2530

31+
def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
32+
"""Activate virtualenv in hooks installed by pre-commit.
33+
34+
This function patches git hooks installed by pre-commit to activate the
35+
session's virtual environment. This allows pre-commit to locate hooks in
36+
that environment when invoked from git.
37+
38+
Args:
39+
session: The Session object.
40+
"""
41+
assert session.bin is not None # nosec
42+
43+
# Only patch hooks containing a reference to this session's bindir. Support
44+
# quoting rules for Python and bash, but strip the outermost quotes so we
45+
# can detect paths within the bindir, like <bindir>/python.
46+
bindirs = [
47+
bindir[1:-1] if bindir[0] in "'\"" else bindir for bindir in (repr(session.bin), shlex.quote(session.bin))
48+
]
49+
50+
virtualenv = session.env.get("VIRTUAL_ENV")
51+
if virtualenv is None:
52+
return
53+
54+
headers = {
55+
# pre-commit < 2.16.0
56+
"python": f"""\
57+
import os
58+
os.environ["VIRTUAL_ENV"] = {virtualenv!r}
59+
os.environ["PATH"] = os.pathsep.join((
60+
{session.bin!r},
61+
os.environ.get("PATH", ""),
62+
))
63+
""",
64+
# pre-commit >= 2.16.0
65+
"bash": f"""\
66+
VIRTUAL_ENV={shlex.quote(virtualenv)}
67+
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
68+
""",
69+
# pre-commit >= 2.17.0 on Windows forces sh shebang
70+
"/bin/sh": f"""\
71+
VIRTUAL_ENV={shlex.quote(virtualenv)}
72+
PATH={shlex.quote(session.bin)}"{os.pathsep}$PATH"
73+
""",
74+
}
75+
76+
hookdir: Path = Path(".git") / "hooks"
77+
if not hookdir.is_dir():
78+
return
79+
80+
for hook in hookdir.iterdir():
81+
if hook.name.endswith(".sample") or not hook.is_file():
82+
continue
83+
84+
if not hook.read_bytes().startswith(b"#!"):
85+
continue
86+
87+
text: str = hook.read_text()
88+
89+
if not any((Path("A") == Path("a") and bindir.lower() in text.lower()) or bindir in text for bindir in bindirs):
90+
continue
91+
92+
lines: list[str] = text.splitlines()
93+
94+
for executable, header in headers.items():
95+
if executable in lines[0].lower():
96+
lines.insert(1, dedent(header))
97+
hook.write_text("\n".join(lines))
98+
break
99+
100+
26101
@nox.session(python=DEFAULT_PYTHON_VERSION, name="pre-commit")
27-
def pre_commit(session: Session) -> None:
28-
"""Run pre-commit checks."""
102+
def precommit(session: Session) -> None:
103+
"""Lint using pre-commit."""
104+
args: list[str] = session.posargs or ["run", "--all-files", "--hook-stage=manual", "--show-diff-on-failure"]
105+
29106
session.log("Installing pre-commit dependencies...")
30107
session.install("-e", ".", "--group", "dev")
31108

109+
session.run("pre-commit", *args)
110+
if args and args[0] == "install":
111+
activate_virtualenv_in_precommit_hooks(session)
112+
32113

33114
@nox.session(python=DEFAULT_PYTHON_VERSION, name="format-python")
34115
def format_python(session: Session) -> None:

{{cookiecutter.project_name}}/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dev = [
2727
"commitizen>=4.7.0",
2828
"nox>={{cookiecutter.copyright_year}}.5.1",
2929
"pre-commit>=4.2.0",
30+
"pre-commit-hooks>=5.0.0",
3031
]
3132
docs = [
3233
"furo>=2024.8.6",

0 commit comments

Comments
 (0)