3 Commits

4 changed files with 90 additions and 35 deletions

6
.vscode/launch.json vendored
View File

@@ -201,7 +201,11 @@
"args": [ "args": [
"install", "install",
"discord.py", "discord.py",
] ],
"env": {
"GISMO_ENVIRONMENT": "development",
"PYTHONPATH": "${workspaceFolder}/src/:$PYTHONPATH"
}
}, },
{ {
"name": "CLI: install local", "name": "CLI: install local",

View File

@@ -2,20 +2,21 @@ import json
import os import os
import subprocess import subprocess
import textwrap import textwrap
import time
from packaging import version
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
from cpl_core.utils.pip import Pip
from cpl_cli.cli_settings import CLISettings from cpl_cli.cli_settings import CLISettings
from cpl_cli.command_abc import CommandABC from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration.build_settings import BuildSettings from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.project_settings import ProjectSettings from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.configuration.settings_helper import SettingsHelper from cpl_cli.configuration.settings_helper import SettingsHelper
from cpl_cli.error import Error from cpl_cli.error import Error
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_core.environment.application_environment_abc import \
ApplicationEnvironmentABC
from cpl_core.utils.pip import Pip
from packaging import version
class InstallService(CommandABC): class InstallService(CommandABC):
@@ -38,6 +39,9 @@ class InstallService(CommandABC):
self._project_settings = project_settings self._project_settings = project_settings
self._cli_settings = cli_settings self._cli_settings = cli_settings
self._is_simulating = False
self._is_virtual = False
self._project_file = f'{self._config.get_configuration("ProjectName")}.json' self._project_file = f'{self._config.get_configuration("ProjectName")}.json'
@property @property
@@ -50,6 +54,9 @@ class InstallService(CommandABC):
package The package to install package The package to install
""") """)
def _wait(self, t: int, *args, source: str = None, stdout=None, stderr=None):
time.sleep(t)
def _install_project(self): def _install_project(self):
""" """
Installs dependencies of CPl project Installs dependencies of CPl project
@@ -64,11 +71,12 @@ class InstallService(CommandABC):
Error.error(f'Found invalid dependencies in {self._project_file}.') Error.error(f'Found invalid dependencies in {self._project_file}.')
return return
if not self._is_virtual:
Pip.set_executable(self._project_settings.python_executable) Pip.set_executable(self._project_settings.python_executable)
for dependency in self._project_settings.dependencies: for dependency in self._project_settings.dependencies:
Console.spinner( Console.spinner(
f'Installing: {dependency}', f'Installing: {dependency}',
Pip.install, dependency, Pip.install if not self._is_virtual else self._wait, dependency if not self._is_virtual else 2,
source=self._cli_settings.pip_path if 'sh_cpl' in dependency else None, source=self._cli_settings.pip_path if 'sh_cpl' in dependency else None,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
@@ -76,6 +84,7 @@ class InstallService(CommandABC):
spinner_foreground_color=ForegroundColorEnum.cyan spinner_foreground_color=ForegroundColorEnum.cyan
) )
if not self._is_virtual:
Pip.reset_executable() Pip.reset_executable()
def _install_package(self, package: str): def _install_package(self, package: str):
@@ -85,6 +94,7 @@ class InstallService(CommandABC):
:return: :return:
""" """
is_already_in_project = False is_already_in_project = False
if not self._is_virtual:
Pip.set_executable(self._project_settings.python_executable) Pip.set_executable(self._project_settings.python_executable)
if self._project_settings is None or self._build_settings is None: if self._project_settings is None or self._build_settings is None:
@@ -129,15 +139,18 @@ class InstallService(CommandABC):
Console.spinner( Console.spinner(
f'Installing: {package}', f'Installing: {package}',
Pip.install, package, Pip.install if not self._is_virtual else self._wait, package if not self._is_virtual else 2,
source=self._cli_settings.pip_path if 'sh_cpl' in package else None, source=self._cli_settings.pip_path if 'sh_cpl' in package else None,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
text_foreground_color=ForegroundColorEnum.green, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan spinner_foreground_color=ForegroundColorEnum.cyan
) )
if self._is_virtual:
new_package = name
else:
new_package = Pip.get_package(name) new_package = Pip.get_package(name)
Console.write_line(new_package)
if new_package is None \ if new_package is None \
or '==' in package and \ or '==' in package and \
version.parse(package.split('==')[1]) != version.parse(new_package.split('==')[1]): version.parse(package.split('==')[1]) != version.parse(new_package.split('==')[1]):
@@ -159,6 +172,7 @@ class InstallService(CommandABC):
self._project_settings.dependencies.append(new_name) self._project_settings.dependencies.append(new_name)
if not self._is_simulating:
config = { config = {
ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings), ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings),
BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings) BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings)
@@ -176,6 +190,16 @@ class InstallService(CommandABC):
:param args: :param args:
:return: :return:
""" """
if '--virtual' in args:
self._is_virtual = True
args.remove('--virtual')
Console.write_line('Running in virtual mode:')
if '--simulate' in args:
self._is_virtual = True
args.remove('--simulate')
Console.write_line('Running in simulation mode:')
if len(args) == 0: if len(args) == 0:
self._install_project() self._install_project()
else: else:

