From 1c353bbcdf159e3556fa8189d7da9ef4169e58c1 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Apr 2021 13:00:26 +0200 Subject: [PATCH] Improved cli workspace handling --- src/cpl_cli/cli.py | 22 +++++++------- src/cpl_cli/command_handler_service.py | 40 +++++++++++++++++--------- src/cpl_cli/command_model.py | 9 ++++-- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 285ca600..d249417c 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -33,17 +33,17 @@ class CLI(ApplicationABC): def configure(self): self._command_handler: CommandHandler = self._services.get_service(CommandHandler) - self._command_handler.add_command(CommandModel('build', ['h', 'B'], BuildService, True, True)) - self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, True, False)) - self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False, False)) - self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, True, True)) - self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, True)) - self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, True, True)) - self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, False)) - self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, True, True)) - self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, True, True)) - self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, True, True)) - self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False, False)) + self._command_handler.add_command(CommandModel('build', ['h', 'B'], BuildService, False, True, True)) + self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, False, True, False)) + self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False, False, False)) + self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, False, True, True)) + self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, False, True)) + self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, False, True, True)) + self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, True, False)) + self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, False, True, True)) + self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, False, True, True)) + self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, False, True, True)) + self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False, False, False)) def main(self): """ diff --git a/src/cpl_cli/command_handler_service.py b/src/cpl_cli/command_handler_service.py index 413e0ee9..8e0fd8d7 100644 --- a/src/cpl_cli/command_handler_service.py +++ b/src/cpl_cli/command_handler_service.py @@ -5,6 +5,7 @@ from typing import Optional from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console from cpl.dependency_injection.service_provider_abc import ServiceProviderABC +from cpl.utils.string import String from cpl_cli.configuration.workspace_settings import WorkspaceSettings from cpl_cli.error import Error from cpl_cli.command_model import CommandModel @@ -57,23 +58,34 @@ class CommandHandler(ABC): workspace = self._config.get_configuration(WorkspaceSettings) if command.is_project_needed: - if os.path.isfile( - os.path.join( - self._env.working_directory, - f'{os.path.basename(self._env.working_directory)}.json' - ) - ): + name = os.path.basename(self._env.working_directory) + project_path = os.path.join( + self._env.working_directory, + f'{name}.json' + ) + project_path_camel_case = os.path.join( + self._env.working_directory, + f'{String.convert_to_camel_case(name)}.json' + ) + + if os.path.isfile(project_path): project_name = os.path.basename(self._env.working_directory) - if workspace is None and project_name is None: - Error.error( - 'The command requires to be run in an CPL workspace or project, ' - 'but a workspace or project could not be found.' - ) - return + if os.path.isfile(project_path_camel_case): + project_name = String.convert_to_camel_case(name) - if project_name is None: - project_name = workspace.default_project + if command.is_workspace_needed: + if workspace is None and project_name is None: + Error.error( + 'The command requires to be run in an CPL workspace or project, ' + 'but a workspace or project could not be found.' + ) + return + + if project_name is None: + project_name = workspace.default_project + + Console.write_line(project_name) self._config.add_configuration('ProjectName', project_name) project_json = f'{project_name}.json' diff --git a/src/cpl_cli/command_model.py b/src/cpl_cli/command_model.py index 42e18274..5fabea2a 100644 --- a/src/cpl_cli/command_model.py +++ b/src/cpl_cli/command_model.py @@ -5,11 +5,12 @@ from cpl_cli.command_abc import CommandABC class CommandModel: - def __init__(self, name: str, aliases: list[str], command: Callable[CommandABC], is_project_needed: bool, - change_cwd: bool): + def __init__(self, name: str, aliases: list[str], command: Callable[CommandABC], is_workspace_needed: bool, + is_project_needed: bool, change_cwd: bool): self._name = name self._aliases = aliases self._command = command + self._is_workspace_needed = is_workspace_needed self._is_project_needed = is_project_needed self._change_cwd = change_cwd @@ -24,6 +25,10 @@ class CommandModel: @property def command(self) -> Callable[CommandABC]: return self._command + + @property + def is_workspace_needed(self) -> bool: + return self._is_workspace_needed @property def is_project_needed(self) -> bool: