Skip to content

Commit a6da1af

Browse files
authored
feat: Add Copier template for project generation (#5)
This commit introduces a Copier template to streamline project setup, which in turn can generate the main branch The template includes: - copier.yml: Defines project configuration options such as project_name, module_name, description, user_name, user_email, python_version, and include_dockerfile. - Renamed and templated pyproject.toml to pyproject.toml.jinja to dynamically set project metadata, based on user input. - Renamed module directory postmodern to {{ module_name }} and its contents (__init__.py, __main__.py, adder.py, cli.py, hello.py, server.py) to allow for customizable module names. - Renamed test_import.py to test_import.py.jinja for templating. The python_version field now includes validation to ensure it follows the X.Y.Z format. Co-authored-by: Jakob Stender Guldberg <jakob1379gmail.com>
1 parent 87c96e2 commit a6da1af

17 files changed

Lines changed: 144 additions & 18 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Render Copier Template
2+
3+
on:
4+
push:
5+
branches:
6+
- template
7+
8+
jobs:
9+
render:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout main branch
13+
uses: actions/checkout@v4
14+
with:
15+
ref: main
16+
path: main
17+
18+
- name: Checkout template branch
19+
uses: actions/checkout@v4
20+
with:
21+
ref: template
22+
path: template
23+
24+
- name: Set up Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: "3.x"
28+
29+
- name: Install uv
30+
run: pip install uv
31+
working-directory: ${{ github.workspace }}/template
32+
33+
# comment
34+
- name: Render template
35+
run: |
36+
cd ${{ github.workspace }}/template
37+
uvx copier copy \
38+
--overwrite \
39+
--defaults \
40+
-d description="Add your description here" \
41+
-d include_dockerfile=true \
42+
-d module_name=postmodern \
43+
-d project_name=postmodern-python \
44+
-d python_version=3.13 \
45+
-d user_email=you@example.com \
46+
-d user_name="Your Name" \
47+
. ../main/
48+
49+
- name: Configure Git
50+
run: |
51+
git config --global user.name "github-actions[bot]"
52+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
53+
54+
- name: Commit and Push Changes
55+
run: |
56+
cd ${{ github.workspace }}/main
57+
rm -f .copier-answers.yml
58+
git add .
59+
if ! git diff --cached --exit-code; then
60+
git commit -m "Auto-render main template: ${{ github.event.head_commit.message }}"
61+
git push origin main
62+
else
63+
echo "No changes to commit."
64+
fi

copier.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
3+
project_name:
4+
type: str
5+
help: Give your project an amazing name
6+
7+
module_name:
8+
type: str
9+
help: Give the module a name
10+
default: "{{ project_name }}"
11+
12+
description:
13+
type: str
14+
help: Make a short description for the project (optional).
15+
default: ""
16+
17+
user_name:
18+
type: str
19+
help: Set your full name
20+
21+
user_email:
22+
type: str
23+
help: Set your email
24+
25+
python_version:
26+
type: str
27+
help: What python version do you want to use? (optional)
28+
default: 3.13
29+
validators:
30+
- >-
31+
{{
32+
(
33+
value.isdigit() # X format
34+
or (
35+
value.replace('.', '').isdigit() and
36+
value.count('.') == 1 and
37+
(value.split('.') | length) == 2 and
38+
value.split('.')[0].isdigit() and
39+
value.split('.')[1].isdigit()
40+
) # X.Y format
41+
or (
42+
value.replace('.', '').isdigit() and
43+
value.count('.') == 2 and
44+
(value.split('.') | length) == 3 and
45+
value.split('.')[0].isdigit() and
46+
value.split('.')[1].isdigit() and
47+
value.split('.')[2].isdigit()
48+
) # X.Y.Z format
49+
)
50+
or 'Version must be in X, X.Y, or X.Y.Z format (e.g., 3, 3.13, or 3.13.4) with numbers only, no <, >, =, or other characters.'
51+
}}
52+
include_dockerfile:
53+
type: bool
54+
help: "Include a Dockerfile in the project?"
55+
default: yes
56+
57+
_src_path: project_template
58+
_exlude:
59+
- .venv/
60+
- .git/

postmodern/__main__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

postmodern/hello.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

pyproject.toml renamed to pyproject.toml.jinja

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ all = [ {ref="fmt"}, {ref="lint"}, {ref="check"}, {ref="test"} ]
1111
"ci:lint" = "ruff check"
1212

1313
[project]
14-
name = "postmodern"
14+
name = "{{module_name}}"
1515
version = "0.1.0"
16-
description = "Add your description here"
16+
description = "{{description}}"
1717
readme = "README.md"
1818
authors = [
19-
{ name = "Your Name", email = "you@example.com" }
19+
{ name = "{{ user_name | title }}", email = "{{ user_email }}" }
2020
]
2121

2222
# Public libraries should be more lenient
2323
# Internal stuff should enforce ~=3.13!
24-
requires-python = ">=3.11"
24+
requires-python = ">={{python_version}}"
2525
dependencies = [
2626
"pydantic>=2.10.4",
2727
]
@@ -69,7 +69,7 @@ select = [
6969

7070
[tool.ruff.lint.isort]
7171
# so it knows to group first-party stuff last
72-
known-first-party = ["postmodern"]
72+
known-first-party = ["{{ module_name }}"]
7373

7474
[tool.pyright]
7575
venvPath = "." # uv installs the venv in the current dir

test_import.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

test_import.py.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from {{module_name}}.hello import main
2+
3+
4+
def test_hello():
5+
main()

.dockerignore renamed to {{ '.dockerignore' if include_dockerfile }}.jinja

File renamed without changes.

Dockerfile renamed to {{ 'Dockerfile' if include_dockerfile }}.jinja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# --- Base Stage: Install Dependencies ---
2-
FROM python:3.13.1-slim-bookworm AS base
2+
FROM python:{{python_version}}-slim-bookworm AS base
33

44
ENV PYTHONUNBUFFERED=True
55
WORKDIR /app
@@ -14,7 +14,7 @@ COPY pyproject.toml uv.lock ./
1414
RUN uv sync --frozen
1515

1616
# --- Final Stage: Build Application Image ---
17-
FROM python:3.13.1-slim-bookworm AS runner
17+
FROM python:{{python_version}}-slim-bookworm AS runner
1818

1919
WORKDIR /app
2020

@@ -25,4 +25,4 @@ COPY --from=base /app/.venv ./.venv
2525
COPY . /app
2626

2727
# Set the entrypoint
28-
CMD ["/app/.venv/bin/python", "/app/postmodern/server.py"]
28+
CMD ["/app/.venv/bin/python", "/app/{{module_name}}/server.py"]

rename.sh renamed to {{ 'rename.sh' if project_name == 'postmodern-python' }}.jinja

File renamed without changes.

0 commit comments

Comments
 (0)