2021-03-08 22:29:28 +01:00
|
|
|
import os
|
2021-03-29 13:29:26 +02:00
|
|
|
from abc import ABC
|
2021-04-08 19:30:00 +02:00
|
|
|
from typing import Optional
|
2021-03-08 22:29:28 +01:00
|
|
|
|
2021-03-16 22:06:37 +01:00
|
|
|
from cpl.configuration.configuration_abc import ConfigurationABC
|
2021-03-23 17:31:24 +01:00
|
|
|
from cpl.console.console import Console
|
2021-03-08 22:02:16 +01:00
|
|
|
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
|
2021-04-11 13:00:26 +02:00
|
|
|
from cpl.utils.string import String
|
2021-04-07 18:19:34 +02:00
|
|
|
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
|
2021-03-08 22:29:28 +01:00
|
|
|
from cpl_cli.error import Error
|
2021-03-04 19:06:53 +01:00
|
|
|
from cpl_cli.command_model import CommandModel
|
2021-03-03 19:37:35 +01:00
|
|
|
|
|
|
|
|
2021-03-29 13:29:26 +02:00
|
|
|
class CommandHandler(ABC):
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service to handle incoming commands and args
|
2021-03-29 08:56:18 +02:00
|
|
|
:param config:
|
2021-03-14 16:01:15 +01:00
|
|
|
:param services:
|
|
|
|
"""
|
2021-03-29 13:29:26 +02:00
|
|
|
ABC.__init__(self)
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-16 22:06:37 +01:00
|
|
|
self._config = config
|
2021-03-29 08:56:18 +02:00
|
|
|
self._env = self._config.environment
|
2021-03-04 19:06:53 +01:00
|
|
|
self._services = services
|
2021-03-04 07:18:36 +01:00
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
self._commands: list[CommandModel] = []
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-13 22:53:28 +01:00
|
|
|
@property
|
|
|
|
def commands(self) -> list[CommandModel]:
|
|
|
|
return self._commands
|
|
|
|
|
2021-04-11 13:20:32 +02:00
|
|
|
@staticmethod
|
|
|
|
def _project_not_found():
|
|
|
|
Error.error(
|
|
|
|
'The command requires to be run in an CPL project, but a project could not be found.'
|
|
|
|
)
|
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
def add_command(self, cmd: CommandModel):
|
2021-03-03 19:37:35 +01:00
|
|
|
self._commands.append(cmd)
|
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
def remove_command(self, cmd: CommandModel):
|
2021-03-03 19:37:35 +01:00
|
|
|
self._commands.remove(cmd)
|
|
|
|
|
|
|
|
def handle(self, cmd: str, args: list[str]):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Handles incoming commands and args
|
|
|
|
:param cmd:
|
|
|
|
:param args:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-03 19:37:35 +01:00
|
|
|
for command in self._commands:
|
|
|
|
if cmd == command.name or cmd in command.aliases:
|
2021-04-09 22:09:55 +02:00
|
|
|
error = None
|
|
|
|
project_name: Optional[str] = None
|
|
|
|
workspace: Optional[WorkspaceSettings] = None
|
2021-04-08 19:30:00 +02:00
|
|
|
|
2021-04-09 22:09:55 +02:00
|
|
|
if os.path.isfile(os.path.join(self._env.working_directory, 'cpl-workspace.json')):
|
|
|
|
self._config.add_json_file('cpl-workspace.json', optional=True, output=False)
|
|
|
|
workspace = self._config.get_configuration(WorkspaceSettings)
|
2021-04-08 19:30:00 +02:00
|
|
|
|
2021-04-09 22:09:55 +02:00
|
|
|
if command.is_project_needed:
|
2021-04-11 13:00:26 +02:00
|
|
|
name = os.path.basename(self._env.working_directory)
|
2021-04-11 14:21:59 +02:00
|
|
|
for r, d, f in os.walk(self._env.working_directory):
|
|
|
|
for file in f:
|
|
|
|
if file.endswith('.json'):
|
|
|
|
f_name = file.split('.json')[0]
|
|
|
|
if f_name == name or \
|
|
|
|
String.convert_to_camel_case(f_name) == String.convert_to_camel_case(name):
|
|
|
|
project_name = f_name
|
|
|
|
break
|
2021-04-11 13:00:26 +02:00
|
|
|
|
2021-04-11 13:20:32 +02:00
|
|
|
if not command.is_workspace_needed and project_name is None and workspace is None:
|
|
|
|
self._project_not_found()
|
|
|
|
return
|
|
|
|
|
2021-04-11 13:23:02 +02:00
|
|
|
elif command.is_workspace_needed or project_name is None:
|
2021-04-11 13:15:19 +02:00
|
|
|
if workspace is None:
|
2021-04-11 13:00:26 +02:00
|
|
|
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
|
2021-04-07 18:19:34 +02:00
|
|
|
|
2021-04-07 19:18:55 +02:00
|
|
|
self._config.add_configuration('ProjectName', project_name)
|
2021-04-08 19:30:00 +02:00
|
|
|
project_json = f'{project_name}.json'
|
2021-04-07 19:18:55 +02:00
|
|
|
|
2021-04-08 19:30:00 +02:00
|
|
|
if workspace is not None:
|
|
|
|
if project_name not in workspace.projects:
|
|
|
|
Error.error(
|
|
|
|
f'Project {project_name} not found.'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
project_json = workspace.projects[project_name]
|
2021-04-07 18:19:34 +02:00
|
|
|
|
|
|
|
if not os.path.isfile(os.path.join(self._env.working_directory, project_json)):
|
2021-04-11 13:20:32 +02:00
|
|
|
self._project_not_found()
|
2021-04-07 18:19:34 +02:00
|
|
|
return
|
|
|
|
|
2021-04-09 20:07:38 +02:00
|
|
|
project_json = os.path.join(self._env.working_directory, project_json)
|
2021-04-07 18:19:34 +02:00
|
|
|
|
2021-04-11 11:12:39 +02:00
|
|
|
if command.change_cwd:
|
|
|
|
self._env.set_working_directory(
|
|
|
|
os.path.join(self._env.working_directory, os.path.dirname(project_json))
|
|
|
|
)
|
2021-03-16 22:06:37 +01:00
|
|
|
|
2021-04-09 20:07:38 +02:00
|
|
|
self._config.add_json_file(project_json, optional=True, output=False)
|
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
self._services.get_service(command.command).run(args)
|
2021-03-23 17:31:24 +01:00
|
|
|
Console.write('\n')
|