2021-03-13 11:05:17 +01:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from contextlib import suppress
|
|
|
|
|
|
|
|
import psutil as psutil
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
from watchdog.observers import Observer
|
|
|
|
|
2021-03-16 19:04:30 +01:00
|
|
|
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
2021-03-13 11:05:17 +01:00
|
|
|
from cpl.console.console import Console
|
2021-03-16 19:04:30 +01:00
|
|
|
from cpl.dependency_injection.service_abc import ServiceABC
|
|
|
|
from cpl_cli.configuration.build_settings import BuildSettings
|
2021-03-13 11:05:17 +01:00
|
|
|
from cpl_cli.live_server.live_server_thread import LiveServerThread
|
|
|
|
|
|
|
|
|
|
|
|
class LiveServerService(ServiceABC, FileSystemEventHandler):
|
|
|
|
|
|
|
|
def __init__(self, runtime: ApplicationRuntimeABC, build_settings: BuildSettings):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service for the live development server
|
|
|
|
:param runtime:
|
|
|
|
:param build_settings:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
ServiceABC.__init__(self)
|
|
|
|
FileSystemEventHandler.__init__(self)
|
|
|
|
|
|
|
|
self._runtime = runtime
|
|
|
|
self._build_settings = build_settings
|
|
|
|
|
|
|
|
self._src_dir = os.path.join(self._runtime.working_directory, self._build_settings.source_path)
|
2021-03-15 18:25:53 +01:00
|
|
|
self._ls_thread = None
|
2021-03-13 11:05:17 +01:00
|
|
|
self._observer = None
|
|
|
|
|
|
|
|
def _start_observer(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Starts the file changes observer
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
self._observer = Observer()
|
|
|
|
self._observer.schedule(self, path=self._src_dir, recursive=True)
|
|
|
|
self._observer.start()
|
|
|
|
|
|
|
|
def _restart(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Restarts the CPL project
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
for proc in psutil.process_iter():
|
|
|
|
with suppress(Exception):
|
2021-03-13 11:15:15 +01:00
|
|
|
if proc.cmdline() == self._ls_thread.command:
|
|
|
|
os.system(f'pkill -f {self._ls_thread.main}')
|
2021-03-13 11:05:17 +01:00
|
|
|
|
|
|
|
Console.write_line('Restart\n')
|
2021-03-13 11:15:15 +01:00
|
|
|
while self._ls_thread.is_alive():
|
2021-03-13 11:05:17 +01:00
|
|
|
time.sleep(1)
|
|
|
|
|
2021-03-15 18:25:53 +01:00
|
|
|
self._start()
|
2021-03-13 11:05:17 +01:00
|
|
|
|
|
|
|
def on_modified(self, event):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Triggers when source file is modified
|
|
|
|
:param event:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
if event.is_directory:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Event is modified, you can process it now
|
|
|
|
if str(event.src_path).endswith('.py'):
|
|
|
|
self._observer.stop()
|
|
|
|
self._restart()
|
|
|
|
|
2021-03-15 18:25:53 +01:00
|
|
|
def _start(self):
|
|
|
|
self._start_observer()
|
|
|
|
self._ls_thread = LiveServerThread(self._src_dir)
|
|
|
|
self._ls_thread.start()
|
|
|
|
self._ls_thread.join()
|
|
|
|
Console.close()
|
|
|
|
|
2021-03-13 11:05:17 +01:00
|
|
|
def start(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Starts the CPL live development server
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 11:05:17 +01:00
|
|
|
Console.write_line('** CPL live development server is running **')
|
2021-03-15 18:25:53 +01:00
|
|
|
self._start()
|