Bugfixes in install command

This commit is contained in:
Sven Heidemann 2021-03-16 22:39:18 +01:00
parent dbb0a4a2df
commit f35f5167f0
2 changed files with 21 additions and 22 deletions

View File

@ -34,7 +34,7 @@ class CLI(ApplicationABC):
self._command_handler.add_command(CommandModel('build', ['h', 'B'], BuildService, True)) self._command_handler.add_command(CommandModel('build', ['h', 'B'], BuildService, True))
self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, True)) self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, True))
self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False)) self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False))
self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, False)) self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, True))
self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False)) self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False))
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, True)) self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, True))
self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, True)) self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, True))

View File

@ -5,7 +5,6 @@ import subprocess
from packaging import version from packaging import version
from cpl.application.application_runtime_abc import ApplicationRuntimeABC from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.configuration import ConfigurationABC
from cpl.console.console import Console from cpl.console.console import Console
from cpl.console.foreground_color_enum import ForegroundColorEnum from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl.utils.pip import Pip from cpl.utils.pip import Pip
@ -19,16 +18,20 @@ from cpl_cli.error import Error
class InstallService(CommandABC): class InstallService(CommandABC):
def __init__(self, runtime: ApplicationRuntimeABC, configuration: ConfigurationABC, cli_settings: CLISettings): def __init__(self, runtime: ApplicationRuntimeABC, build_settings: BuildSettings, project_settings: ProjectSettings,
cli_settings: CLISettings):
""" """
Service for the CLI command install Service for the CLI command install
:param runtime: :param runtime:
:param configuration: :param build_settings:
:param project_settings:
:param cli_settings:
""" """
CommandABC.__init__(self) CommandABC.__init__(self)
self._runtime = runtime self._runtime = runtime
self._config = configuration self._build_settings = build_settings
self._project_settings = project_settings
self._cli_settings = cli_settings self._cli_settings = cli_settings
def _install_project(self): def _install_project(self):
@ -36,19 +39,17 @@ class InstallService(CommandABC):
Installs dependencies of CPl project Installs dependencies of CPl project
:return: :return:
""" """
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
build: BuildSettings = self._config.get_configuration(BuildSettings)
if project is None or build is None: if self._project_settings is None or self._build_settings is None:
Error.error('The command requires to be run in an CPL project, but a project could not be found.') Error.error('The command requires to be run in an CPL project, but a project could not be found.')
return return
if project.dependencies is None: if self._project_settings.dependencies is None:
Error.error('Found invalid dependencies in cpl.json.') Error.error('Found invalid dependencies in cpl.json.')
return return
Pip.set_executable(project.python_path) Pip.set_executable(self._project_settings.python_path)
for dependency in project.dependencies: for dependency in self._project_settings.dependencies:
Console.spinner( Console.spinner(
f'Installing: {dependency}', f'Installing: {dependency}',
Pip.install, dependency, Pip.install, dependency,
@ -68,15 +69,13 @@ class InstallService(CommandABC):
:return: :return:
""" """
is_already_in_project = False is_already_in_project = False
project: ProjectSettings = self._config.get_configuration(ProjectSettings) Pip.set_executable(self._project_settings.python_path)
build: BuildSettings = self._config.get_configuration(BuildSettings)
Pip.set_executable(project.python_path)
if project is None or build is None: if self._project_settings is None or self._build_settings is None:
Error.error('The command requires to be run in an CPL project, but a project could not be found.') Error.error('The command requires to be run in an CPL project, but a project could not be found.')
return return
if project.dependencies is None: if self._project_settings.dependencies is None:
Error.error('Found invalid dependencies in cpl.json.') Error.error('Found invalid dependencies in cpl.json.')
return return
@ -87,7 +86,7 @@ class InstallService(CommandABC):
package_version = package.split('==')[1] package_version = package.split('==')[1]
to_remove_list = [] to_remove_list = []
for dependency in project.dependencies: for dependency in self._project_settings.dependencies:
dependency_version = '' dependency_version = ''
if '==' in dependency: if '==' in dependency:
@ -101,10 +100,10 @@ class InstallService(CommandABC):
is_already_in_project = True is_already_in_project = True
for to_remove in to_remove_list: for to_remove in to_remove_list:
project.dependencies.remove(to_remove) self._project_settings.dependencies.remove(to_remove)
local_package = Pip.get_package(package) local_package = Pip.get_package(package)
if local_package is not None and local_package in project.dependencies: if local_package is not None and local_package in self._project_settings.dependencies:
Error.warn(f'Package {local_package} is already installed.') Error.warn(f'Package {local_package} is already installed.')
return return
@ -131,11 +130,11 @@ class InstallService(CommandABC):
if new_package is None: if new_package is None:
new_package = package new_package = package
project.dependencies.append(new_package) self._project_settings.dependencies.append(new_package)
config = { config = {
ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(project), ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings),
BuildSettings.__name__: SettingsHelper.get_build_settings_dict(build) BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings)
} }
with open(os.path.join(self._runtime.working_directory, 'cpl.json'), 'w') as project_file: with open(os.path.join(self._runtime.working_directory, 'cpl.json'), 'w') as project_file:
project_file.write(json.dumps(config, indent=2)) project_file.write(json.dumps(config, indent=2))