Skip to content

Commit 0f24543

Browse files
committed
[FEAT] add new project file naming feature(need test)
1 parent fab1a56 commit 0f24543

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

src/fastapi_fastkit/cli.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,12 @@ def _inject_project_metadata(
9797
raise TemplateExceptions("ERROR : Having some errors with injecting metadata")
9898

9999

100-
def _print_version(ctx: Context, value: click.Option, *args, **kwargs) -> None:
101-
# TODO : apply this at fastapi-cli group
102-
"""
103-
print current version of fastapi-fastkit
104-
105-
:param ctx: context of passing configurations (NOT specify it at CLI)
106-
:type ctx: <Object click.Context>
107-
"""
108-
if value:
109-
version_info = f"fastapi-fastkit version {__version__}"
110-
click.echo(print(version_info))
111-
ctx.exit()
112-
113-
114100
# --------------------------------------------------------------------------
115101
# Click operator methods
116102
# --------------------------------------------------------------------------
117103
@click.group()
118104
@click.option("--debug/--no-debug", default=False)
119-
# @click.option('--version', callback=_print_version,
120-
# expose_value=False)
105+
@click.version_option(__version__, prog_name="fastapi-fastkit")
121106
@click.pass_context
122107
def fastkit_cli(ctx: Context, debug: bool) -> Union["BaseCommand", None]:
123108
"""
@@ -233,6 +218,10 @@ def startproject(
233218
f"Error: Template '{template}' does not exist in '{template_dir}'."
234219
)
235220
# TODO : add confirm step : checking template stack & name & metadata, confirm it y/n
221+
click.echo(f"\nProject Name: {project_name}")
222+
click.echo(f"Author: {author}")
223+
click.echo(f"Author Email: {author_email}")
224+
click.echo(f"Description: {description}")
236225
# click.echo("Project Stack: [FastAPI, Uvicorn, SQLAlchemy, Docker (optional)]")
237226

238227
confirm = click.confirm(
@@ -244,23 +233,16 @@ def startproject(
244233

245234
try:
246235
user_local = settings.USER_WORKSPACE
247-
click.echo(f"FastAPI template project will deploy at '{user_local}'")
248-
249-
click.echo(f"Project Name: {project_name}")
236+
project_dir = os.path.join(user_local, project_name)
250237

251-
confirm = click.confirm(
252-
"\nDo you want to proceed with project creation?", default=False
253-
)
254-
if not confirm:
255-
click.echo("Project creation aborted!")
256-
return
238+
click.echo(f"FastAPI template project will deploy at '{user_local}'")
257239

258-
copy_and_convert_template(target_template, user_local)
240+
copy_and_convert_template(target_template, project_dir, project_name)
259241

260-
_new_user_local = os.path.join(user_local, template)
242+
# _new_user_local = os.path.join(user_local, template)
261243

262244
_inject_project_metadata(
263-
_new_user_local, project_name, author, author_email, description
245+
project_dir, project_name, author, author_email, description
264246
)
265247

266248
click.echo(

src/fastapi_fastkit/utils/transducer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ def _convert_tpl_to_real_extension(file_path: str) -> str:
2626
return file_path
2727

2828

29-
def copy_and_convert_template(template_dir: str, target_dir: str) -> None:
29+
def copy_and_convert_template(template_dir: str, target_dir: str, project_name: str = None) -> None:
3030
"""
3131
Copies all files from the template directory to the target directory,
3232
converting any files ending in `.*-tpl` during the copy process.
3333
34+
:param project_name: name of new project user defined at CLI.
3435
:param template_dir: The source directory containing the template files.
3536
:type template_dir: str
3637
:param target_dir: The destination directory where files will be copied.
3738
:type target_dir: str
3839
"""
3940
template_name = os.path.basename(template_dir)
40-
target_path = os.path.join(target_dir, template_name)
41+
if project_name is None:
42+
project_name = template_name
43+
target_path = os.path.join(target_dir, project_name)
4144
os.makedirs(target_path, exist_ok=True)
4245

4346
for root, dirs, files in os.walk(template_dir):

tests/test_cli_operations/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def test_startproject(self, temp_dir) -> None:
5353
fastkit_cli, # type: ignore
5454
["startproject", "fastapi-default"],
5555
input="\n".join(
56-
["test-project", "bnbong", "bbbong9@gmail.com", "test project"]
56+
["test-project", "bnbong", "bbbong9@gmail.com", "test project", "Y"]
5757
),
5858
)
5959

6060
# then
6161
project_path = (
62-
Path(temp_dir) / "fastapi-default"
62+
Path(temp_dir) / "test-project"
6363
) # TODO : change this after adding folder naming feature.
6464
assert project_path.exists() and project_path.is_dir()
6565
assert (

0 commit comments

Comments
 (0)