Files
python-cli-template/hooks/post_gen_project.py
2026-01-19 21:16:56 +01:00

65 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Post-generation hook for the Python library template."""
import subprocess
import sys
from pathlib import Path
def run_command(cmd: list[str], description: str) -> None:
"""Run a command and handle errors."""
print(f"Running: {description}")
try:
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
if result.stdout:
print(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error running {description}: {e}")
if e.stderr:
print(f"Error output: {e.stderr}")
sys.exit(1)
def main():
"""Initialize the generated project."""
project_dir = Path.cwd()
print(f"Setting up project in: {project_dir}")
# Initialize git repository
run_command(["git", "init"], "git init")
# Sync dependencies with uv
run_command(["uv", "sync", "--group", "dev"], "uv sync --group dev")
# Run initial formatting and linting
run_command(["uv", "run", "ruff", "format", "src", "tests"], "ruff format")
run_command(
["uv", "run", "ruff", "check", "--fix", "src", "tests"],
"ruff check --fix",
)
# Run type checking
try:
run_command(["uv", "run", "mypy", "src"], "mypy type checking")
except SystemExit:
# mypy might fail on initial template, continue anyway
print("MyPy check failed - this is normal for initial template")
# Run tests to ensure everything works
try:
run_command(["uv", "run", "pytest"], "pytest")
except SystemExit:
print("Tests failed - you may need to adjust the generated code")
print("\n✅ Project setup complete!")
print("Next steps:")
print("1. Review and customize the generated files")
print("2. Add your actual dependencies to pyproject.toml")
print("3. Implement your core logic in src/")
print("4. Update tests as needed")
print("5. Update README.md with proper documentation")
if __name__ == "__main__":
main()