diff --git a/cookiecutter.json b/cookiecutter.json index 8adb8b4..8624a6e 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,6 +5,7 @@ "project_name": "example-project", "project_slug": "{{cookiecutter.project_name|lower|replace('-', '_')}}", "project_description": "This is a template repository for Python projects that use Poetry for their dependency management.", + "src_layout": ["y", "n"], "include_github_actions": ["y", "n"], "publish_to": ["pypi", "artifactory", "none"], "typechecking": ["mypy", "pyright"], diff --git a/docs/prompt_arguments.md b/docs/prompt_arguments.md index 129fd43..047517f 100644 --- a/docs/prompt_arguments.md +++ b/docs/prompt_arguments.md @@ -35,6 +35,9 @@ from import foo A short description of your project. +**src_layout** +`"y"` or `"n"`. Adds [src_layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) structure to the project + **include_github_actions** `"y"` or `"n"`. Adds a `.github` directory with various actions and diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 0fef2aa..9f939ff 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -3,6 +3,7 @@ import os import shutil +from pathlib import Path PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -15,6 +16,14 @@ def remove_dir(filepath: str) -> None: shutil.rmtree(os.path.join(PROJECT_DIRECTORY, filepath)) +def move_to_src_layout() -> None: + package_dir = "{{cookiecutter.project_slug}}" + src_dir = Path(PROJECT_DIRECTORY) / package_dir + tgt_dir = Path(PROJECT_DIRECTORY) / "src" + tgt_dir.mkdir(parents=True, exist_ok=True) + src_dir.rename(tgt_dir / src_dir.name) + + if __name__ == "__main__": if "{{cookiecutter.include_github_actions}}" != "y": remove_dir(".github") @@ -36,3 +45,5 @@ def remove_dir(filepath: str) -> None: if "{{cookiecutter.devcontainer}}" != "y": remove_dir(".devcontainer") + if "{{cookiecutter.src_layout}}" == "y": + move_to_src_layout() diff --git a/{{cookiecutter.project_name}}/pyproject.toml b/{{cookiecutter.project_name}}/pyproject.toml index 7283173..cfa3d28 100644 --- a/{{cookiecutter.project_name}}/pyproject.toml +++ b/{{cookiecutter.project_name}}/pyproject.toml @@ -6,9 +6,11 @@ authors = ["{{cookiecutter.author}} <{{cookiecutter.email}}>"] repository = "https://github.com/{{cookiecutter.author_github_handle}}/{{cookiecutter.project_name}}" documentation = "https://{{cookiecutter.author_github_handle}}.github.io/{{cookiecutter.project_name}}/" readme = "README.md" -packages = [ - {include = "{{cookiecutter.project_slug}}"} -] +{% if cookiecutter.src_layout == 'y' -%} +packages = [{include = "{{cookiecutter.project_slug}}", from = "src"}] +{% else %} +packages = [{include = "{{cookiecutter.project_slug}}"}] +{% endif %} [tool.poetry.dependencies] python = ">=3.8,<4.0"