Skip to content

Commit 1e1c0c8

Browse files
committed
Removed apps folder from scaffold and allowed create-module to specify directory
1 parent d36fd75 commit 1e1c0c8

15 files changed

Lines changed: 97 additions & 112 deletions

File tree

ellar_cli/file_scaffolding.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
from ellar_cli.schema import EllarScaffoldList, EllarScaffoldSchema
99

10-
from .service import EllarCLIService
11-
1210
__all__ = ["FileTemplateScaffold"]
1311

1412

@@ -27,14 +25,12 @@ def __init__(
2725
working_project_name: str,
2826
working_directory: str,
2927
scaffold_ellar_template_root_path: str,
30-
ellar_cli_service: t.Optional[EllarCLIService] = None,
3128
specified_directory: str = None,
3229
) -> None:
3330
self._specified_directory = specified_directory
3431
self._schema = schema
3532
self._ctx = ProjectScaffoldContext(Environment())
3633
self._scaffold_ellar_template_root_path = scaffold_ellar_template_root_path
37-
self.ellar_cli_service = ellar_cli_service
3834

3935
if self._specified_directory:
4036
_cwd_path = Path(self._get_working_cwd(working_directory))

ellar_cli/manage_commands/create_module.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import sys
33
import typing as t
44
from importlib import import_module
5+
from pathlib import Path
56

67
import typer
78
from ellar.common.helper.module_loading import module_dir
89

910
from ellar_cli import scaffolding
10-
from ellar_cli.constants import ELLAR_META
1111
from ellar_cli.schema import EllarScaffoldSchema
1212

1313
from ..file_scaffolding import FileTemplateScaffold
14-
from ..service import EllarCLIException, EllarCLIService
14+
from ..service import EllarCLIException
1515

1616
__all__ = ["create_module"]
1717

@@ -21,6 +21,19 @@
2121

2222

2323
class ModuleTemplateScaffold(FileTemplateScaffold):
24+
def __init__(
25+
self, working_project_name: str, working_directory: str, **kwargs: t.Any
26+
) -> None:
27+
super().__init__(
28+
working_project_name=working_project_name,
29+
working_directory=working_directory,
30+
**kwargs,
31+
)
32+
if self._specified_directory:
33+
_cwd_path = Path(self._get_working_cwd(working_directory))
34+
self._working_project_name = working_project_name
35+
self._working_directory = str(_cwd_path)
36+
2437
def on_scaffold_completed(self) -> None:
2538
print(f"{self._working_project_name} module completely scaffolded")
2639

@@ -59,24 +72,22 @@ def get_scaffolding_context(self, working_project_name: str) -> t.Dict:
5972
return template_context
6073

6174

62-
def create_module(ctx: typer.Context, module_name: str):
75+
def create_module(
76+
module_name: str,
77+
directory: t.Optional[str] = typer.Argument(
78+
None,
79+
help="The name of a new directory to scaffold the module into.",
80+
show_default=False,
81+
),
82+
):
6383
"""- Scaffolds Ellar Application Module -"""
6484

65-
ellar_project_meta = t.cast(t.Optional[EllarCLIService], ctx.meta.get(ELLAR_META))
66-
if not ellar_project_meta:
67-
raise EllarCLIException("No pyproject.toml file found.")
68-
69-
if not ellar_project_meta.has_meta:
70-
raise EllarCLIException(
71-
"No available project found. please create ellar project with `ellar create-project 'project-name'`"
72-
)
73-
7485
schema = EllarScaffoldSchema.parse_file(module_template_json)
7586
project_template_scaffold = ModuleTemplateScaffold(
7687
schema=schema,
77-
working_directory=ellar_project_meta.get_apps_module_path(),
88+
working_directory=os.getcwd(),
7889
scaffold_ellar_template_root_path=root_scaffold_template_path,
79-
ellar_cli_service=ellar_project_meta,
90+
specified_directory=directory,
8091
working_project_name=module_name.lower(),
8192
)
8293
project_template_scaffold.scaffold()

ellar_cli/manage_commands/create_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323

2424
class ProjectTemplateScaffold(FileTemplateScaffold):
25+
def __init__(self, ellar_cli_service: EllarCLIService, **kwargs: t.Any) -> None:
26+
super().__init__(**kwargs)
27+
self.ellar_cli_service = ellar_cli_service
28+
2529
def get_scaffolding_context(self, working_project_name: str) -> t.Dict:
2630
template_context = dict(
2731
project_name=working_project_name, secret_key=f"ellar_{uuid.uuid4()}"

ellar_cli/manage_commands/new.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def __init__(self, project_name: str = None, **kwargs: t.Any) -> None:
2929
)
3030
self._project_name = project_name
3131

32+
def create_file(self, base_path: str, file_name: str, content: t.Any) -> None:
33+
_path = os.path.join(base_path, file_name.replace(".ellar", ".py"))
34+
if os.path.exists(_path):
35+
content = self.read_file_content(path=_path)
36+
37+
with open(
38+
os.path.join(base_path, file_name.replace(".ellar", ".py")), mode="w"
39+
) as fw:
40+
refined_content = self._ctx.environment.from_string(content).render()
41+
fw.writelines(refined_content)
42+
3243
def on_scaffold_completed(self) -> None:
3344
popen_res = subprocess.run(
3445
["ellar", "create-project", self.get_project_name()],
@@ -68,6 +79,8 @@ def is_directory_empty(self) -> bool:
6879
)
6980
if os.path.isdir(working_project_dir):
7081
items = os.listdir(working_project_dir)
82+
exclude = ["poetry.lock", "pyproject.toml"]
83+
items = [item for item in items if item not in exclude]
7184
return len(items) == 0
7285
return True
7386

