cpl start & run - Bessere Einbindung mit den Build-Tools #124 #131

Merged
edraft merged 3 commits from #124 into 2022.12 2022-12-03 23:25:14 +01:00
4 changed files with 68 additions and 12 deletions
Showing only changes of commit 856960d799 - Show all commits

View File

@ -12,6 +12,7 @@ from cpl_core.environment.application_environment_abc import ApplicationEnvironm
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.live_server.live_server_thread import LiveServerThread from cpl_cli.live_server.live_server_thread import LiveServerThread
from cpl_core.utils import String
class LiveServerService(FileSystemEventHandler): class LiveServerService(FileSystemEventHandler):
@ -37,6 +38,7 @@ class LiveServerService(FileSystemEventHandler):
self._publisher = publisher self._publisher = publisher
self._src_dir = os.path.join(self._env.working_directory, self._build_settings.source_path) self._src_dir = os.path.join(self._env.working_directory, self._build_settings.source_path)
self._wd = self._src_dir
self._ls_thread = None self._ls_thread = None
self._observer = None self._observer = None
@ -83,10 +85,11 @@ class LiveServerService(FileSystemEventHandler):
self._restart() self._restart()
def _start(self): def _start(self):
self._build()
self._start_observer() self._start_observer()
self._ls_thread = LiveServerThread( self._ls_thread = LiveServerThread(
self._project_settings.python_executable, self._project_settings.python_executable,
self._src_dir, self._wd,
self._args, self._args,
self._env, self._env,
self._build_settings self._build_settings
@ -98,7 +101,18 @@ class LiveServerService(FileSystemEventHandler):
def _build(self): def _build(self):
if self._is_dev: if self._is_dev:
return return
self._env.set_working_directory(self._src_dir)
Console.disable()
self._publisher.build() self._publisher.build()
Console.enable()
self._wd = os.path.abspath(os.path.join(
self._src_dir,
self._build_settings.output_path,
self._project_settings.name,
'build',
String.convert_to_snake_case(self._project_settings.name)
))
def start(self, args: list[str]): def start(self, args: list[str]):
""" """
@ -114,8 +128,6 @@ class LiveServerService(FileSystemEventHandler):
self._is_dev = True self._is_dev = True
args.remove('dev') args.remove('dev')
self._build()
self._args = args self._args = args
Console.write_line('** CPL live development server is running **') Console.write_line('** CPL live development server is running **')
self._start() self._start()

View File

@ -21,6 +21,7 @@ class StartTestCase(CommandTestCase):
self._application = f'src/{String.convert_to_snake_case(self._source)}/application.py' self._application = f'src/{String.convert_to_snake_case(self._source)}/application.py'
self._test_code = f""" self._test_code = f"""
import json import json
import os
settings = dict() settings = dict()
with open('appsettings.json', 'r', encoding='utf-8') as cfg: with open('appsettings.json', 'r', encoding='utf-8') as cfg:
# load json # load json
@ -31,14 +32,19 @@ class StartTestCase(CommandTestCase):
settings['RunTest']['WasRestarted'] = 'True' settings['RunTest']['WasRestarted'] = 'True'
settings['RunTest']['WasStarted'] = 'True' settings['RunTest']['WasStarted'] = 'True'
settings['RunTest']['Path'] = os.path.dirname(os.path.realpath(__file__))
with open('appsettings.json', 'w', encoding='utf-8') as project_file: with open('appsettings.json', 'w', encoding='utf-8') as project_file:
project_file.write(json.dumps(settings, indent=2)) project_file.write(json.dumps(settings, indent=2))
project_file.close() project_file.close()
""" """
def _get_appsettings(self): def _get_appsettings(self, is_dev=False):
with open(os.path.join(os.getcwd(), self._appsettings), 'r', encoding='utf-8') as cfg: appsettings = f'dist/{self._source}/build/{String.convert_to_snake_case(self._source)}/appsettings.json'
if is_dev:
appsettings = f'src/{String.convert_to_snake_case(self._source)}/appsettings.json'
with open(os.path.join(os.getcwd(), appsettings), 'r', encoding='utf-8') as cfg:
# load json # load json
project_json = json.load(cfg) project_json = json.load(cfg)
cfg.close() cfg.close()
@ -46,7 +52,7 @@ class StartTestCase(CommandTestCase):
return project_json return project_json
def _save_appsettings(self, settings: dict): def _save_appsettings(self, settings: dict):
with open(os.path.join(os.getcwd(), self._appsettings), 'w', encoding='utf-8') as project_file: with open(os.path.join(os.getcwd(), f'src/{String.convert_to_snake_case(self._source)}/appsettings.json'), 'w', encoding='utf-8') as project_file:
project_file.write(json.dumps(settings, indent=2)) project_file.write(json.dumps(settings, indent=2))
project_file.close() project_file.close()
@ -67,6 +73,39 @@ class StartTestCase(CommandTestCase):
def test_start(self): def test_start(self):
thread = StartTestThread() thread = StartTestThread()
thread.start() thread.start()
time.sleep(5)
settings = self._get_appsettings()
self.assertNotEqual(settings, {})
self.assertIn('RunTest', settings)
self.assertIn('WasStarted', settings['RunTest'])
self.assertEqual(
'True',
settings['RunTest']['WasStarted']
)
with open(os.path.join(os.getcwd(), self._application), 'a', encoding='utf-8') as file:
file.write(f'# trigger restart (comment generated by unittest)')
file.close()
time.sleep(5)
settings = self._get_appsettings()
self.assertNotEqual(settings, {})
self.assertIn('RunTest', settings)
self.assertIn('WasStarted', settings['RunTest'])
self.assertIn('WasRestarted', settings['RunTest'])
self.assertEqual(
'True',
settings['RunTest']['WasStarted']
)
self.assertEqual(
'True',
settings['RunTest']['WasRestarted']
)
def test_start_dev(self):
thread = StartTestThread(is_dev=True)
thread.start()
time.sleep(1) time.sleep(1)
settings = self._get_appsettings() settings = self._get_appsettings()
self.assertNotEqual(settings, {}) self.assertNotEqual(settings, {})
@ -83,7 +122,7 @@ class StartTestCase(CommandTestCase):
time.sleep(1) time.sleep(1)
settings = self._get_appsettings() settings = self._get_appsettings(is_dev=True)
self.assertNotEqual(settings, {}) self.assertNotEqual(settings, {})
self.assertIn('RunTest', settings) self.assertIn('RunTest', settings)
self.assertIn('WasStarted', settings['RunTest']) self.assertIn('WasStarted', settings['RunTest'])

View File

@ -5,8 +5,9 @@ from unittests_shared.cli_commands import CLICommands
class StartTestThread(threading.Thread): class StartTestThread(threading.Thread):
def __init__(self): def __init__(self, is_dev=False):
threading.Thread.__init__(self, daemon=True) threading.Thread.__init__(self, daemon=True)
self._is_dev = is_dev
def run(self): def run(self):
CLICommands.start(True) CLICommands.start(is_dev=self._is_dev, output=True)

View File

@ -76,8 +76,12 @@ class CLICommands:
cls._run('run', project, *args, output=output) cls._run('run', project, *args, output=output)
@classmethod @classmethod
def start(cls, output=False): def start(cls, is_dev=False, output=False):
cls._run('start', output=output) args = []
if is_dev:
args.append('--dev')
cls._run('start', *args, output=output)
@classmethod @classmethod
def uninstall(cls, package: str, is_dev=False, output=False): def uninstall(cls, package: str, is_dev=False, output=False):