Skip to content

Commit 78ca05f

Browse files
committed
feat: generate script using jinja
1 parent b931e71 commit 78ca05f

File tree

8 files changed

+81
-4
lines changed

8 files changed

+81
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
- "-l DEBUG"
88
- "-g dev"
99
repo: https://github.com/phi-friday/sync-uv-pre-commit
10-
rev: v0.6.3
10+
rev: v0.6.4
1111

1212
- hooks:
1313
- id: ruff

.pre-commit-hooks.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
- id: sync-uv-pre-commit
22
name: Sync uv and pre commit
33
description: Sync uv and pre commit
4-
language: python
5-
entry: "sync_uv_pre_commit"
4+
language: script
5+
entry: "uv run script.py"
66
minimum_pre_commit_version: "3.5.0"
77
require_serial: true
88
always_run: true

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ classifiers = [
1212
"Programming Language :: Python :: 3.12",
1313
"Programming Language :: Python :: Implementation :: CPython",
1414
]
15-
scripts = { sync_uv_pre_commit = "sync_uv_pre_commit.cli:main" }
15+
scripts = { generate_cli_script = "sync_uv_pre_commit.render:generate_script" }
1616
dependencies = [
1717
"packaging",
1818
"pre-commit>=3.5.0",
@@ -23,6 +23,7 @@ dependencies = [
2323
dev = [
2424
"ruff==0.8.4",
2525
"poethepoet>=0.27.0",
26+
"jinja2>=3.1.5",
2627
]
2728

2829
[tool.uv]

script.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# /// script
2+
# requires-python = ">=3.13"
3+
# dependencies = [
4+
# "packaging",
5+
# "pre-commit>=3.5.0",
6+
# "tomlkit>=0.13.2",
7+
# "typing-extensions>=4.12.2",
8+
# ]
9+
# ///
10+
11+
from __future__ import annotations
12+
13+
import sys
14+
from pathlib import Path
15+
16+
WORKSPACE = Path(__file__).parent
17+
sys.path.append(str(WORKSPACE / "src"))
18+
19+
20+
if __name__ == "__main__":
21+
from sync_uv_pre_commit.cli import main
22+
23+
main()

src/sync_uv_pre_commit/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def check_uv_version() -> None:
227227
sys.exit(ExitCode.PARSING)
228228
sys.exit(ExitCode.UNKNOWN)
229229

230+
logger.info("python version: %s", sys.version_info)
230231
version = process.stdout.strip().split()[1]
231232
logger.info("uv version: %s", version)
232233

src/sync_uv_pre_commit/export.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
WORKSPACE = Path(__file__).parent
7+
sys.path.append(str(WORKSPACE / "src"))
8+
9+
10+
if __name__ == "__main__":
11+
from sync_uv_pre_commit.cli import main
12+
13+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# /// script
2+
# requires-python = "{{ requires_python }}"
3+
# dependencies = [
4+
{% for dependency in dependencies -%}
5+
# "{{ dependency }}",
6+
{% endfor -%}
7+
# ]
8+
# ///
9+
10+
{{ export_script }}

src/sync_uv_pre_commit/render.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
import jinja2
6+
7+
from sync_uv_pre_commit.toml import read_pyproject
8+
9+
10+
def generate_script() -> None:
11+
"""Generate the script.py file."""
12+
pyproject_file = Path(__file__).parent.parent.parent / "pyproject.toml"
13+
export_template_file = Path(__file__).parent / "export.py.j2"
14+
export_script_file = Path(__file__).parent / "export.py"
15+
output = Path(__file__).parent.parent.parent / "script.py"
16+
17+
pyproject = read_pyproject(pyproject_file)
18+
with export_template_file.open("r") as f:
19+
template = jinja2.Template(f.read())
20+
with export_script_file.open("r") as f:
21+
export_script = f.read()
22+
23+
script = template.render(
24+
requires_python=">=3.13",
25+
dependencies=pyproject["project"]["dependencies"],
26+
export_script=export_script,
27+
)
28+
with output.open("w+") as f:
29+
f.write(script)

0 commit comments

Comments
 (0)