Skip to content

Commit

Permalink
feat: generate script using jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Dec 26, 2024
1 parent b931e71 commit 78ca05f
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- "-l DEBUG"
- "-g dev"
repo: https://github.com/phi-friday/sync-uv-pre-commit
rev: v0.6.3
rev: v0.6.4

- hooks:
- id: ruff
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- id: sync-uv-pre-commit
name: Sync uv and pre commit
description: Sync uv and pre commit
language: python
entry: "sync_uv_pre_commit"
language: script
entry: "uv run script.py"
minimum_pre_commit_version: "3.5.0"
require_serial: true
always_run: true
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
]
scripts = { sync_uv_pre_commit = "sync_uv_pre_commit.cli:main" }
scripts = { generate_cli_script = "sync_uv_pre_commit.render:generate_script" }
dependencies = [
"packaging",
"pre-commit>=3.5.0",
Expand All @@ -23,6 +23,7 @@ dependencies = [
dev = [
"ruff==0.8.4",
"poethepoet>=0.27.0",
"jinja2>=3.1.5",
]

[tool.uv]
Expand Down
23 changes: 23 additions & 0 deletions script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "packaging",
# "pre-commit>=3.5.0",
# "tomlkit>=0.13.2",
# "typing-extensions>=4.12.2",
# ]
# ///

from __future__ import annotations

import sys
from pathlib import Path

WORKSPACE = Path(__file__).parent
sys.path.append(str(WORKSPACE / "src"))


if __name__ == "__main__":
from sync_uv_pre_commit.cli import main

main()
1 change: 1 addition & 0 deletions src/sync_uv_pre_commit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def check_uv_version() -> None:
sys.exit(ExitCode.PARSING)
sys.exit(ExitCode.UNKNOWN)

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

Expand Down
13 changes: 13 additions & 0 deletions src/sync_uv_pre_commit/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

import sys
from pathlib import Path

WORKSPACE = Path(__file__).parent
sys.path.append(str(WORKSPACE / "src"))


if __name__ == "__main__":
from sync_uv_pre_commit.cli import main

main()
10 changes: 10 additions & 0 deletions src/sync_uv_pre_commit/export.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# /// script
# requires-python = "{{ requires_python }}"
# dependencies = [
{% for dependency in dependencies -%}
# "{{ dependency }}",
{% endfor -%}
# ]
# ///

{{ export_script }}
29 changes: 29 additions & 0 deletions src/sync_uv_pre_commit/render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from pathlib import Path

import jinja2

from sync_uv_pre_commit.toml import read_pyproject


def generate_script() -> None:
"""Generate the script.py file."""
pyproject_file = Path(__file__).parent.parent.parent / "pyproject.toml"
export_template_file = Path(__file__).parent / "export.py.j2"
export_script_file = Path(__file__).parent / "export.py"
output = Path(__file__).parent.parent.parent / "script.py"

pyproject = read_pyproject(pyproject_file)
with export_template_file.open("r") as f:
template = jinja2.Template(f.read())
with export_script_file.open("r") as f:
export_script = f.read()

script = template.render(
requires_python=">=3.13",
dependencies=pyproject["project"]["dependencies"],
export_script=export_script,
)
with output.open("w+") as f:
f.write(script)

0 comments on commit 78ca05f

Please sign in to comment.