diff --git a/cpl-workspace.json b/cpl-workspace.json index 28077a0b..7f294573 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -4,6 +4,9 @@ "Projects": { "cpl": "src/cpl/cpl.json", "cpl_cli": "src/cpl_cli/cpl_cli.json" + }, + "Scripts": { + "hello-world": "python3 scripts/hello_world.py" } } } \ No newline at end of file diff --git a/scripts/hello_world.py b/scripts/hello_world.py new file mode 100644 index 00000000..d3e46f27 --- /dev/null +++ b/scripts/hello_world.py @@ -0,0 +1 @@ +print('Hello World') \ No newline at end of file diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index a4e029f7..3b121d9e 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -1,11 +1,14 @@ +import os from typing import Optional from cpl.application.application_abc import ApplicationABC +from cpl.configuration.console_argument import ConsoleArgument from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console from cpl.dependency_injection import ServiceProviderABC from cpl_cli.command.add_service import AddService from cpl_cli.command.build_service import BuildService +from cpl_cli.command.custom_script_service import CustomScriptService from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.install_service import InstallService from cpl_cli.command.new_service import NewService @@ -16,6 +19,7 @@ from cpl_cli.command.uninstall_service import UninstallService from cpl_cli.command.update_service import UpdateService from cpl_cli.command_handler_service import CommandHandler from cpl_cli.command_model import CommandModel +from cpl_cli.configuration.workspace_settings import WorkspaceSettings from cpl_cli.error import Error from cpl_cli.command.help_service import HelpService from cpl_cli.command.version_service import VersionService @@ -48,6 +52,11 @@ class CLI(ApplicationABC): 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)) + if os.path.isfile(os.path.join(self._environment.working_directory, 'cpl-workspace.json')): + workspace: Optional[WorkspaceSettings] = self._configuration.get_configuration(WorkspaceSettings) + for script in workspace.scripts: + self._command_handler.add_command(CommandModel(script, [], CustomScriptService, True, True, False)) + self._command_handler.add_command(CommandModel('--help', ['-h', '-H'], HelpService, False, False, False)) self._options.append('--help') diff --git a/src/cpl_cli/command/custom_script_service.py b/src/cpl_cli/command/custom_script_service.py new file mode 100644 index 00000000..c03f6368 --- /dev/null +++ b/src/cpl_cli/command/custom_script_service.py @@ -0,0 +1,39 @@ +import os +import subprocess +import sys + +from cpl.configuration.configuration_abc import ConfigurationABC +from cpl.console.console import Console +from cpl_cli.command_abc import CommandABC +from cpl_cli.configuration.workspace_settings import WorkspaceSettings + + +class CustomScriptService(CommandABC): + + def __init__(self, config: ConfigurationABC, ws: WorkspaceSettings): + """ + Service for CLI scripts + """ + CommandABC.__init__(self) + + self._config = config + self._workspace = ws + + @property + def help_message(self) -> str: + return '' + + def run(self, args: list[str]): + cmd = args[0] if len(args) > 0 else self._config.additional_arguments[0] + + for script in self._workspace.scripts: + if script == cmd: + command = self._workspace.scripts[script] + try: + run_command = [] + for word in command.split(' '): + run_command.append(word) + + subprocess.run(run_command) + except Exception as e: + Console.error(str(e)) diff --git a/src/cpl_cli/command_handler_service.py b/src/cpl_cli/command_handler_service.py index 852345fd..0f53df43 100644 --- a/src/cpl_cli/command_handler_service.py +++ b/src/cpl_cli/command_handler_service.py @@ -57,7 +57,7 @@ class CommandHandler(ABC): workspace: Optional[WorkspaceSettings] = None 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) + # self._config.add_json_file('cpl-workspace.json', optional=True, output=False) workspace = self._config.get_configuration(WorkspaceSettings) if command.is_project_needed: diff --git a/src/cpl_cli/configuration/workspace_settings.py b/src/cpl_cli/configuration/workspace_settings.py index ff40999a..2b358218 100644 --- a/src/cpl_cli/configuration/workspace_settings.py +++ b/src/cpl_cli/configuration/workspace_settings.py @@ -13,6 +13,7 @@ class WorkspaceSettings(ConfigurationModelABC): self._default_project: Optional[str] = None self._projects: dict[str, str] = {} + self._scripts: dict[str, str] = {} @property def default_project(self) -> str: @@ -22,10 +23,15 @@ class WorkspaceSettings(ConfigurationModelABC): def projects(self) -> dict[str, str]: return self._projects + @property + def scripts(self): + return self._scripts + def from_dict(self, settings: dict): try: self._default_project = settings[WorkspaceSettingsNameEnum.default_project.value] self._projects = settings[WorkspaceSettingsNameEnum.projects.value] + self._scripts = settings[WorkspaceSettingsNameEnum.scripts.value] except Exception as e: Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/cpl_cli/configuration/workspace_settings_name_enum.py b/src/cpl_cli/configuration/workspace_settings_name_enum.py index 57bbdd20..7e16f65e 100644 --- a/src/cpl_cli/configuration/workspace_settings_name_enum.py +++ b/src/cpl_cli/configuration/workspace_settings_name_enum.py @@ -5,3 +5,4 @@ class WorkspaceSettingsNameEnum(Enum): default_project = 'DefaultProject' projects = 'Projects' + scripts = 'Scripts' diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index 3e0cf9c9..56a70e04 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -1,4 +1,5 @@ import os +from typing import Optional from cpl.application.startup_abc import StartupABC from cpl.configuration.console_argument import ConsoleArgument @@ -7,6 +8,7 @@ from cpl.dependency_injection.service_collection_abc import ServiceCollectionABC from cpl.dependency_injection.service_provider_abc import ServiceProviderABC from cpl_cli.command.add_service import AddService from cpl_cli.command.build_service import BuildService +from cpl_cli.command.custom_script_service import CustomScriptService from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.install_service import InstallService from cpl_cli.command.new_service import NewService @@ -18,6 +20,7 @@ from cpl_cli.command.update_service import UpdateService from cpl_cli.command_handler_service import CommandHandler from cpl_cli.command.help_service import HelpService from cpl_cli.command.version_service import VersionService +from cpl_cli.configuration.workspace_settings import WorkspaceSettings from cpl_cli.error import Error from cpl_cli.live_server.live_server_service import LiveServerService from cpl_cli.publish.publisher_service import PublisherService @@ -72,6 +75,13 @@ class Startup(StartupABC): self._configuration.add_console_argument(ConsoleArgument('', '--help', ['-h', '-H'], '')) + if os.path.isfile(os.path.join(self._env.working_directory, 'cpl-workspace.json')): + self._configuration.add_json_file('cpl-workspace.json', optional=True, output=False) + workspace: Optional[WorkspaceSettings] = self._configuration.get_configuration(WorkspaceSettings) + for script in workspace.scripts: + self._configuration.add_console_argument( + ConsoleArgument('', script, [], ' ', is_value_token_optional=True)) + self._configuration.add_console_arguments(error=False) return self._configuration @@ -84,6 +94,7 @@ class Startup(StartupABC): self._services.add_transient(AddService) self._services.add_transient(BuildService) + self._services.add_transient(CustomScriptService) self._services.add_transient(GenerateService) self._services.add_transient(HelpService) self._services.add_transient(InstallService) diff --git a/src/tests/custom/general/src/general/__init__.py b/src/tests/custom/general/src/general/__init__.py deleted file mode 100644 index e69de29b..00000000