Skip to content

Commit 06a688d

Browse files
committed
Added new command
1 parent df9f19f commit 06a688d

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

ellar_cli/manage_commands/new.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import os
2+
import re
3+
import subprocess
4+
import typing as t
5+
6+
import typer
7+
from ellar.helper.module_loading import module_dir
8+
9+
from ellar_cli import scaffolding
10+
from ellar_cli.schema import EllarScaffoldSchema
11+
12+
from ..file_scaffolding import FileTemplateScaffold
13+
from ..service import EllarCLIException
14+
15+
__all__ = ["new_command"]
16+
17+
18+
conf_module_dir = module_dir(scaffolding)
19+
root_scaffold_template_path = os.path.join(conf_module_dir, "new_template")
20+
init_template_json = os.path.join(root_scaffold_template_path, "setup.json")
21+
22+
23+
class NewTemplateScaffold(FileTemplateScaffold):
24+
unwanted_chars = "".join(["-", ";", "!", "*", ":", " "])
25+
26+
def __init__(self, project_name: str = None, **kwargs: t.Any) -> None:
27+
self._project_name = project_name
28+
super(NewTemplateScaffold, self).__init__(**kwargs)
29+
30+
def on_scaffold_completed(self) -> None:
31+
popen_res = subprocess.run(
32+
["ellar", "create-project", self.get_project_name()],
33+
cwd=self.get_project_cwd(),
34+
stdout=subprocess.PIPE,
35+
stderr=subprocess.PIPE,
36+
)
37+
if popen_res.returncode == 0:
38+
project_working_project_name = self.get_project_name()
39+
print(
40+
f"`{self._working_project_name}` project created successfully.\n"
41+
f"- cd {self._working_project_name}"
42+
)
43+
print("To start your server, run the command below")
44+
print(
45+
f"- ellar --project {project_working_project_name} runserver --reload\nHappy coding!"
46+
)
47+
else:
48+
print(popen_res.stderr.decode("utf8"))
49+
50+
def validate_project_name(self) -> None:
51+
if os.path.exists(self._working_project_name):
52+
message = "A folder with same name exist '{name}' ".format(
53+
name=self._working_project_name
54+
)
55+
raise EllarCLIException(message)
56+
57+
project_working_project_name = self.get_project_name()
58+
if not project_working_project_name.isidentifier():
59+
message = (
60+
"'{name}' is not a valid project-name. "
61+
"Please make sure the project-name "
62+
"is a valid identifier.".format(name=self._working_project_name)
63+
)
64+
raise EllarCLIException(message)
65+
66+
def get_scaffolding_context(self, working_project_name: str) -> t.Dict:
67+
template_context = dict(folder_name=working_project_name)
68+
return template_context
69+
70+
def get_project_name(self) -> str:
71+
project_working_project_name = self._project_name or self._working_project_name
72+
return re.sub(rf"[{self.unwanted_chars}]", "_", project_working_project_name)
73+
74+
def get_project_cwd(self) -> str:
75+
return os.path.join(self._working_directory, self._working_project_name)
76+
77+
78+
def new_command(
79+
folder_name: str,
80+
project_name: t.Optional[str] = typer.Option(
81+
None,
82+
help="Project Module Name. Defaults to `folder-name` if not set",
83+
show_default=False,
84+
),
85+
):
86+
"""- Runs a complete Ellar project scaffold and creates all files required for managing you application -"""
87+
schema = EllarScaffoldSchema.parse_file(init_template_json)
88+
init_template_scaffold = NewTemplateScaffold(
89+
schema=schema,
90+
working_directory=os.getcwd(),
91+
scaffold_ellar_template_root_path=root_scaffold_template_path,
92+
working_project_name=folder_name.lower(),
93+
project_name=project_name,
94+
)
95+
init_template_scaffold.scaffold()

ellar_cli/scaffolding/new_template/folder_name/README.md

Whitespace-only changes.

ellar_cli/scaffolding/new_template/folder_name/pyproject.toml

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from ellar.core import TestClientFactory, TestClient
2+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"context": ["folder_name"],
3+
"files": [
4+
{
5+
"name": "folder_name",
6+
"is_directory": "true",
7+
"files": [
8+
{
9+
"name": "pyproject.toml"
10+
},
11+
{
12+
"name": "README.md"
13+
},
14+
{
15+
"name": "tests",
16+
"is_directory": "true",
17+
"files": [
18+
{
19+
"name": "conftest.ellar"
20+
}
21+
]
22+
}
23+
]
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)