Remove uv-pre-commit from pre-commit in post gen hook when heroku is not used

This commit is contained in:
Jelmer Draaijer 2024-10-27 11:40:32 +01:00
parent 9978e3f35d
commit c0b26c9118
3 changed files with 27 additions and 9 deletions

View File

@ -16,6 +16,7 @@ import os
import random import random
import shutil import shutil
import string import string
from pathlib import Path
try: try:
# Inspired by # Inspired by
@ -218,21 +219,25 @@ def handle_js_runner(choice, use_docker, use_async):
def remove_prettier_pre_commit(): def remove_prettier_pre_commit():
with open(".pre-commit-config.yaml", "r") as fd: remove_repo_from_pre_commit_config("mirrors-prettier")
content = fd.readlines()
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 removing = False
new_lines = [] new_lines = ""
for line in content: for line in content:
if removing and "- repo:" in line: if removing and "- repo:" in line:
removing = False removing = False
if "mirrors-prettier" in line: if repo_to_remove in line:
removing = True removing = True
if not removing: if not removing:
new_lines.append(line) new_lines += line
with open(".pre-commit-config.yaml", "w") as fd: pre_commit_config.write_text(new_lines)
fd.writelines(new_lines)
def remove_celery_files(): def remove_celery_files():
@ -471,6 +476,7 @@ def main():
if "{{ cookiecutter.use_heroku }}".lower() == "n": if "{{ cookiecutter.use_heroku }}".lower() == "n":
remove_heroku_files() 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.use_docker }}".lower() == "n" and "{{ cookiecutter.use_heroku }}".lower() == "n":
if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y": if "{{ cookiecutter.keep_local_envs_in_vcs }}".lower() == "y":

View File

@ -437,3 +437,15 @@ def test_pyproject_toml(cookies, context):
assert data["project"]["authors"][0]["email"] == author_email assert data["project"]["authors"][0]["email"] == author_email
assert data["project"]["authors"][0]["name"] == author_name assert data["project"]["authors"][0]["name"] == author_name
assert data["project"]["name"] == context["project_slug"] 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

View File

@ -48,14 +48,14 @@ repos:
hooks: hooks:
- id: djlint-reformat-django - id: djlint-reformat-django
- id: djlint-django - id: djlint-django
{%- if cookiecutter.use_heroku == "y" %}
- repo: https://github.com/astral-sh/uv-pre-commit - repo: https://github.com/astral-sh/uv-pre-commit
# uv version. # uv version.
rev: 0.4.15 rev: 0.4.15
hooks: hooks:
- id: uv-export - id: uv-export
args: ['--frozen', '--no-dev'] args: ['--frozen', '--no-dev']
{%- endif -%}
# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date # sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci: ci:
autoupdate_schedule: weekly autoupdate_schedule: weekly