From 7a65dbe39b2bc78cff44d2cde317dcc4be63629c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 24 May 2022 22:28:38 +0200 Subject: [PATCH] Readded logic to handle pre and post scripts from workspace --- src/cpl_cli/command/custom_script_service.py | 14 ++++++----- src/cpl_core/configuration/configuration.py | 26 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/cpl_cli/command/custom_script_service.py b/src/cpl_cli/command/custom_script_service.py index 4ed3d91f..4208b8d5 100644 --- a/src/cpl_cli/command/custom_script_service.py +++ b/src/cpl_cli/command/custom_script_service.py @@ -26,9 +26,11 @@ class CustomScriptService(CommandABC): cmd = self._config.get_configuration('ACTIVE_EXECUTABLE') for script in self._workspace.scripts: - if script == cmd: - command = self._workspace.scripts[script] - try: - subprocess.run(command, shell=True if os.name == 'posix' else None) - except Exception as e: - Console.error(str(e)) + if script != cmd: + continue + + command = self._workspace.scripts[script] + try: + subprocess.run(command, shell=True if os.name == 'posix' else None) + except Exception as e: + Console.error(str(e)) diff --git a/src/cpl_core/configuration/configuration.py b/src/cpl_core/configuration/configuration.py index 748590e3..4746808b 100644 --- a/src/cpl_core/configuration/configuration.py +++ b/src/cpl_core/configuration/configuration.py @@ -5,6 +5,8 @@ import traceback from collections.abc import Callable from typing import Union, Type, Optional +from cpl_cli.command.custom_script_service import CustomScriptService +from cpl_cli.configuration import WorkspaceSettings from cpl_core.configuration.argument_abc import ArgumentABC from cpl_core.configuration.argument_builder import ArgumentBuilder from cpl_core.configuration.argument_executable_abc import ArgumentExecutableABC @@ -154,6 +156,28 @@ class Configuration(ConfigurationABC): self._print_error(__name__, f'Cannot load config file: {file}! -> {e}') return {} + def _handle_pre_or_post_executables(self, pre: bool, argument: ExecutableArgument, services: ServiceProviderABC): + script_type = 'pre-' if pre else 'post-' + workspace: Optional[WorkspaceSettings] = self.get_configuration(WorkspaceSettings) + if workspace is None or len(workspace.scripts) == 0: + return + + for script in workspace.scripts: + if script_type not in script and not script.startswith(script_type): + continue + + # split in two ifs to prevent exception + if script.split(script_type)[1] != argument.name: + continue + + css: CustomScriptService = services.get_service(CustomScriptService) + if css is None: + continue + + Console.write_line() + self.add_configuration('ACTIVE_EXECUTABLE', script) + css.run([]) + def _parse_arguments(self, executables: list[ArgumentABC], arg_list: list[str], args_types: list[ArgumentABC]): for i in range(0, len(arg_list)): arg_str = arg_list[i] @@ -308,8 +332,10 @@ class Configuration(ConfigurationABC): sys.exit() cmd: ArgumentExecutableABC = services.get_service(exe.executable_type) + self._handle_pre_or_post_executables(True, exe, services) self.add_configuration('ACTIVE_EXECUTABLE', exe.name) cmd.execute(self._additional_arguments) + self._handle_pre_or_post_executables(False, exe, services) prevent = exe.prevent_next_executable success = True except Exception as e: