From 43128cc7894b9789fc63aa4c02af4744e2c22bac Mon Sep 17 00:00:00 2001 From: colyerdeng Date: Mon, 17 Feb 2025 17:47:19 +0800 Subject: [PATCH 1/2] feat: update project template and cli feat: update template and cli tools feat: update template and update cli tools --- .gitignore | 3 +- noxfile.py | 3 +- pyproject.toml | 5 +- repo_scaffold/cli.py | 194 ++++++++++++++---- .../templates/cookiecutter.json | 2 +- .../template-python/cookiecutter.json | 28 +++ .../template-python}/hooks/__init__.py | 0 .../template-python/hooks/post_gen_project.py | 40 ++++ .../.github/workflows/bump_version.yaml | 0 .../.github/workflows/lint.yaml | 0 .../.github/workflows/release_build.yaml | 21 +- .../{{cookiecutter.project_slug}}/.gitignore | 0 .../{{cookiecutter.project_slug}}/README.md | 0 .../{{cookiecutter.project_slug}}/__init__.py | 0 .../{{cookiecutter.project_slug}}/docs/api.md | 5 + .../docs/contributing.md | 20 ++ .../docs/index.md | 17 ++ .../{{cookiecutter.project_slug}}/mkdocs.yml | 73 +++++++ .../{{cookiecutter.project_slug}}/noxfile.py | 175 ++++++++++------ .../pyproject.toml | 8 +- .../tests/__init__.py | 0 .../tests/test_import.py | 0 .../{{cookiecutter.project_slug}}/__init__.py | 0 .../{{cookiecutter.project_slug}}/cli.py | 0 .../constants.py | 0 .../{{cookiecutter.project_slug}}/core.py | 0 template-python/cookiecutter.json | 11 - template-python/hooks/post_gen_project.py | 19 -- template-python/hooks/pre_gen_project.py | 0 template-python/hooks/pre_prompt.py | 0 30 files changed, 482 insertions(+), 142 deletions(-) rename cookiecutter.json => repo_scaffold/templates/cookiecutter.json (78%) create mode 100644 repo_scaffold/templates/template-python/cookiecutter.json rename {template-python => repo_scaffold/templates/template-python}/hooks/__init__.py (100%) create mode 100644 repo_scaffold/templates/template-python/hooks/post_gen_project.py rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/.github/workflows/bump_version.yaml (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/.github/workflows/lint.yaml (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/.github/workflows/release_build.yaml (72%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/.gitignore (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/README.md (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/__init__.py (100%) create mode 100644 repo_scaffold/templates/template-python/{{cookiecutter.project_slug}}/docs/api.md create mode 100644 repo_scaffold/templates/template-python/{{cookiecutter.project_slug}}/docs/contributing.md create mode 100644 repo_scaffold/templates/template-python/{{cookiecutter.project_slug}}/docs/index.md create mode 100644 repo_scaffold/templates/template-python/{{cookiecutter.project_slug}}/mkdocs.yml rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/noxfile.py (51%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/pyproject.toml (86%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/tests/__init__.py (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/tests/test_import.py (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/__init__.py (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/cli.py (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/constants.py (100%) rename {template-python => repo_scaffold/templates/template-python}/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/core.py (100%) delete mode 100644 template-python/cookiecutter.json delete mode 100644 template-python/hooks/post_gen_project.py delete mode 100644 template-python/hooks/pre_gen_project.py delete mode 100644 template-python/hooks/pre_prompt.py diff --git a/.gitignore b/.gitignore index 87b832e..a58c49a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ /.zip/ /venv/ /dist/ -/__pycache__/ +**/__pycache__/ +*.pyc diff --git a/noxfile.py b/noxfile.py index e61d0d5..bb282f6 100644 --- a/noxfile.py +++ b/noxfile.py @@ -58,8 +58,7 @@ def lint(session: nox.Session) -> None: session.run( "ruff", "format", - "--verbose", - "--diff" + "--verbose" ) diff --git a/pyproject.toml b/pyproject.toml index f16b8f7..5534276 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,9 @@ dev = [ [project.scripts] repo-scaffold = "repo_scaffold.cli:cli" +[tool.setuptools.package-data] +repo_scaffold = ["templates/**/*", "cookiecutter.json"] + [build-system] requires = ["hatchling"] build-backend = "hatchling.build" @@ -34,7 +37,7 @@ build-backend = "hatchling.build" [tool.ruff] line-length = 120 include = ["pyproject.toml", "repo_scaffold/*.py"] -exclude = ["template-python/**/*"] +exclude = ["repo_scaffold/templates/**/*"] [tool.ruff.lint] select = [ diff --git a/repo_scaffold/cli.py b/repo_scaffold/cli.py index 3b3ed45..e08e0b0 100644 --- a/repo_scaffold/cli.py +++ b/repo_scaffold/cli.py @@ -1,64 +1,184 @@ -"""Command Line Interface module for project scaffolding. +"""Repository scaffolding CLI tool. -This module provides the CLI commands for creating new projects using cookiecutter -templates. It serves as the main entry point for the repo_scaffold tool and handles -all command-line interactions. +This module provides a command-line interface for creating new projects from templates. +It serves as the main entry point for the repo-scaffold tool. -Typical usage example: +Example: + To use this module as a CLI tool: + ```bash + # List available templates + $ repo-scaffold list + + # Create a new project + $ repo-scaffold create python + ``` + + To use this module in your code: + + ```python from repo_scaffold.cli import cli if __name__ == '__main__': cli() - -Attributes: - cli: The main Click command group that serves as the entry point. + ``` """ +import importlib.resources +import json import os +from pathlib import Path +from typing import Any +from typing import Dict import click from cookiecutter.main import cookiecutter +def get_package_path(relative_path: str) -> str: + """Get absolute path to a resource in the package. + + Args: + relative_path: Path relative to the package root + + Returns: + str: Absolute path to the resource + """ # noqa: W293 + # 使用 files() 获取包资源 + package_files = importlib.resources.files("repo_scaffold") + resource_path = package_files.joinpath(relative_path) + if not (resource_path.is_file() or resource_path.is_dir()): + raise FileNotFoundError(f"Resource not found: {relative_path}") + return str(resource_path) + + +def load_templates() -> Dict[str, Any]: + """Load available project templates configuration. + + Reads template configurations from the cookiecutter.json file in the templates directory. + Each template contains information about its name, path, title, and description. + + Returns: + Dict[str, Any]: Template configuration dictionary where keys are template names + and values are template information: + { + "template-name": { + "path": "relative/path", + "title": "Template Title", + "description": "Template description" + } + } + + Raises: + FileNotFoundError: If the configuration file doesn't exist + json.JSONDecodeError: If the configuration file is not valid JSON + """ + config_path = get_package_path("templates/cookiecutter.json") + with open(config_path, "r", encoding="utf-8") as f: + config = json.load(f) + return config["templates"] + + @click.group() def cli(): - """Repository scaffolding CLI tool. + """Modern project scaffolding tool. - A tool for creating new projects from templates. + Provides multiple project templates for quick project initialization. + Use `repo-scaffold list` to view available templates, + or `repo-scaffold create