From c0b26c911809e4a01211c6cd7bea9b8cd01a5ea3 Mon Sep 17 00:00:00 2001 From: Jelmer Draaijer Date: Sun, 27 Oct 2024 11:40:32 +0100 Subject: [PATCH] Remove uv-pre-commit from pre-commit in post gen hook when heroku is not used --- hooks/post_gen_project.py | 20 ++++++++++++------- tests/test_cookiecutter_generation.py | 12 +++++++++++ .../.pre-commit-config.yaml | 4 ++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index d4a40efa..c706d9ab 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -16,6 +16,7 @@ import os import random import shutil import string +from pathlib import Path try: # Inspired by @@ -218,21 +219,25 @@ def handle_js_runner(choice, use_docker, use_async): def remove_prettier_pre_commit(): - with open(".pre-commit-config.yaml", "r") as fd: - content = fd.readlines() + remove_repo_from_pre_commit_config("mirrors-prettier") + + +def remove_repo_from_pre_commit_config(repo_to_remove: str): + pre_commit_config = Path(".pre-commit-config.yaml") + content = pre_commit_config.read_text().splitlines(True) removing = False - new_lines = [] + new_lines = "" + for line in content: if removing and "- repo:" in line: removing = False - if "mirrors-prettier" in line: + if repo_to_remove in line: removing = True if not removing: - new_lines.append(line) + new_lines += line - with open(".pre-commit-config.yaml", "w") as fd: - fd.writelines(new_lines) + pre_commit_config.write_text(new_lines) def remove_celery_files(): @@ -471,6 +476,7 @@ def main(): if "{{ cookiecutter.use_heroku }}".lower() == "n": remove_heroku_files() + remove_repo_from_pre_commit_config("uv-pre-commit") if "{{ cookiecutter.use_docker }}".lower() == "n" and "{{ cookiecutter.use_heroku }}".lower() == "n": if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y": diff --git a/tests/test_cookiecutter_generation.py b/tests/test_cookiecutter_generation.py index 7ad8bffa..1a327737 100755 --- a/tests/test_cookiecutter_generation.py +++ b/tests/test_cookiecutter_generation.py @@ -437,3 +437,15 @@ def test_pyproject_toml(cookies, context): assert data["project"]["authors"][0]["email"] == author_email assert data["project"]["authors"][0]["name"] == author_name assert data["project"]["name"] == context["project_slug"] + + +def test_pre_commit_without_heroku(cookies, context): + context.update({"use_heroku": "n"}) + result = cookies.bake(extra_context=context) + assert result.exit_code == 0 + + pre_commit_config = result.project_path / ".pre-commit-config.yaml" + + data = pre_commit_config.read_text() + + assert "uv-pre-commit" not in data diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 6efb7fb0..dc8b08a2 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -48,14 +48,14 @@ repos: hooks: - id: djlint-reformat-django - id: djlint-django -{%- if cookiecutter.use_heroku == "y" %} + - repo: https://github.com/astral-sh/uv-pre-commit # uv version. rev: 0.4.15 hooks: - id: uv-export args: ['--frozen', '--no-dev'] -{%- endif -%} + # sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date ci: autoupdate_schedule: weekly