2021.10 #41

Merged
edraft merged 53 commits from 2021.10 into master 2021-10-04 09:32:42 +02:00
9 changed files with 71 additions and 1 deletions
Showing only changes of commit be6838f97b - Show all commits

View File

@ -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"
}
}
}

1
scripts/hello_world.py Normal file
View File

@ -0,0 +1 @@
print('Hello World')

View File

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

View File

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

View File

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

View File

@ -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()}')

View File

@ -5,3 +5,4 @@ class WorkspaceSettingsNameEnum(Enum):
default_project = 'DefaultProject'
projects = 'Projects'
scripts = 'Scripts'

View File

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