ellar_cli/scaffolding/project_template/project_name/apps/__init__.ellar

Whitespace-only changes.

ellar_cli/scaffolding/project_template/setup.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
"name": "project_name",
66
"is_directory": "true",
77
"files": [
8-
{
9-
"name": "apps",
10-
"is_directory": "true",
11-
"files": [
12-
{
13-
"name": "__init__.ellar"
14-
}
15-
]
16-
},
178
{
189
"name": "core",
1910
"is_directory": "true",

ellar_cli/schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class EllarPyProjectSerializer(Serializer):
99
application: str = Field(alias="application")
1010
config: str = Field(alias="config")
1111
root_module: str = Field(alias="root-module")
12-
apps_module: str = Field(alias="apps-module")
1312

1413

1514
class EllarScaffoldList(Serializer):

ellar_cli/service.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
from click import ClickException
55
from ellar.common.constants import ELLAR_CONFIG_MODULE
6-
from ellar.common.helper.importer import import_from_string, module_import
7-
from ellar.common.helper.module_loading import module_dir
6+
from ellar.common.helper.importer import import_from_string
87
from ellar.core import App, Config, ModuleBase
98
from tomlkit import dumps as tomlkit_dumps, parse as tomlkit_parse, table
109
from tomlkit.items import Table
@@ -41,8 +40,8 @@ def has_default_project(self) -> bool:
4140
return self._ellar.get(ELLAR_DEFAULT_KEY, None) is not None
4241

4342
@property
44-
def default_project(self) -> str:
45-
return self._ellar.get(ELLAR_DEFAULT_KEY, None)
43+
def default_project(self) -> t.Optional[str]:
44+
return self._ellar.get(ELLAR_DEFAULT_KEY) or None
4645

4746
@default_project.setter
4847
def default_project(self, value: str) -> None:
@@ -163,7 +162,6 @@ def create_ellar_project_meta(self, project_name: str) -> None:
163162
ellar_new_project.add(
164163
"root-module", f"{project_name}.root_module:ApplicationModule"
165164
)
166-
ellar_new_project.add("apps-module", f"{project_name}.apps")
167165

168166
# TODO: lock pyproject.toml file
169167
EllarCLIService.write_py_project(self.py_project_path, pyproject_table)
@@ -202,12 +200,3 @@ def import_root_module(self) -> t.Type["ModuleBase"]:
202200
t.Type["ModuleBase"], import_from_string(self._meta.root_module)
203201
)
204202
return root_module
205-
206-
def import_apps_module(self) -> t.Any:
207-
assert self._meta
208-
apps_module = module_import(self._meta.apps_module)
209-
return apps_module
210-
211-
def get_apps_module_path(self) -> str:
212-
apps_module = self.import_apps_module()
213-
return module_dir(apps_module) # type: ignore

tests/sample_app/pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ project-name = "example_project"
66
application = "example_project.server:application"
77
config = "example_project.config:DevelopmentConfig"
88
root-module = "example_project.root_module:ApplicationModule"
9-
apps-module = "example_project.apps"
109

1110
[ellar.projects.example_project_2]
1211
project-name = "example_project_2"
1312
application = "example_project_2.server:application"
1413
config = "example_project_2.config:DevelopmentConfig"
1514
root-module = "example_project_2.root_module:ApplicationModule"
16-
apps-module = "example_project_2.apps"

tests/test_build_typers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ def test_help_command(cli_runner):
6060
assert result.returncode == 0
6161
assert (
6262
result.stdout
63-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
63+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion Install completion for the current shell.\n --show-completion Show completion for the current shell, to copy it or\n customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
6464
)
6565
result = subprocess.run(
6666
["ellar", "-p", "example_project", "--help"], stdout=subprocess.PIPE
6767
)
6868
assert result.returncode == 0
6969
assert (
7070
result.stdout
71-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
71+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion Install completion for the current shell.\n --show-completion Show completion for the current shell, to copy it or\n customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
7272
)
7373

7474
result = subprocess.run(
@@ -77,5 +77,5 @@ def test_help_command(cli_runner):
7777
assert result.returncode == 0
7878
assert (
7979
result.stdout
80-
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion [bash|zsh|fish|powershell|pwsh]\n Install completion for the specified shell.\n --show-completion [bash|zsh|fish|powershell|pwsh]\n Show completion for the specified shell, to\n copy it or customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n project-2-command Project 2 Custom Command\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
80+
== b"Usage: Ellar, Python Web framework [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n -p, --project TEXT Run Specific Command on a specific project\n --install-completion Install completion for the current shell.\n --show-completion Show completion for the current shell, to copy it or\n customize the installation.\n --help Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n create-project - Scaffolds Ellar Application -\n db\n new - Runs a complete Ellar project scaffold and creates...\n project-2-command Project 2 Custom Command\n runserver - Starts Uvicorn Server -\n say-hi\n whatever-you-want Whatever you want\n"
8181
)

0 commit comments

Comments
 (0)