|
| 1 | +"""Development server module for Syrinx with file watching and live reload. |
| 2 | +
|
| 3 | +This module provides a development server that watches for file changes in the |
| 4 | +parent directory hierarchy and automatically rebuilds the site when changes are |
| 5 | +detected. It includes an HTTP server to serve the built files. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +from socketserver import TCPServer |
| 10 | +from watchdog.observers import Observer |
| 11 | +from syrinx.build import build |
| 12 | +from syrinx.read import read |
| 13 | +from syrinx.preprocess import preprocess |
| 14 | +from syrinx.server.hot_reload_handler import HotReloadHandler |
| 15 | +from syrinx.server.rebuild_handler import RebuildHandler |
| 16 | + |
| 17 | + |
| 18 | +class DevServer: |
| 19 | + """Development server with file watching and HTTP serving. |
| 20 | + |
| 21 | + Provides a complete development environment for Syrinx projects by: |
| 22 | + - Watching parent directories for file changes |
| 23 | + - Automatically rebuilding on changes |
| 24 | + - Serving the built files via HTTP |
| 25 | + |
| 26 | + Attributes: |
| 27 | + root_dir: The absolute path to the Syrinx project root. |
| 28 | + port: The port number for the HTTP server. |
| 29 | + reload_version: Version counter for triggering reloads. |
| 30 | + """ |
| 31 | + def __init__(self, root_dir, port=8000): |
| 32 | + self.root_dir = os.path.abspath(root_dir) |
| 33 | + self.port = port |
| 34 | + self.reload_version = 0 |
| 35 | + |
| 36 | + def trigger_reload(self): |
| 37 | + """Increment reload version to trigger browser reloads.""" |
| 38 | + self.reload_version += 1 |
| 39 | + |
| 40 | + def start(self): |
| 41 | + """Start the development server. |
| 42 | + |
| 43 | + Performs an initial build, sets up file watching on the parent |
| 44 | + directory, and starts an HTTP server to serve the dist directory. |
| 45 | + The server runs until interrupted with Ctrl+C. |
| 46 | + """ |
| 47 | + # Determine parent directory to watch |
| 48 | + watch_dir = os.path.dirname(self.root_dir) |
| 49 | + print(f"[DEV] Watching for changes in: {watch_dir}") |
| 50 | + print(f"[DEV] Building from: {self.root_dir}") |
| 51 | + |
| 52 | + # Initial build |
| 53 | + print("[DEV] Initial build...") |
| 54 | + preprocess(self.root_dir, clean=False) |
| 55 | + root = read(self.root_dir) |
| 56 | + build(root, self.root_dir) |
| 57 | + print("[DEV] Initial build complete!") |
| 58 | + |
| 59 | + # Setup file watcher with reload callback |
| 60 | + event_handler = RebuildHandler(self.root_dir, watch_dir, self.trigger_reload) |
| 61 | + observer = Observer() |
| 62 | + observer.schedule(event_handler, watch_dir, recursive=True) |
| 63 | + observer.start() |
| 64 | + |
| 65 | + # Setup HTTP server |
| 66 | + dist_dir = os.path.join(self.root_dir, 'dist') |
| 67 | + |
| 68 | + # Store original directory to restore later |
| 69 | + original_dir = os.getcwd() |
| 70 | + |
| 71 | + # Configure the handler class attributes |
| 72 | + HotReloadHandler.dev_server = self |
| 73 | + HotReloadHandler.dist_dir = dist_dir |
| 74 | + |
| 75 | + try: |
| 76 | + with TCPServer(("", self.port), HotReloadHandler) as httpd: |
| 77 | + print(f"\n[DEV] Server running at http://localhost:{self.port}") |
| 78 | + print("[DEV] Press Ctrl+C to stop\n") |
| 79 | + httpd.serve_forever() |
| 80 | + except KeyboardInterrupt: |
| 81 | + print("\n[DEV] Shutting down...") |
| 82 | + observer.stop() |
| 83 | + finally: |
| 84 | + observer.join() |
| 85 | + os.chdir(original_dir) |
0 commit comments