Improved workspace handling in start command

This commit is contained in:
2021-04-08 19:30:00 +02:00
parent 806888bf49
commit 5f668e17c9
11 changed files with 68 additions and 50 deletions

View File

@@ -6,21 +6,29 @@ from datetime import datetime
from cpl.console.console import Console
from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
from cpl_cli.configuration import BuildSettings
class LiveServerThread(threading.Thread):
def __init__(self, path: str, build_settings: BuildSettings, args: list[str]):
def __init__(self, executable: str, path: str, args: list[str], env: ApplicationEnvironmentABC,
build_settings: BuildSettings):
"""
Thread to start the CPL project for the live development server
:param executable:
:param path:
:param args:
:param env:
:param build_settings:
"""
threading.Thread.__init__(self)
self._executable = executable
self._path = path
self._build_settings = build_settings
self._args = args
self._env = env
self._build_settings = build_settings
self._main = ''
self._command = []
@@ -38,11 +46,9 @@ class LiveServerThread(threading.Thread):
Starts the CPL project
:return:
"""
src_path = ''
main = self._build_settings.main
if '.' in self._build_settings.main:
length = len(self._build_settings.main.split('.')) - 1
src_path = self._path.replace(f'{"/".join(self._build_settings.main.split(".")[:length])}/', '')
main = self._build_settings.main.split('.')[length]
self._main = os.path.join(self._path, f'{main}.py')
@@ -52,11 +58,11 @@ class LiveServerThread(threading.Thread):
env_vars = os.environ
if sys.platform == 'win32':
env_vars['PYTHONPATH'] = f'{os.path.dirname(src_path)};' \
f'{os.path.join(os.path.dirname(src_path), self._build_settings.source_path)}'
env_vars['PYTHONPATH'] = f'{self._env.working_directory};' \
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
else:
env_vars['PYTHONPATH'] = f'{os.path.dirname(src_path)}:' \
f'{os.path.join(os.path.dirname(src_path), self._build_settings.source_path)}'
env_vars['PYTHONPATH'] = f'{self._env.working_directory}:' \
f'{os.path.join(self._env.working_directory, self._build_settings.source_path)}'
Console.set_foreground_color(ForegroundColorEnum.green)
Console.write_line('Read successfully')
@@ -65,5 +71,6 @@ class LiveServerThread(threading.Thread):
Console.write_line(f'Started at {now.strftime("%Y-%m-%d %H:%M:%S")}\n\n')
Console.set_foreground_color(ForegroundColorEnum.default)
self._command = [sys.executable, self._main, ''.join(self._args)]
os.chdir(self._env.working_directory)
self._command = [self._executable, self._main, ''.join(self._args)]
subprocess.run(self._command, env=env_vars)