Skip to content

Commit d0c81e6

Browse files
authored
Merge pull request #2 from fastapi-mvc/update_template
Update generator template to work with the new API
2 parents ccdeb0c + eecad78 commit d0c81e6

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
This file documents changes to [copier-generator](https://github.com/fastapi-mvc/copier-generator). The release numbering uses [semantic versioning](http://semver.org).
44

5+
## 0.3.0 (01.03.2023)
6+
7+
### Internal
8+
9+
* Update template to work with the new API for creating generator's CLIs. [fastapi-mvc/fastapi-mvc#247](https://github.com/fastapi-mvc/fastapi-mvc/issues/247). PR [#2](https://github.com/fastapi-mvc/copier-generator/pull/2)
10+
511
## 0.2.0 (10.02.2023)
612

713
### Features

template/{{generator_name}}.py.jinja

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import os
33

44
import click
5-
from fastapi_mvc import Generator
5+
import copier
6+
from fastapi_mvc.cli import GeneratorCommand
7+
from fastapi_mvc.utils import require_fastapi_mvc_project
8+
from fastapi_mvc.constants import ANSWERS_FILE
69

710

811
cmd_short_help = "Run custom generator {{generator_name.replace("_", "-")}}."
@@ -20,27 +23,38 @@ Example:
2023

2124

2225
@click.command(
23-
cls=Generator,
24-
# Or use repository address
25-
template=os.path.dirname(__file__),
26-
category="Custom",
26+
# Using ``GeneratorCommand`` class is not required.
27+
# It only provides you with alias, category and some help formatting utils.
28+
# You can use ``click.Command`` should you choose.
29+
cls=GeneratorCommand,
2730
help=cmd_help,
2831
short_help=cmd_short_help,
32+
# Define alias short-cut for more efficient invocation
33+
alias="foo",
34+
# Define category under which generator should be printed in ``fastapi-mvc generate`` CLI command help page.
35+
category="Custom",
2936
)
3037
@click.argument(
3138
"NAME",
3239
required=True,
3340
nargs=1,
3441
)
35-
@click.pass_context
36-
def {{generator_name}}(ctx, name):
37-
# You can access Generator class object instance for this command via click.Context
38-
# https://click.palletsprojects.com/en/8.1.x/api/?highlight=context#click.Context.command
39-
ctx.command.ensure_project_data()
42+
def {{generator_name}}(name: str) -> None:
43+
"""Define {{generator_name}} generator command-line interface.
44+
45+
Args:
46+
name (str): Given name to greet.
47+
48+
"""
49+
project_data = require_fastapi_mvc_project()
4050

4151
data = {
42-
"project_name": ctx.command.project_data["project_name"],
52+
"project_name": project_data["project_name"],
4353
"name": name.lower().replace("-", "_"),
4454
}
4555

46-
ctx.command.run_copy(data=data)
56+
copier.run_copy(
57+
src_path=os.path.dirname(__file__), # Or use git repository
58+
data=data,
59+
answers_file=ANSWERS_FILE,
60+
)

0 commit comments

Comments
 (0)