From 6af9328e149d27502235e82dd7875c0983eb7260 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Apr 2021 15:36:04 +0200 Subject: [PATCH] Added cli command add --- src/cpl_cli/cli.py | 8 +- src/cpl_cli/command/add_service.py | 109 ++++++++++++++++++++++++++++ src/cpl_cli/command/help_service.py | 1 + src/cpl_cli/startup.py | 4 + 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/cpl_cli/command/add_service.py diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index d249417c..145abc12 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -4,6 +4,7 @@ from cpl.application.application_abc import ApplicationABC 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.generate_service import GenerateService from cpl_cli.command.install_service import InstallService @@ -33,7 +34,8 @@ 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, False, True, True)) + self._command_handler.add_command(CommandModel('add', ['a', 'a'], AddService, False, False, False)) + self._command_handler.add_command(CommandModel('build', ['b', '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)) @@ -60,10 +62,14 @@ class CLI(ApplicationABC): else: for cmd in self._command_handler.commands: result = self._configuration.get_configuration(cmd.name) + result_args = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments') if result is not None: command = cmd.name args.append(result) + for arg in result_args: + args.append(arg) + if command is None: Error.error(f'Expected command') return diff --git a/src/cpl_cli/command/add_service.py b/src/cpl_cli/command/add_service.py new file mode 100644 index 00000000..3bed17bc --- /dev/null +++ b/src/cpl_cli/command/add_service.py @@ -0,0 +1,109 @@ +import json +import os.path +from typing import Optional + +from cpl.configuration.configuration_abc import ConfigurationABC +from cpl.console.console import Console +from cpl.console.foreground_color_enum import ForegroundColorEnum +from cpl_cli.command_abc import CommandABC +from cpl_cli.configuration.build_settings import BuildSettings +from cpl_cli.configuration.project_settings import ProjectSettings +from cpl_cli.configuration.settings_helper import SettingsHelper +from cpl_cli.configuration.workspace_settings import WorkspaceSettings + + +class AddService(CommandABC): + + def __init__(self, config: ConfigurationABC, workspace: WorkspaceSettings): + """ + Service for CLI command add + """ + CommandABC.__init__(self) + + self._config = config + + self._workspace = workspace + + @staticmethod + def _edit_project_file(source: str, project_settings: ProjectSettings, build_settings: BuildSettings): + with open(source, 'w') as file: + file.write(json.dumps({ + ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(project_settings), + BuildSettings.__name__: SettingsHelper.get_build_settings_dict(build_settings) + }, indent=2)) + file.close() + + def run(self, args: list[str]): + """ + Entry point of command + :param args: + :return: + """ + if len(args) == 0: + Console.error('Expected source and target project') + return + + elif len(args) == 1: + Console.error('Expected target project') + return + + elif len(args) > 2: + Console.error(f'Unexpected argument: {" ".join(args[2:])}') + return + + # file names + source = args[0] + target = args[1] + # validation flags + is_invalid_source = False + is_invalid_target = source == target + + if not is_invalid_target: + if self._workspace is None: + is_invalid_source = not os.path.isfile(source) + is_invalid_target = not os.path.isfile(target) + + else: + if source not in self._workspace.projects: + is_invalid_source = True + + else: + source = self._workspace.projects[source] + + if target not in self._workspace.projects: + is_invalid_target = True + + else: + target = self._workspace.projects[target] + + # load project-name.json + self._config.add_json_file(source, optional=True, output=False) + project_settings: Optional[ProjectSettings] = self._config.get_configuration(ProjectSettings) + build_settings: Optional[BuildSettings] = self._config.get_configuration(BuildSettings) + + if project_settings is None or build_settings is None: + is_invalid_source = True + + if is_invalid_source: + Console.error(f'Invalid source: {source}') + return + + if is_invalid_target or source == target or not os.path.isfile(target): + Console.error(f'Invalid target: {target}') + return + + if target in build_settings.project_references: + Console.error(f'Project reference already exists.') + return + + build_settings.project_references.append(f'../{target}') + + Console.spinner( + f'Editing {source}', + self._edit_project_file, + source, + project_settings, + build_settings, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) diff --git a/src/cpl_cli/command/help_service.py b/src/cpl_cli/command/help_service.py index 5ccbf326..fb96a419 100644 --- a/src/cpl_cli/command/help_service.py +++ b/src/cpl_cli/command/help_service.py @@ -19,6 +19,7 @@ class HelpService(CommandABC): """ Console.write_line('Available Commands:') commands = [ + ['add (a|a)', 'Adds a project reference to given project.'], ['build (b|B)', 'Prepares files for publish into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.'], ['generate (g|G)', 'Generate a new file.'], ['help (h|H)', 'Lists available command and their short descriptions.'], diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index d2ad9ec6..84e77c8d 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -5,6 +5,7 @@ from cpl.configuration.console_argument import ConsoleArgument from cpl.configuration.configuration_abc import ConfigurationABC 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.generate_service import GenerateService from cpl_cli.command.install_service import InstallService @@ -41,6 +42,8 @@ class Startup(StartupABC): self._configuration.add_environment_variables('CPL_') self._configuration.add_json_file('appsettings.json', path=self._env.runtime_directory, optional=False, output=False) + + self._configuration.add_console_argument(ConsoleArgument('', 'add', ['a', 'a'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'build', ['b', 'B'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', console_arguments=[ ConsoleArgument('', 'abc', ['a', 'A'], ' '), @@ -74,6 +77,7 @@ class Startup(StartupABC): self._services.add_transient(PublisherABC, PublisherService) self._services.add_transient(LiveServerService) + self._services.add_transient(AddService) self._services.add_transient(BuildService) self._services.add_transient(GenerateService) self._services.add_transient(HelpService)