2021.4 #19
@ -1,68 +1,13 @@
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
import psutil as psutil
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
from cpl.application import ApplicationRuntimeABC
|
||||
from cpl.console.console import Console
|
||||
from cpl_cli.command_abc import CommandABC
|
||||
from cpl_cli.configuration import BuildSettings
|
||||
from cpl_cli.live_server.live_server import LiveServerThread
|
||||
from cpl_cli.live_server.live_server_service import LiveServerService
|
||||
|
||||
|
||||
class StartService(CommandABC, FileSystemEventHandler):
|
||||
class StartService(CommandABC):
|
||||
|
||||
def __init__(self, runtime: ApplicationRuntimeABC, build_settings: BuildSettings):
|
||||
def __init__(self, live_server: LiveServerService):
|
||||
CommandABC.__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)
|
||||
self._live_server = LiveServerThread(self._src_dir)
|
||||
self._observer = None
|
||||
|
||||
def _start_observer(self):
|
||||
self._observer = Observer()
|
||||
self._observer.schedule(self, path=self._src_dir, recursive=True)
|
||||
self._observer.start()
|
||||
|
||||
def _restart(self):
|
||||
for proc in psutil.process_iter():
|
||||
try:
|
||||
if proc.cmdline() == self._live_server.command:
|
||||
os.system(f'pkill -f {self._live_server.main}')
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
Console.write_line('Restart\n')
|
||||
|
||||
while self._live_server.is_alive():
|
||||
time.sleep(1)
|
||||
|
||||
self._live_server = LiveServerThread(self._src_dir)
|
||||
self._live_server.start()
|
||||
|
||||
self._start_observer()
|
||||
|
||||
def on_modified(self, event):
|
||||
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()
|
||||
self._live_server = live_server
|
||||
|
||||
def run(self, args: list[str]):
|
||||
Console.write_line('** CPL live development server is running **')
|
||||
self._start_observer()
|
||||
self._live_server.start()
|
||||
|
||||
Console.close()
|
||||
Console.write('\n')
|
||||
|
@ -1,23 +0,0 @@
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from cpl.console.console import Console
|
||||
from cpl_cli.live_server.live_server import LiveServerThread
|
||||
|
||||
|
||||
class FileChangeHandler(FileSystemEventHandler):
|
||||
|
||||
def __init__(self, live_server: LiveServerThread):
|
||||
FileSystemEventHandler.__init__(self)
|
||||
|
||||
self._live_server = live_server
|
||||
|
||||
def on_any_event(self, event):
|
||||
if event.is_directory:
|
||||
return None
|
||||
|
||||
elif event.event_type == 'modified':
|
||||
# Event is modified, you can process it now
|
||||
if str(event.src_path).endswith('.py'):
|
||||
Console.write_line(f'Detected change in {event.src_path}')
|
||||
self._live_server.kill_application()
|
||||
self._live_server.start()
|
64
src/cpl_cli/live_server/live_server_service.py
Normal file
64
src/cpl_cli/live_server/live_server_service.py
Normal file
@ -0,0 +1,64 @@
|
||||
import os
|
||||
import time
|
||||
from contextlib import suppress
|
||||
|
||||
import psutil as psutil
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
from cpl.application import ApplicationRuntimeABC
|
||||
from cpl.console.console import Console
|
||||
from cpl.dependency_injection import ServiceABC
|
||||
from cpl_cli.configuration import BuildSettings
|
||||
from cpl_cli.live_server.live_server_thread import LiveServerThread
|
||||
|
||||
|
||||
class LiveServerService(ServiceABC, FileSystemEventHandler):
|
||||
|
||||
def __init__(self, runtime: ApplicationRuntimeABC, build_settings: BuildSettings):
|
||||
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)
|
||||
self._live_server = LiveServerThread(self._src_dir)
|
||||
self._observer = None
|
||||
|
||||
def _start_observer(self):
|
||||
self._observer = Observer()
|
||||
self._observer.schedule(self, path=self._src_dir, recursive=True)
|
||||
self._observer.start()
|
||||
|
||||
def _restart(self):
|
||||
for proc in psutil.process_iter():
|
||||
with suppress(Exception):
|
||||
if proc.cmdline() == self._live_server.command:
|
||||
os.system(f'pkill -f {self._live_server.main}')
|
||||
|
||||
Console.write_line('Restart\n')
|
||||
while self._live_server.is_alive():
|
||||
time.sleep(1)
|
||||
|
||||
self._live_server = LiveServerThread(self._src_dir)
|
||||
self._live_server.start()
|
||||
|
||||
self._start_observer()
|
||||
|
||||
def on_modified(self, event):
|
||||
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()
|
||||
|
||||
def start(self):
|
||||
Console.write_line('** CPL live development server is running **')
|
||||
self._start_observer()
|
||||
self._live_server.start()
|
||||
|
||||
Console.close()
|
||||
Console.write('\n')
|
@ -27,7 +27,7 @@ class LiveServerThread(threading.Thread):
|
||||
def run(self):
|
||||
self._main = os.path.join(self._path, 'main.py')
|
||||
if not os.path.isfile(self._main):
|
||||
Console.error('Entry point main.py does not exist')
|
||||
Console.error('Entry point main.py not found')
|
||||
return
|
||||
|
||||
Console.write_line('Read successfully')
|
@ -15,6 +15,7 @@ from cpl_cli.command_handler_service import CommandHandler
|
||||
from cpl_cli.command.help_service import HelpService
|
||||
from cpl_cli.command.version_service import VersionService
|
||||
from cpl_cli.error import Error
|
||||
from cpl_cli.live_server.live_server_service import LiveServerService
|
||||
from cpl_cli.publish.publisher_service import PublisherService
|
||||
from cpl_cli.publish.publisher_abc import PublisherABC
|
||||
|
||||
@ -67,6 +68,7 @@ class Startup(StartupABC):
|
||||
self._services.add_singleton(CommandHandler)
|
||||
|
||||
self._services.add_transient(PublisherABC, PublisherService)
|
||||
self._services.add_transient(LiveServerService)
|
||||
|
||||
self._services.add_transient(BuildService)
|
||||
self._services.add_transient(GenerateService)
|
||||
|
Loading…
Reference in New Issue
Block a user