iris-hub 1.0.0

Iris - Mensageira dos Devs. Hub para conectar e executar aplicações com comandos personalizados.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! # Process Manager Service
//! 
//! Este serviço é responsável pelo gerenciamento do ciclo de vida
//! dos processos das aplicações configuradas.
//! 
//! ## Funcionalidades
//! - Iniciar processos em terminais Windows
//! - Parar processos em execução
//! - Reiniciar processos
//! - Monitorar estado dos processos
//! - Limpeza automática de processos mortos

use std::collections::{HashMap, HashSet};
use std::fs;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};
use std::time::Duration;

#[cfg(windows)]
use std::os::windows::process::CommandExt;

use crate::core::{AppConfig, RunningProcess};

/// Flags de criação do Windows para ocultar janelas de comando
#[cfg(windows)]
const CREATE_NO_WINDOW: u32 = 0x08000000;

/// Gerenciador de Processos.
/// 
/// Controla o ciclo de vida de todos os processos das aplicações.
/// Thread-safe através de Arc<Mutex>.
/// 
/// # Exemplo
/// ```rust
/// let manager = ProcessManager::new();
/// manager.launch_app(&app_config);
/// 
/// if manager.is_running(&app_config.id) {
///     manager.stop_app(&app_config.id);
/// }
/// ```
pub struct ProcessManager {
    /// Mapa de processos em execução (app_id -> RunningProcess)
    running_apps: Arc<Mutex<HashMap<String, RunningProcess>>>,
    
    /// Conjunto de aplicações em processo de inicialização
    loading_apps: Arc<Mutex<HashSet<String>>>,
}

impl ProcessManager {
    /// Cria uma nova instância do gerenciador de processos
    pub fn new() -> Self {
        Self {
            running_apps: Arc::new(Mutex::new(HashMap::new())),
            loading_apps: Arc::new(Mutex::new(HashSet::new())),
        }
    }
    
    /// Retorna uma referência Arc para os processos em execução
    pub fn running_apps(&self) -> Arc<Mutex<HashMap<String, RunningProcess>>> {
        Arc::clone(&self.running_apps)
    }
    
    /// Retorna uma referência Arc para as aplicações em loading
    pub fn loading_apps(&self) -> Arc<Mutex<HashSet<String>>> {
        Arc::clone(&self.loading_apps)
    }
    
    /// Inicia uma aplicação em um novo terminal Windows.
    /// 
    /// Este método:
    /// 1. Para qualquer processo anterior da mesma aplicação
    /// 2. Marca a aplicação como "loading"
    /// 3. Cria um arquivo batch temporário com os comandos
    /// 4. Executa o batch em um novo terminal
    /// 5. Registra o processo em execução
    /// 
    /// # Argumentos
    /// * `app` - Configuração da aplicação a ser iniciada
    pub fn launch_app(&self, app: &AppConfig) {
        if app.commands.is_empty() {
            return;
        }

        // Para o processo anterior se existir
        self.stop_app(&app.id, Some(&app.name), Some(&app.commands));
        
        // Marca como loading
        {
            let mut loading = self.loading_apps.lock().unwrap();
            loading.insert(app.id.clone());
        }

        // Clona os dados necessários para a thread
        let app_clone = app.clone();
        let running_apps = Arc::clone(&self.running_apps);
        let loading_apps = Arc::clone(&self.loading_apps);

        // Executa em uma thread separada para não bloquear a UI
        std::thread::spawn(move || {
            Self::launch_in_thread(app_clone, running_apps, loading_apps);
        });
    }
    
    /// Lógica de inicialização executada em thread separada
    fn launch_in_thread(
        app: AppConfig,
        running_apps: Arc<Mutex<HashMap<String, RunningProcess>>>,
        loading_apps: Arc<Mutex<HashSet<String>>>,
    ) {
        // Cria um arquivo batch temporário
        let temp_dir = std::env::temp_dir();
        let batch_file = temp_dir.join(format!("iris_{}.bat", app.id));
        
        // Monta o conteúdo do batch
        let batch_content = Self::build_batch_content(&app);
        
        if fs::write(&batch_file, &batch_content).is_err() {
            let mut loading = loading_apps.lock().unwrap();
            loading.remove(&app.id);
            return;
        }

        // Executa o batch
        let child = Command::new("cmd")
            .args(["/C", "start", "", &batch_file.to_string_lossy()])
            .spawn();

        // Aguarda um pouco para o console abrir
        std::thread::sleep(Duration::from_millis(800));

        if let Ok(child) = child {
            // Tenta obter o PID do processo do console
            let console_pid = Self::find_console_pid(&app.name);
            
            let mut running = running_apps.lock().unwrap();
            running.insert(app.id.clone(), RunningProcess::new(child, console_pid));
        }
        
        // Remove do loading
        let mut loading = loading_apps.lock().unwrap();
        loading.remove(&app.id);
    }
    
