Skip to content

Commit 48a94b6

Browse files
committed
Copier Template: README: More compact, aesthetically-pleasing representation of other projects.
Also, * Package: Copier Template: Add 'preserve' option to CLI for validator to allow for inspection of temporarily-generated projects. (Useful for validating new or altered features, such as the above.)
1 parent fbd14ac commit 48a94b6

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

sources/emcdproj/__/imports.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import os
3636
import shutil
3737
import sys
38+
import tempfile
3839
import types
3940

4041
from pathlib import Path

sources/emcdproj/template.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from __future__ import annotations
2525

2626
import subprocess as _subprocess
27-
import tempfile as _tempfile
2827

2928
from . import __
3029
from . import interfaces as _interfaces
@@ -76,13 +75,18 @@ class ValidateCommand(
7675
__.typx.Doc( ''' Configuration variant to validate. ''' ),
7776
__.tyro.conf.Positional,
7877
]
78+
preserve: __.typx.Annotated[
79+
bool,
80+
__.typx.Doc( ''' Preserve generated project for inspection? ''' ),
81+
] = False
7982

8083
async def __call__(
8184
self, auxdata: __.Globals, display: _interfaces.ConsoleDisplay
8285
) -> None:
8386
''' Copies new project from template for configuration variant. '''
8487
# TODO: Validate variant argument.
85-
validate_variant( auxdata, self.variant )
88+
validate_variant(
89+
auxdata, self.variant, preserve = self.preserve )
8690

8791

8892
def copy_template( answers_file: __.Path, projectdir: __.Path ) -> None:
@@ -103,16 +107,18 @@ def survey_variants( auxdata: __.Globals ) -> __.cabc.Sequence[ str ]:
103107
if fsent.is_file( ) )
104108

105109

106-
def validate_variant( auxdata: __.Globals, variant: str ) -> None:
110+
def validate_variant(
111+
auxdata: __.Globals, variant: str, preserve: bool
112+
) -> None:
107113
''' Validates configuration variant. '''
108114
answers_file = (
109115
auxdata.distribution.provide_data_location(
110116
'copier', f"answers-{variant}.yaml" ) )
111117
if not answers_file.is_file( ):
112118
# TODO: Raise error.
113119
return
114-
with _tempfile.TemporaryDirectory( ) as tmpdir:
115-
projectdir = __.Path( tmpdir ) / variant
120+
with _manage_temporary_directory( preserve = preserve ) as tmpdir:
121+
projectdir = tmpdir / variant
116122
copy_template( answers_file, projectdir )
117123
validate_variant_project( projectdir )
118124

@@ -125,3 +131,14 @@ def validate_variant_project( projectdir: __.Path ) -> None:
125131
'--upgrade', 'pip', 'build' ),
126132
( 'hatch', '--env', 'develop', 'run', 'make-all' ),
127133
): _subprocess.run( command, cwd = str( projectdir ), check = True ) # noqa: S603
134+
135+
136+
@__.ctxl.contextmanager
137+
def _manage_temporary_directory(
138+
preserve: bool
139+
) -> __.cabc.Iterator[ __.Path ]:
140+
# TODO: Python 3.12: Replace with tempfile.TemporaryDirectory,
141+
# ( delete = not preserve )
142+
location = __.Path( __.tempfile.mkdtemp( ) )
143+
yield location
144+
if not preserve: __.shutil.rmtree( location )

template/README.rst.jinja

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,7 @@ Other Projects by This Author 🌟
136136
{%- set my_projects = my_projects_json | from_json %}
137137

138138
{% for repo_name, info in my_projects.items() if repo_name != project_name %}
139-
* `{{ repo_name }} <https://github.com/{{ gh_owner }}/{{ repo_name }}>`_
140-
{%- if 'pypi_name' in info %}
141-
- PyPI: `{{ info.pypi_name }} <https://pypi.org/project/{{ info.pypi_name }}/>`_
142-
{%- endif %}
143-
{%- if 'crate_name' in info %}
144-
- Crates.io: `{{ info.crate_name }} <https://crates.io/crates/{{ info.crate_name }}>`_
145-
{%- endif %}
146-
147-
{{ info.blurb }}
139+
* `{{ repo_name }} <https://github.com/{{ gh_owner }}/{{ repo_name }}>`_ {% if 'pypi_name' in info %}(`{{ info.pypi_name }} <https://pypi.org/project/{{ info.pypi_name }}/>`_ on PyPI){% endif %} {% if 'crate_name' in info %}(`{{ info.crate_name }} <https://crates.io/crates/{{ info.crate_name }}/>`_ on Crates.io){% endif %}
140+
141+
{{ info.blurb }}
148142
{%- endfor %}

0 commit comments

Comments
 (0)