View File

@@ -2,6 +2,7 @@ import json
import os import os
import subprocess import subprocess
import textwrap import textwrap
import time
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
@@ -32,6 +33,9 @@ class UninstallService(CommandABC):
self._build_settings = build_settings self._build_settings = build_settings
self._project_settings = project_settings self._project_settings = project_settings
self._is_simulating = False
self._is_virtual = False
@property @property
def help_message(self) -> str: def help_message(self) -> str:
return textwrap.dedent("""\ return textwrap.dedent("""\
@@ -42,6 +46,9 @@ class UninstallService(CommandABC):
package The package to uninstall package The package to uninstall
""") """)
def _wait(self, t: int, *args, source: str = None, stdout=None, stderr=None):
time.sleep(t)
def run(self, args: list[str]): def run(self, args: list[str]):
""" """
Entry point of command Entry point of command
@@ -53,12 +60,27 @@ class UninstallService(CommandABC):
Console.error(f'Usage: cpl uninstall <package>') Console.error(f'Usage: cpl uninstall <package>')
return return
if '--virtual' in args:
self._is_virtual = True
args.remove('--virtual')
Console.write_line('Running in virtual mode:')
if '--simulate' in args:
self._is_virtual = True
args.remove('--simulate')
Console.write_line('Running in simulation mode:')
if not self._is_virtual:
Pip.set_executable(self._project_settings.python_executable) Pip.set_executable(self._project_settings.python_executable)
package = args[0] package = args[0]
is_in_dependencies = False is_in_dependencies = False
if not self._is_virtual:
pip_package = Pip.get_package(package) pip_package = Pip.get_package(package)
else:
pip_package = package
for dependency in self._project_settings.dependencies: for dependency in self._project_settings.dependencies:
if package in dependency: if package in dependency:
@@ -74,7 +96,7 @@ class UninstallService(CommandABC):
Console.spinner( Console.spinner(
f'Uninstalling: {package}', f'Uninstalling: {package}',
Pip.uninstall, package, Pip.uninstall if not self._is_virtual else self._wait, package if not self._is_virtual else 2,
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
text_foreground_color=ForegroundColorEnum.green, text_foreground_color=ForegroundColorEnum.green,
@@ -83,6 +105,7 @@ class UninstallService(CommandABC):
if package in self._project_settings.dependencies: if package in self._project_settings.dependencies:
self._project_settings.dependencies.remove(package) self._project_settings.dependencies.remove(package)
if not self._is_simulating:
config = { config = {
ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings), ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings),
BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings) BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings)
@@ -92,4 +115,5 @@ class UninstallService(CommandABC):
project_file.close() project_file.close()
Console.write_line(f'Removed {package}') Console.write_line(f'Removed {package}')
if not self._is_virtual:
Pip.reset_executable() Pip.reset_executable()

View File

@@ -55,7 +55,10 @@ class Startup(StartupABC):
ConsoleArgument('', 'help', ['h', 'H'], ' ', is_value_token_optional=True) ConsoleArgument('', 'help', ['h', 'H'], ' ', is_value_token_optional=True)
) )
configuration.add_console_argument( configuration.add_console_argument(
ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True) 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=[ configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', console_arguments=[
ConsoleArgument('', 'console', ['c', 'C'], ' '), ConsoleArgument('', 'console', ['c', 'C'], ' '),