Added cli command add

This commit is contained in:
Sven Heidemann 2021-04-11 15:36:04 +02:00
parent 5987b71c80
commit 6af9328e14
4 changed files with 121 additions and 1 deletions

View File

@ -4,6 +4,7 @@ from cpl.application.application_abc import ApplicationABC
from cpl.configuration.configuration_abc import ConfigurationABC from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.console.console import Console from cpl.console.console import Console
from cpl.dependency_injection import ServiceProviderABC 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.build_service import BuildService
from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.install_service import InstallService from cpl_cli.command.install_service import InstallService
@ -33,7 +34,8 @@ class CLI(ApplicationABC):
def configure(self): def configure(self):
self._command_handler: CommandHandler = self._services.get_service(CommandHandler) 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('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('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('install', ['i', 'I'], InstallService, False, True, True))
@ -60,10 +62,14 @@ class CLI(ApplicationABC):
else: else:
for cmd in self._command_handler.commands: for cmd in self._command_handler.commands:
result = self._configuration.get_configuration(cmd.name) result = self._configuration.get_configuration(cmd.name)
result_args = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments')
if result is not None: if result is not None:
command = cmd.name command = cmd.name
args.append(result) args.append(result)
for arg in result_args:
args.append(arg)
if command is None: if command is None:
Error.error(f'Expected command') Error.error(f'Expected command')
return return

View File

@ -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
)

View File

@ -19,6 +19,7 @@ class HelpService(CommandABC):
""" """
Console.write_line('Available Commands:') Console.write_line('Available Commands:')
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.'], ['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.'], ['generate (g|G)', 'Generate a new file.'],
['help (h|H)', 'Lists available command and their short descriptions.'], ['help (h|H)', 'Lists available command and their short descriptions.'],

View File

@ -5,6 +5,7 @@ from cpl.configuration.console_argument import ConsoleArgument
from cpl.configuration.configuration_abc import ConfigurationABC from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.dependency_injection.service_collection_abc import ServiceCollectionABC from cpl.dependency_injection.service_collection_abc import ServiceCollectionABC
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC 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.build_service import BuildService
from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.install_service import InstallService from cpl_cli.command.install_service import InstallService
@ -41,6 +42,8 @@ class Startup(StartupABC):
self._configuration.add_environment_variables('CPL_') self._configuration.add_environment_variables('CPL_')
self._configuration.add_json_file('appsettings.json', path=self._env.runtime_directory, self._configuration.add_json_file('appsettings.json', path=self._env.runtime_directory,
optional=False, output=False) 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('', 'build', ['b', 'B'], ''))
self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', console_arguments=[ self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', console_arguments=[
ConsoleArgument('', 'abc', ['a', 'A'], ' '), ConsoleArgument('', 'abc', ['a', 'A'], ' '),
@ -74,6 +77,7 @@ class Startup(StartupABC):
self._services.add_transient(PublisherABC, PublisherService) self._services.add_transient(PublisherABC, PublisherService)
self._services.add_transient(LiveServerService) self._services.add_transient(LiveServerService)
self._services.add_transient(AddService)
self._services.add_transient(BuildService) self._services.add_transient(BuildService)
self._services.add_transient(GenerateService) self._services.add_transient(GenerateService)
self._services.add_transient(HelpService) self._services.add_transient(HelpService)