From 682e7a30c6bbbfbb1c2317840eb7bf9ecb37a50b Mon Sep 17 00:00:00 2001 From: bnbong Date: Sat, 13 Sep 2025 18:05:32 +0900 Subject: [PATCH 1/2] [TEMPLATE] add fastapi-single-module template, edit fastsapi-psql-orm template's docker steps --- .pre-commit-config.yaml | 7 ++ scripts/inspect-changed-templates.py | 103 ++++++++++++++++++ .../fastapi-mcp/README.md-tpl | 24 ++-- .../fastapi-psql-orm/Dockerfile-tpl | 5 - .../fastapi-psql-orm/docker-compose.yml-tpl | 2 - .../fastapi-single-module/README.md-tpl | 37 +++++++ .../fastapi-single-module/pyproject.toml-tpl | 27 +++++ .../requirements.txt-tpl | 6 + .../fastapi-single-module/setup.cfg-tpl | 4 + .../fastapi-single-module/setup.py-tpl | 17 +++ .../fastapi-single-module/src/main.py-tpl | 16 +++ .../tests/conftest.py-tpl | 15 +++ .../tests/test_main.py-tpl | 16 +++ 13 files changed, 260 insertions(+), 19 deletions(-) create mode 100644 scripts/inspect-changed-templates.py create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/README.md-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/pyproject.toml-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/requirements.txt-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/setup.cfg-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/setup.py-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/src/main.py-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/tests/conftest.py-tpl create mode 100644 src/fastapi_fastkit/fastapi_project_template/fastapi-single-module/tests/test_main.py-tpl diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd7da7d..1878a45 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,6 +32,13 @@ repos: types: [python] pass_filenames: false + - id: inspect-templates + name: inspect changed fastapi templates + entry: python scripts/inspect-changed-templates.py + language: system + pass_filenames: false + stages: [commit] + ci: autofix_commit_msg: 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks autoupdate_commit_msg: ⬆ [pre-commit.ci] pre-commit autoupdate diff --git a/scripts/inspect-changed-templates.py b/scripts/inspect-changed-templates.py new file mode 100644 index 0000000..376271f --- /dev/null +++ b/scripts/inspect-changed-templates.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# -------------------------------------------------------------------------- +# Inspect only changed FastAPI templates (for pre-commit / local use) +# -------------------------------------------------------------------------- +import os +import subprocess +import sys +from pathlib import Path +from typing import List, Set + +# Ensure src is importable +PROJECT_ROOT = Path(__file__).parent.parent +sys.path.insert(0, str(PROJECT_ROOT / "src")) + +from fastapi_fastkit.backend.inspector import inspect_fastapi_template + +TEMPLATE_DIR = PROJECT_ROOT / "src" / "fastapi_fastkit" / "fastapi_project_template" + + +def get_changed_files() -> List[str]: + """Return a list of changed files according to git (staged + unstaged).""" + # Include staged and unstaged changes compared to HEAD + cmd = [ + "git", + "diff", + "--name-only", + "--cached", + ] + staged = subprocess.run(cmd, capture_output=True, text=True, check=False) + cmd_unstaged = [ + "git", + "diff", + "--name-only", + ] + unstaged = subprocess.run(cmd_unstaged, capture_output=True, text=True, check=False) + + files: Set[str] = set() + if staged.stdout: + files.update( + [line.strip() for line in staged.stdout.splitlines() if line.strip()] + ) + if unstaged.stdout: + files.update( + [line.strip() for line in unstaged.stdout.splitlines() if line.strip()] + ) + return sorted(files) + + +def extract_changed_templates(changed_files: List[str]) -> List[str]: + """Map changed files to template directory names under TEMPLATE_DIR.""" + templates: Set[str] = set() + template_root_str = str(TEMPLATE_DIR).rstrip("/") + + for f in changed_files: + fp = (PROJECT_ROOT / f).resolve() + # Only consider files under template root + try: + if str(fp).startswith(template_root_str): + # template name is immediate child dir of TEMPLATE_DIR + # e.g., .../fastapi_project_template/