    /// Constrói o conteúdo do arquivo batch para execução.
    /// 
    /// Trata comandos especiais como npm, yarn, cargo e scripts .bat.
    /// Também detecta automaticamente inputs para scripts interativos.
    fn build_batch_content(app: &AppConfig) -> String {
        let mut batch_content = String::new();
        batch_content.push_str("@echo off\n");
        batch_content.push_str(&format!("title [IRIS] {}\n", app.name));
        
        if !app.working_dir.is_empty() {
            batch_content.push_str(&format!("cd /d \"{}\"\n", app.working_dir));
        }
        
        let commands = &app.commands;
        let mut i = 0;
        while i < commands.len() {
            let cmd = &commands[i];
            let cmd_lower = cmd.to_lowercase();
            
            // Verifica se o próximo comando parece ser um input
            let next_is_input = if i + 1 < commands.len() {
                let next = &commands[i + 1];
                next.chars().all(|c| c.is_numeric() || c == '.')
                    || next.eq_ignore_ascii_case("s")
                    || next.eq_ignore_ascii_case("n")
                    || next.eq_ignore_ascii_case("y")
            } else {
                false
            };
            
            // Se o comando atual é um .bat/.cmd e o próximo é input
            if next_is_input && (cmd_lower.ends_with(".bat") || cmd_lower.ends_with(".cmd")) {
                let input = &commands[i + 1];
                let input_file = format!("iris_input_{}.txt", app.id);
                batch_content.push_str(&format!("echo {}> %TEMP%\\{}\n", input, input_file));
                batch_content.push_str(&format!("call {} < %TEMP%\\{}\n", cmd, input_file));
                batch_content.push_str(&format!("del %TEMP%\\{} 2>nul\n", input_file));
                i += 2;
                batch_content.push_str(&format!("title [IRIS] {}\n", app.name));
                continue;
            }
            
            // Comandos que precisam de "call"
            let needs_call = cmd_lower.starts_with("npm ")
                || cmd_lower.starts_with("yarn ")
                || cmd_lower.starts_with("pnpm ")
                || cmd_lower.starts_with("npx ")
                || cmd_lower.starts_with("dotnet ")
                || cmd_lower.starts_with("cargo ")
                || cmd_lower.ends_with(".bat")
                || cmd_lower.ends_with(".cmd");
            
            if needs_call {
                batch_content.push_str(&format!("call {}\n", cmd));
            } else {
                batch_content.push_str(&format!("{}\n", cmd));
            }
            
            batch_content.push_str(&format!("title [IRIS] {}\n", app.name));
            i += 1;
        }
        
        batch_content.push_str(&format!("title [IRIS] {}\n", app.name));
        batch_content.push_str("cmd /k\n");
        
        batch_content
    }
    
    /// Encontra o PID do console Windows pelo título da janela.
    fn find_console_pid(title: &str) -> Option<u32> {
        let search_title = format!("[IRIS] {}", title);
        
        for _ in 0..5 {
            // Busca pelo título exato
            #[cfg(windows)]
            let output = Command::new("powershell")
                .args([
                    "-NoProfile",
                    "-Command",
                    &format!(
                        "Get-Process cmd -ErrorAction SilentlyContinue | Where-Object {{$_.MainWindowTitle -eq '{}'}} | Select-Object -First 1 -ExpandProperty Id",
                        search_title
                    ),
                ])
                .creation_flags(CREATE_NO_WINDOW)
                .output()
                .ok()?;
            
            #[cfg(not(windows))]
            let output = Command::new("echo")
                .arg("")
                .output()
                .ok()?;

            let pid_str = String::from_utf8_lossy(&output.stdout);
            if let Ok(pid) = pid_str.trim().parse() {
                return Some(pid);
            }
            
            // Tenta com -like se -eq não funcionou
            #[cfg(windows)]
            let output = Command::new("powershell")
                .args([
                    "-NoProfile",
                    "-Command",
                    &format!(
                        "Get-Process cmd -ErrorAction SilentlyContinue | Where-Object {{$_.MainWindowTitle -like '*[IRIS]*{}*'}} | Select-Object -First 1 -ExpandProperty Id",
                        title
                    ),
                ])
                .creation_flags(CREATE_NO_WINDOW)
                .output()
                .ok()?;

            #[cfg(not(windows))]
            let output = Command::new("echo")
                .arg("")
                .output()
                .ok()?;

            let pid_str = String::from_utf8_lossy(&output.stdout);
            if let Ok(pid) = pid_str.trim().parse() {
                return Some(pid);
            }
            
            std::thread::sleep(Duration::from_millis(300));
        }
        
        None
    }
    
