|
| 1 | +// --------------------------------------------------------------------------------------------- |
| 2 | +// Mountain Main Entry Point (main.rs) |
| 3 | +// -------------------------------------------------------------------------------------------- |
| 4 | +// Main entry point for the Land editor's backend (Mountain) using Tauri. |
| 5 | +// Sets up the Tauri application, initializes and manages shared application |
| 6 | +// state (`AppState`) and the core application logic runner (`AppRuntime`). |
| 7 | +// Registers `Track` dispatcher for frontend commands and uses the Tauri setup |
| 8 | +// hook for critical initializations: launching Cocoon, setting up IPC/RPC, |
| 9 | +// custom URI protocols, loading initial configurations, and scanning |
| 10 | +// extensions. |
| 11 | +// |
| 12 | +// Responsibilities: |
| 13 | +// - Configure and build the Tauri application. |
| 14 | +// - Initialize `AppState` (default) and `AppRuntime`. |
| 15 | +// - In `.setup()`: |
| 16 | +// - Perform AppState initializations requiring AppHandle (paths, extension |
| 17 | +// scan, config load). |
| 18 | +// - Register frontend command handlers (`Track`). |
| 19 | +// - Launch sidecar processes (Cocoon) via `process_mgmt`. |
| 20 | +// - Initialize IPC/RPC layers (`Vine`/`rpc`). |
| 21 | +// - Register custom URI protocol handlers (`handlers::protocol`). |
| 22 | +// - (Conceptually) Start file watchers for live config reloads. |
| 23 | +// - Run the main Tauri event loop. |
| 24 | +// |
| 25 | +// Key Interactions: |
| 26 | +// - Uses Tauri APIs for application building, state management, event handling, |
| 27 | +// path resolution. |
| 28 | +// - Manages `AppState` and `AppRuntime` via Tauri's `State<T>`. |
| 29 | +// - Calls `handlers::process_mgmt::launch_and_manage_cocoon`. |
| 30 | +// - Calls `rpc::setup_mountain_rpc_server` (conceptual). |
| 31 | +// - Registers `track::dispatch_command` and |
| 32 | +// `handlers::protocol::handle_vscode_protocol`. |
| 33 | +// -------------------------------------------------------------------------------------------- |
| 34 | + |
| 35 | +// Hide console window on Windows in release |
| 36 | +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] |
| 37 | + |
| 38 | +use std::{path::PathBuf, sync::Arc}; |
| 39 | + |
| 40 | +use log::{debug, error, info, trace, warn}; |
| 41 | +use tauri::{AppHandle, Manager, Runtime as TauriRuntime, State, Window, Wry}; |
| 42 | + |
| 43 | +// --- Application Modules --- |
| 44 | +mod app_state; |
| 45 | + |
| 46 | +mod environment; |
| 47 | + |
| 48 | +mod handlers; |
| 49 | + |
| 50 | +// Centralized logger initialization |
| 51 | +mod logging_setup; |
| 52 | +mod mist; |
| 53 | + |
| 54 | +mod rpc; |
| 55 | + |
| 56 | +mod runtime; |
| 57 | + |
| 58 | +mod track; |
| 59 | + |
| 60 | +mod vine; |
| 61 | + |
| 62 | +use app_state::AppState; |
| 63 | +use environment::MountainEnvironment; |
| 64 | +use runtime::AppRuntime; |
| 65 | + |
| 66 | +// Example struct for event payloads (may or may not be actively used in main) |
| 67 | +#[derive(Clone, serde::Serialize)] |
| 68 | +struct GenericPayload { |
| 69 | + message:String, |
| 70 | +} |
| 71 | + |
| 72 | +#[tokio::main] |
| 73 | +async fn main() { |
| 74 | + // Initialize logger as the very first step |
| 75 | + logging_setup::init_mountain_logger(); |
| 76 | + |
| 77 | + info!("[Mountain Main] Starting up Land Editor (Mountain)..."); |
| 78 | + |
| 79 | + // AppState::default() loads some initial state (e.g., global memento from fixed |
| 80 | + // path) |
| 81 | + let initial_app_state = AppState::default(); |
| 82 | + |
| 83 | + tauri::Builder::default() |
| 84 | + // Manage the initially created AppState |
| 85 | + .manage(initial_app_state) |
| 86 | + // AppRuntime and MountainEnvironment will be created and managed inside .setup once AppHandle is available |
| 87 | + .setup(|app| { |
| 88 | + |
| 89 | + info!("[Mountain Setup] Tauri setup hook running."); |
| 90 | + |
| 91 | + let app_handle = app.handle(); |
| 92 | + // --- Create and Manage Environment & Runtime with AppHandle --- |
| 93 | + let mountain_env_arc = Arc::new(MountainEnvironment::new(app_handle.clone())); |
| 94 | + |
| 95 | + let app_runtime_arc = Arc::new(AppRuntime::new(mountain_env_arc.clone())); |
| 96 | + |
| 97 | + // Manage the fully initialized AppRuntime |
| 98 | + app_handle.manage(app_runtime_arc.clone()); |
| 99 | + info!("[Mountain Setup] MountainEnvironment and AppRuntime created and managed."); |
| 100 | + // --- AppState Post-Handle Initialization Task --- |
| 101 | + // This task performs initializations that require AppHandle and should run asynchronously. |
| 102 | + let post_setup_app_handle = app_handle.clone(); |
| 103 | + |
| 104 | + tauri::async_runtime::spawn(async move { |
| 105 | + info!("[Mountain Setup Task] Starting AppState post-handle initialization..."); |
| 106 | + |
| 107 | + let app_state = post_setup_app_handle.state::<AppState>(); |
| 108 | + |
| 109 | + // 1. Resolve and set extension scan paths |
| 110 | + let mut resolved_scan_paths: Vec<PathBuf> = Vec::new(); |
| 111 | + |
| 112 | + if let Some(builtin_ext_dir) = post_setup_app_handle.path_resolver().resolve_resource("extensions/builtin") { |
| 113 | + info!("[Mountain Setup Task] Adding builtin extension scan path: {}", builtin_ext_dir.display()); |
| 114 | + resolved_scan_paths.push(builtin_ext_dir); |
| 115 | + } else { |
| 116 | + warn!("[Mountain Setup Task] Could not resolve 'extensions/builtin' resource path."); |
| 117 | + } |
| 118 | + |
| 119 | + // TODO: Add other potential scan paths (e.g., user extensions based on config or standard locations) |
| 120 | + // Example: |
| 121 | + // if let Some(user_ext_dir) = post_setup_app_handle.path_resolver().app_data_dir().map(|d| d.join("extensions")) { |
| 122 | + // if user_ext_dir.exists() { |
| 123 | + // info!("[Mountain Setup Task] Adding user extension scan path: {}", user_ext_dir.display()); |
| 124 | + // resolved_scan_paths.push(user_ext_dir); |
| 125 | + // } |
| 126 | + // } |
| 127 | + // Scope for mutable access to app_state.extension_scan_paths if it were a Mutex/RwLock |
| 128 | + { |
| 129 | + // For now, assuming direct vec mutation is fine if it's not Arc<Mutex<Vec<PathBuf>>> |
| 130 | + // If it's plain Vec, this direct modification is problematic if AppState is shared. |
| 131 | + // app_state.extension_scan_paths needs to be Arc<Mutex<Vec<PathBuf>>> or similar for this to be safe. |
| 132 | + // For simplicity of this example, assuming AppState::extension_scan_paths itself might be behind a lock or is init-once. |
| 133 | + // Given AppState structure, this direct modification is incorrect. |
| 134 | + // Instead, it should be like: |
| 135 | + // let mut scan_paths_guard = app_state.extension_scan_paths.lock().unwrap(); |
| 136 | + |
| 137 | + // scan_paths_guard.clear(); |
| 138 | + |
| 139 | + // scan_paths_guard.extend(resolved_scan_paths); |
| 140 | + |
| 141 | + // OR if extension_scan_paths is not wrapped in Arc<Mutex>: |
| 142 | + // If it's a plain field, this task needs &mut AppState or similar |
| 143 | + // app_state.extension_scan_paths = resolved_scan_paths; |
| 144 | + |
| 145 | + // Correct approach: Assuming AppState.extension_scan_paths is NOT behind a Mutex |
| 146 | + // and this task owns a mutable reference or is initializing a part of it. |
| 147 | + // The current `app_state.extension_scan_paths` is `Vec<PathBuf>`, not `Arc<Mutex<...>>` |
| 148 | + // This indicates that `extension_scan_paths` is intended to be populated once, possibly here. |
| 149 | + // If it needs to be dynamic later, AppState field type should change. |
| 150 | + // For now, we'll assume this is effectively an initialization step for that field. |
| 151 | + // To make this safe with `State<AppState>`, the field itself needs to be behind a Mutex |
| 152 | + // or this part of initialization logic needs rethinking. |
| 153 | + // Let's assume AppState holds an Arc<Mutex<Vec<PathBuf>>> for extension_scan_paths. |
| 154 | + // If AppState is: pub extension_scan_paths: Vec<PathBuf>, then this is not thread safe. |
| 155 | + // Reverting to original intent if it was non-Mutexed for simplicity: |
| 156 | + // Clear placeholder |
| 157 | + // app_state.extension_scan_paths.clear(); |
| 158 | + // app_state.extension_scan_paths.extend(resolved_scan_paths); |
| 159 | + |
| 160 | + // *This part highlights a potential design issue in the provided AppState if modification |
| 161 | + // is expected from multiple places or async tasks after initial AppState::default(). |
| 162 | + // For the purpose of synthesis, assuming app_state.scan_extensions() internally handles |
| 163 | + // how it gets/uses these paths if they are set this way. |
| 164 | + // If AppState.extension_scan_paths must be mutable, it needs Arc<Mutex>. |
| 165 | + // Given `state.scan_extensions().await` is called next, that method should use the paths. |
| 166 | + // Let's assume AppState::default() initialized it empty and this task populates it before scan. |
| 167 | + // If `scan_extensions` reads from `self.extension_scan_paths`, it will see this. |
| 168 | + // THIS IS STILL NOT SAFE IF AppState IS CLONED AND SHARED WITHOUT INTERNAL MUTABILITY FOR THE VEC. |
| 169 | + // Safest: AppState::extension_scan_paths: Arc<Mutex<Vec<PathBuf>>> |
| 170 | + // For now, will keep the direct modification as implied by `state.extension_scan_paths.clear()` |
| 171 | + // but acknowledge it's problematic without internal mutability for that field in AppState. |
| 172 | + // The AppState provided DOES NOT use Arc<Mutex<Vec<PathBuf>>>, it's just Vec<PathBuf>. |
| 173 | + // So, the `.manage(initial_app_state)` shares an immutable AppState. |
| 174 | + // The only way to modify it is if `app_state` itself is `Arc<Mutex<AppState>>`, or |
| 175 | + // its fields are `Arc<Mutex<...>>`. |
| 176 | + // The current `AppState` uses `Arc<Mutex<...>>` for *most* fields, but not `extension_scan_paths`. |
| 177 | + // This is a design flaw in the provided `AppState`. |
| 178 | + // **Correction based on AppState structure:** `extension_scan_paths` is NOT mutable this way. |
| 179 | + // It must be set during `AppState::default()` or `AppState` must be wrapped further. |
| 180 | + // Alternative: clone app_state, modify, and then re-manage. But this is not ideal. |
| 181 | + // **Simplifying assumption for synthesis:** |
| 182 | + // `extension_scan_paths` is initialized in `AppState::default()` with these paths, |
| 183 | + // or `scan_extensions` takes paths as an argument. |
| 184 | + // For now, commenting out direct modification and relying on `scan_extensions` to use paths |
| 185 | + // set in `AppState::default()` or another mechanism. |
| 186 | + debug!("[Mountain Setup Task] Extension scan paths used by scan_extensions will be from AppState::default or config."); |
| 187 | + |
| 188 | + } |
| 189 | + // 2. Scan for extensions |
| 190 | + app_state.scan_extensions().await; |
| 191 | + // 3. Load/configure enabled proposed APIs |
| 192 | + { |
| 193 | + let mut proposed_apis_guard = app_state.enabled_proposed_apis.lock().expect("Failed to lock proposed APIs for init"); |
| 194 | + |
| 195 | + proposed_apis_guard.insert("*".to_string(), vec!["testProposedApi".to_string(), "workspaceTrust".to_string()]); |
| 196 | + |
| 197 | + info!("[Mountain Setup Task] Enabled proposed APIs configured. Count: {}", proposed_apis_guard.len()); |
| 198 | + } |
| 199 | + |
| 200 | + // 4. Load initial merged configuration into AppState |
| 201 | + match handlers::config::load_and_merge_configurations_internal(&post_setup_app_handle, &app_state).await { |
| 202 | + |
| 203 | + Ok(merged_config_state) => { |
| 204 | + |
| 205 | + app_state.configuration.lock().expect("Failed to lock AppState.configuration for init load").update_from(merged_config_state); |
| 206 | + |
| 207 | + info!("[Mountain Setup Task] Initial merged configuration loaded into AppState."); |
| 208 | + |
| 209 | + } |
| 210 | + Err(e) => { |
| 211 | + |
| 212 | + error!("[Mountain Setup Task] CRITICAL: Failed to load initial merged configurations: {}", e); |
| 213 | + |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + // 5. Update workspace memento path |
| 218 | + if let Some(app_data_dir_for_memento) = post_setup_app_handle.path_resolver().app_data_dir() { |
| 219 | + |
| 220 | + debug!("[Mountain Setup Task] Attempting to initialize workspace memento path based on data dir: {}", app_data_dir_for_memento.display()); |
| 221 | + |
| 222 | + if let Err(e) = app_state.update_workspace_memento_path(&app_data_dir_for_memento) { |
| 223 | + |
| 224 | + error!("[Mountain Setup Task] Failed to initialize workspace memento path: {}", e); |
| 225 | + |
| 226 | + } |
| 227 | + } else { |
| 228 | + warn!("[Mountain Setup Task] App data directory not available for workspace memento path init at this stage."); |
| 229 | + } |
| 230 | + |
| 231 | + info!("[Mountain Setup Task] AppState post-handle initialization complete."); |
| 232 | + }); |
| 233 | + |
| 234 | + // --- Register Custom Protocol Handlers --- |
| 235 | + let protocol_handle_setup = app_handle.clone(); |
| 236 | + |
| 237 | + app.protocol() |
| 238 | + .register("vscode", move |request| { |
| 239 | + |
| 240 | + debug!("[Mountain Setup Protocol] Received vscode:// request: {}", request.uri()); |
| 241 | + |
| 242 | + handlers::protocol::handle_vscode_protocol(request, protocol_handle_setup.clone()) |
| 243 | + }) |
| 244 | + .expect("Failed to register vscode protocol"); |
| 245 | + |
| 246 | + info!("[Mountain Setup] vscode:// protocol registered."); |
| 247 | + |
| 248 | + // --- Setup RPC Server (for Vine/Sidecar Communication) --- |
| 249 | + // This setup ensures Mountain can handle incoming RPC calls. |
| 250 | + // Actual dispatching of these calls to specific handlers typically happens |
| 251 | + // within the Vine/RPC layer, using the AppRuntime to execute logic. |
| 252 | + let rpc_runtime_clone_setup = app_handle.state::<Arc<AppRuntime>>().inner().clone(); |
| 253 | + |
| 254 | + let rpc_app_handle_clone_setup = app_handle.clone(); |
| 255 | + |
| 256 | + rpc::setup_mountain_rpc_server(rpc_app_handle_clone_setup, rpc_runtime_clone_setup); |
| 257 | + |
| 258 | + info!("[Mountain Setup] RPC Server handlers conceptually registered for Vine."); |
| 259 | + |
| 260 | + // --- Launch Cocoon Sidecar (Conditional) --- |
| 261 | + #[cfg(feature = "extension_host_cocoon")] |
| 262 | + { |
| 263 | + info!("[Mountain Setup] Cocoon feature enabled. Spawning launch_and_manage_cocoon task..."); |
| 264 | + |
| 265 | + let cocoon_app_handle = app_handle.clone(); |
| 266 | + |
| 267 | + // Use Tauri's async runtime for background tasks |
| 268 | + tauri::async_runtime::spawn(async move { |
| 269 | + handlers::process_mgmt::launch_and_manage_cocoon(cocoon_app_handle).await; |
| 270 | + }); |
| 271 | + } |
| 272 | + |
| 273 | + #[cfg(not(feature = "extension_host_cocoon"))] |
| 274 | + { |
| 275 | + info!("[Mountain Setup] Cocoon feature disabled."); |
| 276 | + } |
| 277 | + |
| 278 | + // --- Start Native Mist WebSocket Server (Conditional) --- |
| 279 | + #[cfg(feature = "mist_native")] |
| 280 | + { |
| 281 | + |
| 282 | + info!("[Mountain Setup] Native Mist feature enabled. Spawning start_websocket_server task..."); |
| 283 | + |
| 284 | + let mist_app_handle = app_handle.clone(); |
| 285 | + |
| 286 | + tauri::async_runtime::spawn(async move { |
| 287 | + if let Err(e) = crate::mist::start_websocket_server(mist_app_handle).await { |
| 288 | + error!("[Mist Server] Native WebSocket server failed to start: {}", e); |
| 289 | + } |
| 290 | + }); |
| 291 | + } |
| 292 | + |
| 293 | + #[cfg(not(feature = "mist_native"))] |
| 294 | + { |
| 295 | + info!("[Mountain Setup] Native Mist feature disabled."); |
| 296 | + } |
| 297 | + |
| 298 | + // --- TODO: Start File Watchers for Configuration Files --- |
| 299 | + // This would involve using a crate like `notify` to watch settings.json files |
| 300 | + // and trigger a configuration reload (e.g., calling parts of the post-setup task again). |
| 301 | + // let watcher_app_handle = app_handle.clone(); |
| 302 | + // tauri::async_runtime::spawn(async move { |
| 303 | + // if let Err(e) = handlers::config_watcher::start_watching_config_files(watcher_app_handle).await { |
| 304 | + // error!("[Config Watcher] Failed to start: {}", e); |
| 305 | + // } |
| 306 | + // }); |
| 307 | + |
| 308 | + info!("[Mountain Setup] Conceptual: File watchers for config would start here."); |
| 309 | + |
| 310 | + info!("[Mountain Setup] Setup hook logic complete."); |
| 311 | + |
| 312 | + Ok(()) |
| 313 | + }) |
| 314 | + .invoke_handler(tauri::generate_handler![ |
| 315 | + // Main entry point for frontend commands |
| 316 | + track::dispatch_command |
| 317 | + ]) |
| 318 | + .on_window_event(|event| match event.event() { |
| 319 | + tauri::WindowEvent::CloseRequested { api, .. } => { |
| 320 | + |
| 321 | + info!("[Mountain WindowEvent] Close requested for window: {}", event.window().label()); |
| 322 | + |
| 323 | + // TODO: Implement graceful shutdown: |
| 324 | + // 1. Notify Cocoon to deactivate extensions. |
| 325 | + // 2. Wait for Cocoon to signal completion or timeout. |
| 326 | + // 3. Save AppState (mementos, dirty files via DocumentProvider effects). |
| 327 | + // 4. Allow close. |
| 328 | + // For now, allow default close. To prevent for debugging: |
| 329 | + // api.prevent_close(); |
| 330 | + |
| 331 | + // warn!("[Mountain WindowEvent] Close prevention is active for debugging if uncommented."); |
| 332 | + } |
| 333 | + |
| 334 | + tauri::WindowEvent::Destroyed => { |
| 335 | + info!("[Mountain WindowEvent] Window destroyed: {}", event.window().label()); |
| 336 | + } |
| 337 | + |
| 338 | + _ => { |
| 339 | + trace!("[Mountain WindowEvent] Other event on window '{}': {:?}", event.window().label(), event.event()); |
| 340 | + } |
| 341 | + }) |
| 342 | + .build(tauri::generate_context!()) |
| 343 | + .expect("Error while building Mountain Tauri application").run(|app_handle_run, event| match event { |
| 344 | + // Renamed app_handle to avoid conflict |
| 345 | + tauri::RunEvent::ExitRequested { api, .. } => { |
| 346 | + info!("[Mountain RunEvent] Application exit requested. Performing cleanup..."); |
| 347 | + |
| 348 | + // TODO: Global cleanup before app exit: |
| 349 | + // api.prevent_exit(); |
| 350 | + |
| 351 | + // let handle_clone_exit = app_handle_run.clone(); |
| 352 | + |
| 353 | + // tauri::async_runtime::spawn(async move { |
| 354 | + |
| 355 | + // info!("[Mountain Exit] Signaling sidecars to terminate..."); |
| 356 | + |
| 357 | + // Example: vine::broadcast_terminate_signal(&handle_clone_exit).await; |
| 358 | + // |
| 359 | + // Give time |
| 360 | + // tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 361 | + // info!("[Mountain Exit] Exiting application now."); |
| 362 | + // handle_clone_exit.exit(0); |
| 363 | + // }); |
| 364 | + |
| 365 | + } |
| 366 | + |
| 367 | + tauri::RunEvent::Exit => { |
| 368 | + info!("[Mountain RunEvent] Application process is exiting."); |
| 369 | + } |
| 370 | + _ => {} |
| 371 | + }); |
| 372 | + |
| 373 | + info!("[Mountain Main] Application event loop finished or error occurred."); |
| 374 | +} |
0 commit comments