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

@@ -9,20 +9,24 @@ from watchdog.observers import Observer
from cpl.console.console import Console
from cpl.environment.application_environment_abc import ApplicationEnvironmentABC
from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.live_server.live_server_thread import LiveServerThread
class LiveServerService(FileSystemEventHandler):
def __init__(self, env: ApplicationEnvironmentABC, build_settings: BuildSettings):
def __init__(self, env: ApplicationEnvironmentABC, project_settings: ProjectSettings,
build_settings: BuildSettings):
"""
Service for the live development server
:param env:
:param project_settings:
:param build_settings:
"""
FileSystemEventHandler.__init__(self)
self._env = env
self._project_settings = project_settings
self._build_settings = build_settings
self._src_dir = os.path.join(self._env.working_directory, self._build_settings.source_path)
@@ -72,7 +76,13 @@ class LiveServerService(FileSystemEventHandler):
def _start(self):
self._start_observer()
self._ls_thread = LiveServerThread(self._src_dir, self._build_settings, self._args)
self._ls_thread = LiveServerThread(
self._project_settings.python_executable,
self._src_dir,
self._args,
self._env,
self._build_settings
)
self._ls_thread.start()
self._ls_thread.join()
Console.close()

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)