    /// Para uma aplicação em execução.
    /// 
    /// Utiliza múltiplas estratégias para garantir que o processo seja terminado:
    /// 1. Mata pelo título do comando
    /// 2. Mata pelo título [IRIS]
    /// 3. Mata pela árvore de processos (PID)
    /// 4. Usa WMIC para matar pelo CommandLine
    /// 
    /// # Argumentos
    /// * `app_id` - ID da aplicação a ser parada
    /// * `app_name` - Nome opcional da aplicação (para busca por título)
    /// * `commands` - Comandos opcionais (para busca por título)
    pub fn stop_app(&self, app_id: &str, app_name: Option<&str>, commands: Option<&Vec<String>>) {
        let mut running = self.running_apps.lock().unwrap();
        
        if let Some(mut process) = running.remove(app_id) {
            // Estratégia 1: Mata pelo título do comando
            if let Some(cmds) = commands {
                for cmd in cmds {
                    #[cfg(windows)]
                    {
                        let _ = Command::new("taskkill")
                            .args(["/F", "/FI", &format!("WINDOWTITLE eq {}", cmd)])
                            .creation_flags(CREATE_NO_WINDOW)
                            .output();
                        
                        let _ = Command::new("taskkill")
                            .args(["/F", "/FI", &format!("WINDOWTITLE eq {}*", cmd)])
                            .creation_flags(CREATE_NO_WINDOW)
                            .output();
                    }
                }
            }

            // Estratégia 2: Pelo título [IRIS] Nome
            if let Some(name) = app_name {
                #[cfg(windows)]
                let _ = Command::new("taskkill")
                    .args(["/F", "/FI", &format!("WINDOWTITLE eq [IRIS] {}", name)])
                    .creation_flags(CREATE_NO_WINDOW)
                    .output();
            }

            // Estratégia 3: Pela árvore de processos
            if let Some(pid) = process.console_pid {
                #[cfg(windows)]
                let _ = Command::new("taskkill")
                    .args(["/F", "/T", "/PID", &pid.to_string()])
                    .creation_flags(CREATE_NO_WINDOW)
                    .output();
            }

            // Estratégia 4: WMIC pelo CommandLine
            #[cfg(windows)]
            {
                let batch_name = format!("iris_{}.bat", app_id);
                let _ = Command::new("cmd")
                    .args(["/C", &format!(
                        "wmic process where \"CommandLine like '%{}%'\" call terminate 2>nul",
                        batch_name
                    )])
                    .creation_flags(CREATE_NO_WINDOW)
                    .output();
            }

            // Estratégia 5: Mata padrões comuns
            #[cfg(windows)]
            for pattern in ["npm*", "node*", "vite*", "yarn*", "pnpm*"] {
                let _ = Command::new("taskkill")
                    .args(["/F", "/FI", &format!("WINDOWTITLE eq {}", pattern)])
                    .creation_flags(CREATE_NO_WINDOW)
                    .output();
            }
            
            // Mata o processo child diretamente
            let _ = process.child.kill();
        }
    }
    
    /// Reinicia uma aplicação.
    /// 
    /// Para o processo atual e inicia novamente após um pequeno delay.
    pub fn restart_app(&self, app: &AppConfig) {
        self.stop_app(&app.id, Some(&app.name), Some(&app.commands));
        std::thread::sleep(Duration::from_millis(200));
        self.launch_app(app);
    }
    
    /// Verifica se uma aplicação está em execução
    pub fn is_running(&self, app_id: &str) -> bool {
        let running = self.running_apps.lock().unwrap();
        running.contains_key(app_id)
    }
    
    /// Verifica se uma aplicação está em processo de inicialização
    pub fn is_loading(&self, app_id: &str) -> bool {
        let loading = self.loading_apps.lock().unwrap();
        loading.contains(app_id)
    }
    
    /// Retorna o número de aplicações em execução
    pub fn running_count(&self) -> usize {
        let running = self.running_apps.lock().unwrap();
        running.len()
    }
    
    /// Verifica se há alguma aplicação em loading
    pub fn has_loading(&self) -> bool {
        let loading = self.loading_apps.lock().unwrap();
        !loading.is_empty()
    }
    
    /// Verifica se há alguma aplicação em execução
    pub fn has_running(&self) -> bool {
        let running = self.running_apps.lock().unwrap();
        !running.is_empty()
    }
    
    /// Limpa processos que morreram do registro.
    /// 
    /// Verifica se os processos registrados ainda estão ativos
    /// e remove os que foram encerrados.
    pub fn cleanup_dead_processes(&self) {
        let mut running = self.running_apps.lock().unwrap();
        let mut to_remove = Vec::new();
        
        for (app_id, process) in running.iter() {
            if let Some(pid) = process.console_pid {
                #[cfg(windows)]
                let output = Command::new("tasklist")
                    .args(["/FI", &format!("PID eq {}", pid), "/NH"])
                    .output();
                
                #[cfg(not(windows))]
                let output = Command::new("ps")
                    .args(["-p", &pid.to_string()])
                    .output();
                
                if let Ok(output) = output {
                    let output_str = String::from_utf8_lossy(&output.stdout);
                    #[cfg(windows)]
                    let is_dead = !output_str.to_lowercase().contains("cmd.exe");
                    #[cfg(not(windows))]
                    let is_dead = output_str.is_empty();
                    
                    if is_dead {
                        to_remove.push(app_id.clone());
                    }
                }
            }
        }
        
        for app_id in to_remove {
            running.remove(&app_id);
        }
    }
}

impl Default for ProcessManager {
    fn default() -> Self {
        Self::new()
    }
}