|
3 | 3 | import click |
4 | 4 | from ellar.app import AppFactory |
5 | 5 | from ellar.common.constants import MODULE_METADATA |
6 | | -from ellar.core import ModuleSetup, reflector |
| 6 | +from ellar.core import ModuleBase, ModuleSetup, reflector |
7 | 7 |
|
8 | 8 | from ellar_cli.constants import ELLAR_META |
9 | | -from ellar_cli.service import EllarCLIService |
| 9 | +from ellar_cli.service import EllarCLIService, EllarCLIServiceWithPyProject |
10 | 10 |
|
11 | 11 | from .command import Command |
12 | 12 | from .util import with_app_context |
@@ -45,29 +45,56 @@ def group(self, *args: t.Any, **kwargs: t.Any) -> t.Any: |
45 | 45 | class EllarCommandGroup(AppContextGroup): |
46 | 46 | """Overrides AppContextGroup to add loading of Ellar Application registered commands in the modules""" |
47 | 47 |
|
| 48 | + def __init__( |
| 49 | + self, app_import_string: t.Optional[str] = None, **kwargs: t.Any |
| 50 | + ) -> None: |
| 51 | + super().__init__(**kwargs) |
| 52 | + meta = None |
| 53 | + |
| 54 | + if app_import_string: |
| 55 | + meta = EllarCLIServiceWithPyProject(app_import_string=app_import_string) |
| 56 | + |
| 57 | + self._cli_meta: t.Optional[EllarCLIServiceWithPyProject] = meta |
| 58 | + |
48 | 59 | def _load_application_commands(self, ctx: click.Context) -> None: |
49 | 60 | # get project option from cli |
50 | | - app_name = ctx.params.get("project") |
| 61 | + module_configs: t.Any |
| 62 | + if self._cli_meta: |
| 63 | + application = self._cli_meta.import_application() |
51 | 64 |
|
52 | | - # loads project metadata from pyproject.toml |
53 | | - meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta( |
54 | | - app_name |
55 | | - ) |
56 | | - ctx.meta[ELLAR_META] = meta_ |
| 65 | + module_configs = (i for i in application.injector.get_modules().keys()) |
57 | 66 |
|
58 | | - if meta_ and meta_.has_meta: |
59 | | - module_configs = AppFactory.get_all_modules( |
60 | | - ModuleSetup(meta_.import_root_module()) |
| 67 | + ctx.meta[ELLAR_META] = self._cli_meta |
| 68 | + else: |
| 69 | + module_configs = () |
| 70 | + app_name = ctx.params.get("project") |
| 71 | + |
| 72 | + # loads project metadata from pyproject.toml |
| 73 | + meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta( |
| 74 | + app_name |
61 | 75 | ) |
62 | 76 |
|
63 | | - for module_config in module_configs: |
64 | | - click_commands = ( |
65 | | - reflector.get(MODULE_METADATA.COMMANDS, module_config.module) or [] |
66 | | - ) |
| 77 | + ctx.meta[ELLAR_META] = meta_ |
67 | 78 |
|
68 | | - for click_command in click_commands: |
69 | | - if isinstance(click_command, click.Command): |
70 | | - self.add_command(click_command, click_command.name) |
| 79 | + if meta_ and meta_.has_meta: |
| 80 | + module_configs = ( |
| 81 | + module_config.module |
| 82 | + for module_config in AppFactory.get_all_modules( |
| 83 | + ModuleSetup(meta_.import_root_module()) |
| 84 | + ) |
| 85 | + ) |
| 86 | + self._find_commands_from_modules(module_configs) |
| 87 | + |
| 88 | + def _find_commands_from_modules( |
| 89 | + self, |
| 90 | + modules: t.Union[t.Sequence[ModuleBase], t.Iterator[ModuleBase], t.Generator], |
| 91 | + ) -> None: |
| 92 | + for module in modules: |
| 93 | + click_commands = reflector.get(MODULE_METADATA.COMMANDS, module) or [] |
| 94 | + |
| 95 | + for click_command in click_commands: |
| 96 | + if isinstance(click_command, click.Command): |
| 97 | + self.add_command(click_command, click_command.name) |
71 | 98 |
|
72 | 99 | def get_command( |
73 | 100 | self, ctx: click.Context, cmd_name: str |
|
0 commit comments