Improved & added commands
This commit is contained in:
@@ -33,32 +33,33 @@ class CLI(ApplicationABC):
|
||||
"""
|
||||
ApplicationABC.__init__(self, config, services)
|
||||
|
||||
self._command_handler: Optional[CommandHandler] = None
|
||||
# self._command_handler: Optional[CommandHandler] = None
|
||||
self._options: list[str] = []
|
||||
|
||||
def configure(self):
|
||||
self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
|
||||
|
||||
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))
|
||||
self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, False, True))
|
||||
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, False, True, True))
|
||||
self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, True, False))
|
||||
self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, False, True, True))
|
||||
self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, False, True, True))
|
||||
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')
|
||||
pass
|
||||
# self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
|
||||
#
|
||||
# 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))
|
||||
# self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, False, True))
|
||||
# self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, False, True, True))
|
||||
# self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, True, False))
|
||||
# self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, False, True, True))
|
||||
# self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, False, True, True))
|
||||
# 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')
|
||||
|
||||
def main(self):
|
||||
"""
|
||||
@@ -66,61 +67,62 @@ class CLI(ApplicationABC):
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
command = None
|
||||
args = []
|
||||
if len(self._configuration.additional_arguments) > 0:
|
||||
is_option = False
|
||||
for opt in self._options:
|
||||
if opt in self._configuration.additional_arguments:
|
||||
is_option = True
|
||||
command = opt
|
||||
args = self._configuration.additional_arguments
|
||||
args.remove(opt)
|
||||
|
||||
if not is_option:
|
||||
command = self._configuration.additional_arguments[0]
|
||||
if len(self._configuration.additional_arguments) > 1:
|
||||
args = self._configuration.additional_arguments[1:]
|
||||
else:
|
||||
for cmd in self._command_handler.commands:
|
||||
result = self._configuration.get_configuration(cmd.name)
|
||||
result_args: list[str] = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments')
|
||||
is_option = False
|
||||
if result is None:
|
||||
continue
|
||||
|
||||
for opt in self._options:
|
||||
if opt == result:
|
||||
is_option = True
|
||||
command = opt
|
||||
|
||||
elif result_args is not None and opt in result_args:
|
||||
is_option = True
|
||||
command = opt
|
||||
result_args.remove(opt)
|
||||
|
||||
if is_option:
|
||||
args.append(cmd.name)
|
||||
if result_args is not None:
|
||||
for arg in result_args:
|
||||
args.append(arg)
|
||||
|
||||
elif result is not None:
|
||||
command = cmd.name
|
||||
args.append(result)
|
||||
|
||||
for arg in result_args:
|
||||
args.append(arg)
|
||||
|
||||
else:
|
||||
Error.error(f'Unexpected command')
|
||||
return
|
||||
|
||||
if command is None:
|
||||
Error.error(f'Expected command')
|
||||
return
|
||||
|
||||
self._command_handler.handle(command, args)
|
||||
pass
|
||||
# command = None
|
||||
# args = []
|
||||
# if len(self._configuration.additional_arguments) > 0:
|
||||
# is_option = False
|
||||
# for opt in self._options:
|
||||
# if opt in self._configuration.additional_arguments:
|
||||
# is_option = True
|
||||
# command = opt
|
||||
# args = self._configuration.additional_arguments
|
||||
# args.remove(opt)
|
||||
#
|
||||
# if not is_option:
|
||||
# command = self._configuration.additional_arguments[0]
|
||||
# if len(self._configuration.additional_arguments) > 1:
|
||||
# args = self._configuration.additional_arguments[1:]
|
||||
# else:
|
||||
# for cmd in self._command_handler.commands:
|
||||
# result = self._configuration.get_configuration(cmd.name)
|
||||
# result_args: list[str] = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments')
|
||||
# is_option = False
|
||||
# if result is None:
|
||||
# continue
|
||||
#
|
||||
# for opt in self._options:
|
||||
# if opt == result:
|
||||
# is_option = True
|
||||
# command = opt
|
||||
#
|
||||
# elif result_args is not None and opt in result_args:
|
||||
# is_option = True
|
||||
# command = opt
|
||||
# result_args.remove(opt)
|
||||
#
|
||||
# if is_option:
|
||||
# args.append(cmd.name)
|
||||
# if result_args is not None:
|
||||
# for arg in result_args:
|
||||
# args.append(arg)
|
||||
#
|
||||
# elif result is not None:
|
||||
# command = cmd.name
|
||||
# args.append(result)
|
||||
#
|
||||
# for arg in result_args:
|
||||
# args.append(arg)
|
||||
#
|
||||
# else:
|
||||
# Error.error(f'Unexpected command')
|
||||
# return
|
||||
#
|
||||
# if command is None:
|
||||
# Error.error(f'Expected command')
|
||||
# return
|
||||
#
|
||||
# self._command_handler.handle(command, args)
|
||||
except KeyboardInterrupt:
|
||||
Console.write_line()
|
||||
sys.exit()
|
||||
|
@@ -22,7 +22,6 @@ class AddService(CommandABC):
|
||||
CommandABC.__init__(self)
|
||||
|
||||
self._config = config
|
||||
|
||||
self._workspace = workspace
|
||||
|
||||
@property
|
||||
@@ -104,15 +103,15 @@ class AddService(CommandABC):
|
||||
Console.error(f'Invalid target: {target}')
|
||||
return
|
||||
|
||||
if target in build_settings.project_references:
|
||||
Console.error(f'Project reference already exists.')
|
||||
return
|
||||
|
||||
if self._workspace is None:
|
||||
target = f'../{target}'
|
||||
else:
|
||||
target = target.replace('src', '..')
|
||||
|
||||
if target in build_settings.project_references:
|
||||
Console.error(f'Project reference already exists.')
|
||||
return
|
||||
|
||||
build_settings.project_references.append(target)
|
||||
|
||||
Console.spinner(
|
||||
|
@@ -70,7 +70,6 @@ class RemoveService(CommandABC):
|
||||
:param args:
|
||||
:return:
|
||||
"""
|
||||
|
||||
project_name = args[0]
|
||||
if project_name not in self._workspace.projects:
|
||||
Console.error(f'Project {project_name} not found in workspace.')
|
||||
|
@@ -1,7 +1,9 @@
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
from cpl_core.configuration.executable_argument import ExecutableArgument
|
||||
|
||||
class CommandABC(ABC):
|
||||
|
||||
class CommandABC(ExecutableArgument):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
@@ -10,6 +12,3 @@ class CommandABC(ABC):
|
||||
@property
|
||||
@abstractmethod
|
||||
def help_message(self) -> str: pass
|
||||
|
||||
@abstractmethod
|
||||
def run(self, args: list[str]): pass
|
||||
|
@@ -42,6 +42,8 @@
|
||||
"*.json"
|
||||
]
|
||||
},
|
||||
"ProjectReferences": []
|
||||
"ProjectReferences": [
|
||||
"../cpl_core/cpl_core.json"
|
||||
]
|
||||
}
|
||||
}
|
@@ -2,8 +2,9 @@ import os
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.application.startup_abc import StartupABC
|
||||
from cpl_core.configuration.argument_abc import ConsoleArgument
|
||||
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.console import Console
|
||||
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
|
||||
from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC
|
||||
from cpl_cli.command.add_service import AddService
|
||||
@@ -40,52 +41,48 @@ class Startup(StartupABC):
|
||||
configuration.add_environment_variables('PYTHON_')
|
||||
configuration.add_environment_variables('CPL_')
|
||||
configuration.add_json_file('appsettings.json', path=environment.runtime_directory, optional=False, output=False)
|
||||
configuration.add_json_file('cpl-workspace.json', path=environment.working_directory, optional=True, output=False)
|
||||
|
||||
configuration.add_console_argument(ConsoleArgument('', 'add', ['a', 'a'], ' '))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'build', ['b', 'B'], ''))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', console_arguments=[
|
||||
ConsoleArgument('', 'abc', ['a', 'A'], ' '),
|
||||
ConsoleArgument('', 'class', ['c', 'C'], ' '),
|
||||
ConsoleArgument('', 'enum', ['e', 'E'], ' '),
|
||||
ConsoleArgument('', 'service', ['s', 'S'], ' '),
|
||||
ConsoleArgument('', 'settings', ['st', 'ST'], ' '),
|
||||
ConsoleArgument('', 'thread', ['t', 't'], ' ')
|
||||
]))
|
||||
configuration.add_console_argument(
|
||||
ConsoleArgument('', 'help', ['h', 'H'], ' ', is_value_token_optional=True)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'add', ['a', 'A'], AddService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'build', ['b', 'B'], BuildService)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'generate', ['g', 'G'], GenerateService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'abc', ['a', 'A'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'class', ['c', 'C'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'enum', ['e', 'E'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'service', ['s', 'S'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'settings', ['st', 'ST'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'thread', ['t', 't'], ' ')
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'install', ['i', 'I'], InstallService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'virtual', ['v', 'V']) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'new', ['n', 'N'], NewService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'console', ['c', 'C'], ' ') \
|
||||
.add_console_argument(ArgumentTypeEnum.Variable, '', 'library', ['l', 'L'], ' ')
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'publish', ['p', 'P'], PublishService)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'remove', ['r', 'R'], RemoveService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'start', ['S', 'S'], StartService)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'uninstall', ['ui', 'UI'], UninstallService) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'virtual', ['v', 'V']) \
|
||||
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'update', ['u', 'U'], UpdateService)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'version', ['v', 'V'], VersionService)
|
||||
|
||||
configuration.for_each_argument(
|
||||
lambda a: a.add_console_argument(ArgumentTypeEnum.Executable, '--', 'help', ['h', 'H'], HelpService)
|
||||
)
|
||||
configuration.add_console_argument(
|
||||
ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True, console_arguments= [
|
||||
ConsoleArgument('', '--virtual', ['--v', '--V'], ''),
|
||||
ConsoleArgument('', '--simulate', ['--s', '--S'], ''),
|
||||
])
|
||||
)
|
||||
configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', console_arguments=[
|
||||
ConsoleArgument('', 'console', ['c', 'C'], ' '),
|
||||
ConsoleArgument('', 'library', ['l', 'L'], ' ')
|
||||
]))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'publish', ['p', 'P'], ''))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'remove', ['r', 'R'], ' '))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'start', ['s', 'S'], ''))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'uninstall', ['ui', 'UI'], ' '))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], ''))
|
||||
configuration.add_console_argument(ConsoleArgument('', 'version', ['v', 'V'], ''))
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', 'help', ['h', 'H'], HelpService)
|
||||
|
||||
configuration.add_console_argument(ConsoleArgument('', '--help', ['-h', '-H'], ''))
|
||||
|
||||
if os.path.isfile(os.path.join(environment.working_directory, 'cpl-workspace.json')):
|
||||
configuration.add_json_file('cpl-workspace.json', optional=True, output=False)
|
||||
workspace: Optional[WorkspaceSettings] = configuration.get_configuration(WorkspaceSettings)
|
||||
workspace: Optional[WorkspaceSettings] = configuration.get_configuration(WorkspaceSettings)
|
||||
if workspace is not None:
|
||||
for script in workspace.scripts:
|
||||
configuration.add_console_argument(
|
||||
ConsoleArgument('', script, [], ' ', is_value_token_optional=True))
|
||||
|
||||
configuration.parse_console_arguments(error=False)
|
||||
configuration.create_console_argument(ArgumentTypeEnum.Executable, '', script, [], CustomScriptService)
|
||||
|
||||
return configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_singleton(CommandHandler)
|
||||
# services.add_singleton(CommandHandler)
|
||||
|
||||
services.add_transient(PublisherABC, PublisherService)
|
||||
services.add_transient(LiveServerService)
|
||||
|
Reference in New Issue
Block a user