Skip to content

Commit 1898c5c

Browse files
refactor: move defaults for pre-commit and commitizen to templates
]
1 parent 4195b06 commit 1898c5c

File tree

6 files changed

+69
-98
lines changed

6 files changed

+69
-98
lines changed

afterpython/.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# default is only ["pre-commit"], without changing the default,
2+
# we need to explicitly pass in --hook-type whenever we run "pre-commit install --hook-type ..."
13
default_install_hook_types:
24
- pre-commit
35
- commit-msg

src/afterpython/cli/commands/init.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,13 @@
77

88

99
def init_ruff_toml():
10-
from afterpython.tools.pre_commit import update_pre_commit
11-
12-
afterpython_path = ap.paths.afterpython_path
13-
ruff_toml_path = afterpython_path / "ruff.toml"
10+
ruff_toml_path = ap.paths.afterpython_path / "ruff.toml"
1411
if ruff_toml_path.exists():
1512
click.echo(f"Ruff configuration file {ruff_toml_path} already exists")
1613
return
1714
ruff_template_path = ap.paths.templates_path / "ruff-template.toml"
1815
shutil.copy(ruff_template_path, ruff_toml_path)
1916
click.echo(f"Created {ruff_toml_path}")
20-
# add ruff-pre-commit hook to .pre-commit-config.yaml
21-
data_update = {
22-
"repos": [
23-
{
24-
"repo": "https://github.com/astral-sh/ruff-pre-commit",
25-
"rev": "v0.14.6",
26-
"hooks": [
27-
{
28-
"id": "ruff-check",
29-
"stages": ["pre-commit"],
30-
"args": ["--config", "./afterpython/ruff.toml"],
31-
},
32-
{
33-
"id": "ruff-format",
34-
"stages": ["pre-commit"],
35-
"args": ["--config", "./afterpython/ruff.toml"],
36-
},
37-
],
38-
},
39-
]
40-
}
41-
update_pre_commit(data_update)
4217

4318

4419
def init_website():
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tool.commitizen]
2+
name = "cz_conventional_commits"
3+
# name = "cz_customize"
4+
tag_format = "v$version"
5+
version_scheme = "pep440"
6+
version_provider = "uv"
7+
update_changelog_on_bump = false
8+
major_version_zero = true
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
default_install_hook_types:
2+
- pre-commit
3+
- commit-msg
4+
- pre-push
5+
exclude: ^afterpython/_website/
6+
repos:
7+
- repo: https://github.com/pre-commit/pre-commit-hooks
8+
rev: v6.0.0
9+
hooks:
10+
- id: trailing-whitespace
11+
stages:
12+
- pre-commit
13+
- id: end-of-file-fixer
14+
stages:
15+
- pre-commit
16+
- id: check-yaml
17+
stages:
18+
- pre-commit
19+
- id: check-toml
20+
stages:
21+
- pre-commit
22+
- id: check-added-large-files
23+
stages:
24+
- pre-commit
25+
- repo: https://github.com/astral-sh/ruff-pre-commit
26+
rev: v0.14.6
27+
hooks:
28+
- id: ruff-check
29+
stages:
30+
- pre-commit
31+
args:
32+
- --config
33+
- ./afterpython/ruff.toml
34+
- id: ruff-format
35+
stages:
36+
- pre-commit
37+
args:
38+
- --config
39+
- ./afterpython/ruff.toml
40+
- repo: https://github.com/commitizen-tools/commitizen
41+
rev: v4.10.0
42+
hooks:
43+
- id: commitizen
44+
stages:
45+
- commit-msg
46+
- id: commitizen-branch
47+
stages:
48+
- pre-push
Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
1-
import afterpython as ap
2-
3-
commitizen_default = {
4-
"tool": {
5-
"commitizen": {
6-
"name": "cz_conventional_commits",
7-
"tag_format": "v$version",
8-
"version_scheme": "pep440",
9-
"version_provider": "uv",
10-
"update_changelog_on_bump": False,
11-
"major_version_zero": True,
12-
}
13-
}
14-
}
1+
import shutil
152

16-
17-
commitizen_pre_commit_hook = {
18-
"repos": [
19-
{
20-
"repo": "https://github.com/commitizen-tools/commitizen",
21-
"rev": "v4.10.0",
22-
"hooks": [
23-
{
24-
"id": "commitizen",
25-
"stages": ["commit-msg"],
26-
},
27-
{
28-
"id": "commitizen-branch",
29-
"stages": ["pre-push"],
30-
},
31-
],
32-
},
33-
]
34-
}
3+
import afterpython as ap
354

365

376
def init_commitizen():
38-
from afterpython._io.toml import write_toml
39-
from afterpython.tools.pre_commit import update_pre_commit
40-
41-
afterpython_path = ap.paths.afterpython_path
42-
commitizen_toml_path = afterpython_path / "cz.toml"
7+
commitizen_toml_path = ap.paths.afterpython_path / "cz.toml"
438
if commitizen_toml_path.exists():
449
print(f"Commitizen configuration file {commitizen_toml_path} already exists")
4510
return
46-
47-
# Create commitizen configuration with default values
48-
write_toml(commitizen_toml_path, commitizen_default)
11+
cz_template_path = ap.paths.templates_path / "cz-template.toml"
12+
shutil.copy(cz_template_path, commitizen_toml_path)
4913
print(f"Created {commitizen_toml_path}")
50-
51-
# add commitizen hook to .pre-commit-config.yaml
52-
update_pre_commit(commitizen_pre_commit_hook)
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
1+
import shutil
12
import subprocess
23

34
import afterpython as ap
45
from afterpython._io.yaml import read_yaml, write_yaml
56

6-
pre_commit_default = {
7-
# default is only ["pre-commit"], without changing the default,
8-
# we need to explicitly pass in --hook-type whenever we run "pre-commit install --hook-type ..."
9-
"default_install_hook_types": ["pre-commit", "commit-msg", "pre-push"],
10-
"exclude": "^afterpython/_website/", # do not check the project website template
11-
"repos": [
12-
{
13-
"repo": "https://github.com/pre-commit/pre-commit-hooks",
14-
"rev": "v6.0.0",
15-
"hooks": [
16-
{"id": "trailing-whitespace", "stages": ["pre-commit"]},
17-
{"id": "end-of-file-fixer", "stages": ["pre-commit"]},
18-
{"id": "check-yaml", "stages": ["pre-commit"]},
19-
{"id": "check-toml", "stages": ["pre-commit"]},
20-
{"id": "check-added-large-files", "stages": ["pre-commit"]},
21-
],
22-
},
23-
# {
24-
# "repo": "https://github.com/astral-sh/uv-pre-commit",
25-
# "rev": "0.9.8",
26-
# "hooks": [
27-
# {"id": "uv-lock"}, # Update the uv lockfile
28-
# ]
29-
# },
30-
],
31-
}
32-
337

348
def install_pre_commit():
359
# installed in .git/hooks
@@ -55,6 +29,9 @@ def init_pre_commit():
5529
if pre_commit_path.exists():
5630
print(f".pre-commit-config.yaml already exists at {pre_commit_path}")
5731
return
58-
write_yaml(pre_commit_path, pre_commit_default)
32+
pre_commit_template_path = (
33+
ap.paths.templates_path / "pre-commit-config-template.yaml"
34+
)
35+
shutil.copy(pre_commit_template_path, pre_commit_path)
5936
print(f"Created {pre_commit_path}")
6037
install_pre_commit()

0 commit comments

Comments
 (0)