Skip to content

Commit 93bec37

Browse files
committed
Add dependencies to uv in post_gen hook
1 parent 8b4e277 commit 93bec37

File tree

5 files changed

+72
-20
lines changed

5 files changed

+72
-20
lines changed

Diff for: hooks/post_gen_project.py

+47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import json
2+
import os
23
import random
34
import shutil
45
import string
6+
import subprocess
7+
import sys
58
from pathlib import Path
69

710
try:
@@ -502,8 +505,52 @@ def main():
502505
if "{{ cookiecutter.use_async }}".lower() == "n":
503506
remove_async_files()
504507

508+
setup_dependencies()
509+
505510
print(SUCCESS + "Project initialized, keep up the good work!" + TERMINATOR)
506511

507512

513+
def setup_dependencies():
514+
print("Installing python dependencies using uv...")
515+
516+
if "{{ cookiecutter.use_docker }}".lower() == "y":
517+
# Build the Docker service using Docker Compose
518+
try:
519+
subprocess.run(["docker", "compose", "-f", "docker-compose.local.yml", "build", "django"], check=True)
520+
except subprocess.CalledProcessError as e:
521+
print(f"Error building Docker service: {e}", file=sys.stderr)
522+
sys.exit(1)
523+
524+
# Use Docker to run the uv command
525+
uv_cmd = ["docker", "compose", "-f", "docker-compose.local.yml", "run", "--rm", "django", "uv"]
526+
else:
527+
# Use uv command directly
528+
uv_cmd = ["uv"]
529+
530+
# Install production dependencies
531+
try:
532+
subprocess.run(uv_cmd + ["add", "-r", "requirements/production.txt"], check=True)
533+
except subprocess.CalledProcessError as e:
534+
print(f"Error installing production dependencies: {e}", file=sys.stderr)
535+
sys.exit(1)
536+
537+
# Install local (development) dependencies
538+
try:
539+
subprocess.run(uv_cmd + ["add", "--dev", "-r", "requirements/local.txt"], check=True)
540+
except subprocess.CalledProcessError as e:
541+
print(f"Error installing local dependencies: {e}", file=sys.stderr)
542+
sys.exit(1)
543+
544+
# Remove the requirements directory
545+
if os.path.exists("requirements"):
546+
try:
547+
shutil.rmtree("requirements")
548+
except Exception as e:
549+
print(f"Error removing 'requirements' folder: {e}", file=sys.stderr)
550+
sys.exit(1)
551+
552+
print("Setup complete!")
553+
554+
508555
if __name__ == "__main__":
509556
main()

Diff for: tests/test_cookiecutter_generation.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ def _fixture_id(ctx):
150150

151151
def build_files_list(base_path: Path):
152152
"""Build a list containing absolute paths to the generated files."""
153-
return [dirpath / file_path for dirpath, subdirs, files in base_path.walk() for file_path in files]
153+
f = []
154+
for dirpath, subdirs, files in base_path.walk():
155+
if ".venv" in subdirs:
156+
subdirs.remove(".venv")
157+
158+
for file_path in files:
159+
f.append(dirpath / file_path)
160+
return f
154161

155162

156163
def check_paths(paths: Iterable[Path]):

Diff for: tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ envlist = py312,black-template
44

55
[testenv]
66
passenv = AUTOFIXABLE_STYLES
7-
commands = pytest -n auto {posargs:./tests}
7+
commands = pytest --instafail -n auto {posargs:./tests}
88

99
[testenv:black-template]
1010
deps = black

Diff for: uv.lock

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: {{cookiecutter.project_slug}}/requirements/local.txt

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
-r production.txt
2-
31
Werkzeug[watchdog]==3.1.3 # https://github.com/pallets/werkzeug
42
ipdb==0.13.13 # https://github.com/gotcha/ipdb
53
{%- if cookiecutter.use_docker == 'y' %}

0 commit comments

Comments
 (0)