raz-core 0.2.4

Universal command generator for Rust projects - Core library with stateless file analysis and cursor-aware execution
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Command structures and execution logic

use crate::browser::{extract_server_url, open_browser};
use crate::error::{RazError, RazResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command as TokioCommand;

/// Represents a command that can be executed
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Command {
    /// Unique identifier for this command
    pub id: String,

    /// Human-readable label for display
    pub label: String,

    /// Optional description providing more context
    pub description: Option<String>,

    /// The actual command to execute
    pub command: String,

    /// Command arguments
    pub args: Vec<String>,

    /// Environment variables to set
    pub env: HashMap<String, String>,

    /// Working directory for execution
    pub cwd: Option<PathBuf>,

    /// Command category for grouping
    pub category: CommandCategory,

    /// Priority for sorting (higher = more important)
    pub priority: u8,

    /// Conditions that must be met for this command to be available
    pub conditions: Vec<Condition>,

    /// Tags for filtering and search
    pub tags: Vec<String>,

    /// Whether this command requires user input
    pub requires_input: bool,

    /// Estimated execution time in seconds
    pub estimated_duration: Option<u32>,
}

impl Command {
    /// Create a new command builder
    pub fn builder(id: impl Into<String>, command: impl Into<String>) -> CommandBuilder {
        CommandBuilder::new(id, command)
    }

    /// Execute this command asynchronously
    pub async fn execute(&self) -> RazResult<ExecutionResult> {
        self.execute_with_options(false, None).await
    }

    /// Execute this command with browser launching options
    pub async fn execute_with_browser(
        &self,
        open_browser: bool,
        browser: Option<String>,
    ) -> RazResult<ExecutionResult> {
        self.execute_with_options(open_browser, browser).await
    }

    /// Internal execution with options
    async fn execute_with_options(
        &self,
        should_open_browser: bool,
        browser: Option<String>,
    ) -> RazResult<ExecutionResult> {
        // For long-running/interactive commands, we spawn and inherit stdio
        let is_interactive = self.is_interactive();

        if is_interactive {
            self.execute_interactive_with_browser(should_open_browser, browser)
                .await
        } else {
            self.execute_captured().await
        }
    }

    /// Check if this is an interactive/long-running command
    fn is_interactive(&self) -> bool {
        // Commands that typically run indefinitely or need user interaction
        let is_cargo_leptos = matches!(self.command.as_str(), "cargo-leptos");
        let has_serve_or_watch =
            self.args.contains(&"serve".to_string()) || self.args.contains(&"watch".to_string());
        let has_serve_tag = self.tags.contains(&"serve".to_string());
        let has_watch_tag = self.tags.contains(&"watch".to_string());
        let has_interactive_tag = self.tags.contains(&"interactive".to_string());

        (is_cargo_leptos && has_serve_or_watch)
            || has_serve_tag
            || has_watch_tag
            || has_interactive_tag
    }

    /// Execute command with inherited stdio for interactive processes
    async fn execute_interactive_with_browser(
        &self,
        should_open_browser: bool,
        browser: Option<String>,
    ) -> RazResult<ExecutionResult> {
        let mut cmd = TokioCommand::new(&self.command);
        cmd.args(&self.args);

        // Set environment variables
        for (key, value) in &self.env {
            cmd.env(key, value);
        }

        // Set working directory
        if let Some(cwd) = &self.cwd {
            cmd.current_dir(cwd);
        }

        let start_time = std::time::Instant::now();

        let is_server_cmd = self.is_server_command();

        if should_open_browser && is_server_cmd {
            // For server commands with browser launching, we need to capture initial output
            cmd.stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .stdin(Stdio::inherit());

            let mut child = cmd.spawn().map_err(|e| {
                RazError::execution(format!("Failed to spawn command '{}': {}", self.command, e))
            })?;

            // Spawn tasks to read and forward output while looking for server URL
            let stdout = child.stdout.take().unwrap();
            let stderr = child.stderr.take().unwrap();

            let browser_clone = browser.clone();
            let url_opened = Arc::new(tokio::sync::Mutex::new(false));
            let url_opened_clone = url_opened.clone();

            // Handle stdout
            let stdout_task = tokio::spawn(async move {
                let reader = BufReader::new(stdout);
                let mut lines = reader.lines();

                while let Ok(Some(line)) = lines.next_line().await {
                    println!("{line}");

                    // Check if we should open browser
                    let mut opened = url_opened_clone.lock().await;
                    if !*opened {
                        if let Some(url) = extract_server_url(&line) {
                            if let Err(e) = open_browser(&url, browser_clone.as_deref()) {
                                eprintln!("Warning: Failed to open browser: {e}");
                            } else {
                                println!("\n🌐 Opening {url} in browser...");
                            }
                            *opened = true;
                        }
                    }
                }
            });

            // Handle stderr
            let stderr_task = tokio::spawn(async move {
                let reader = BufReader::new(stderr);
                let mut lines = reader.lines();

                while let Ok(Some(line)) = lines.next_line().await {
                    eprintln!("{line}");
                }
            });

            // Wait for process to complete
            let status = child.wait().await.map_err(|e| {
                RazError::execution(format!(
                    "Failed to wait for command '{}': {}",
                    self.command, e
                ))
            })?;

            // Clean up tasks
            let _ = stdout_task.await;
            let _ = stderr_task.await;

            let duration = start_time.elapsed();

            Ok(ExecutionResult {
                exit_code: status.code().unwrap_or(0),
                stdout: String::new(),
                stderr: String::new(),
                duration,
                command: self.clone(),
            })
        } else {
            // Standard interactive execution
            cmd.stdin(Stdio::inherit())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit());

            let mut child = cmd.spawn().map_err(|e| {
                RazError::execution(format!("Failed to spawn command '{}': {}", self.command, e))
            })?;

            let status = child.wait().await.map_err(|e| {
                RazError::execution(format!(
                    "Failed to wait for command '{}': {}",
                    self.command, e
                ))
            })?;

            let duration = start_time.elapsed();

            Ok(ExecutionResult {
                exit_code: status.code().unwrap_or(0),
                stdout: String::new(), // Output was inherited
                stderr: String::new(), // Output was inherited
                duration,
                command: self.clone(),
            })
        }
    }

    /// Check if this is a server command that might output a URL
    fn is_server_command(&self) -> bool {
        let has_serve_tag = self.tags.contains(&"serve".to_string());
        let has_server_tag = self.tags.contains(&"server".to_string());
        let has_dev_tag = self.tags.contains(&"dev".to_string());
        let has_watch_tag = self.tags.contains(&"watch".to_string());
        let is_cargo_leptos =
            self.command == "cargo" && self.args.first() == Some(&"leptos".to_string());
        let is_cargo_leptos_serve =
            self.command == "cargo-leptos" && self.args.contains(&"serve".to_string());
        let is_cargo_leptos_watch =
            self.command == "cargo-leptos" && self.args.contains(&"watch".to_string());
        let is_trunk_serve = self.command == "trunk" && self.args.contains(&"serve".to_string());
        let is_dx_serve = self.command == "dx" && self.args.contains(&"serve".to_string());

        has_serve_tag
            || has_server_tag
            || has_dev_tag
            || has_watch_tag
            || is_cargo_leptos
            || is_cargo_leptos_serve
            || is_cargo_leptos_watch
            || is_trunk_serve
            || is_dx_serve
    }

    /// Execute command with captured output
    async fn execute_captured(&self) -> RazResult<ExecutionResult> {
        let mut cmd = TokioCommand::new(&self.command);
        cmd.args(&self.args);

        // Set environment variables
        for (key, value) in &self.env {
            cmd.env(key, value);
        }

        // Set working directory
        if let Some(cwd) = &self.cwd {
            cmd.current_dir(cwd);
        }

        // Configure stdio
        cmd.stdout(Stdio::piped()).stderr(Stdio::piped());

        let start_time = std::time::Instant::now();

        let output = cmd.output().await.map_err(|e| {
            RazError::execution(format!(
                "Failed to execute command '{}': {}",
                self.command, e
            ))
        })?;

        let duration = start_time.elapsed();

        Ok(ExecutionResult {
            exit_code: output.status.code().unwrap_or(-1),
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            duration,
            command: self.clone(),
        })
    }

    /// Check if all conditions for this command are met
    pub fn is_available(&self, context: &crate::ProjectContext) -> bool {
        self.conditions
            .iter()
            .all(|condition| condition.is_met(context))
    }

    /// Get the full command line as a string
    pub fn command_line(&self) -> String {
        if self.args.is_empty() {
            self.command.clone()
        } else {
            format!("{} {}", self.command, self.args.join(" "))
        }
    }
}

/// Builder for creating commands
pub struct CommandBuilder {
    command: Command,
}

impl CommandBuilder {
    pub fn new(id: impl Into<String>, command: impl Into<String>) -> Self {
        let command_str = command.into();
        Self {
            command: Command {
                id: id.into(),
                label: command_str.clone(),
                description: None,
                command: command_str,
                args: Vec::new(),
                env: HashMap::new(),
                cwd: None,
                category: CommandCategory::Custom("default".to_string()),
                priority: 50,
                conditions: Vec::new(),
                tags: Vec::new(),
                requires_input: false,
                estimated_duration: None,
            },
        }
    }

    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.command.label = label.into();
        self
    }

    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.command.description = Some(description.into());
        self
    }

    pub fn args(mut self, args: Vec<String>) -> Self {
        self.command.args = args;
        self
    }

    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.command.args.push(arg.into());
        self
    }

    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.command.env.insert(key.into(), value.into());
        self
    }

    pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.command.cwd = Some(cwd.into());
        self
    }

    pub fn category(mut self, category: CommandCategory) -> Self {
        self.command.category = category;
        self
    }

    pub fn priority(mut self, priority: u8) -> Self {
        self.command.priority = priority;
        self
    }

    pub fn condition(mut self, condition: Condition) -> Self {
        self.command.conditions.push(condition);
        self
    }

    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.command.tags.push(tag.into());
        self
    }

    pub fn requires_input(mut self, requires_input: bool) -> Self {
        self.command.requires_input = requires_input;
        self
    }

    pub fn estimated_duration(mut self, seconds: u32) -> Self {
        self.command.estimated_duration = Some(seconds);
        self
    }

    pub fn build(self) -> Command {
        self.command
    }
}

/// Command execution result
#[derive(Debug, Clone)]
pub struct ExecutionResult {
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
    pub duration: std::time::Duration,
    pub command: Command,
}

impl ExecutionResult {
    pub fn is_success(&self) -> bool {
        self.exit_code == 0
    }

    pub fn output(&self) -> &str {
        if self.stdout.is_empty() {
            &self.stderr
        } else {
            &self.stdout
        }
    }
}

/// Categories for grouping commands
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum CommandCategory {
    Build,
    Test,
    Run,
    Debug,
    Deploy,
    Lint,
    Format,
    Generate,
    Install,
    Update,
    Clean,
    Custom(String),
}

impl CommandCategory {
    pub fn as_str(&self) -> &str {
        match self {
            CommandCategory::Build => "build",
            CommandCategory::Test => "test",
            CommandCategory::Run => "run",
            CommandCategory::Debug => "debug",
            CommandCategory::Deploy => "deploy",
            CommandCategory::Lint => "lint",
            CommandCategory::Format => "format",
            CommandCategory::Generate => "generate",
            CommandCategory::Install => "install",
            CommandCategory::Update => "update",
            CommandCategory::Clean => "clean",
            CommandCategory::Custom(name) => name,
        }
    }
}

/// Conditions that determine when a command is available
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Condition {
    /// File must exist
    FileExists(PathBuf),

    /// File pattern must match
    FilePattern(String),

    /// Cursor must be in a function with this name
    CursorInFunction(String),

    /// Cursor must be in a struct with this name
    CursorInStruct(String),

    /// Cursor must be in a test function
    CursorInTest,

    /// Project must have this dependency
    HasDependency(String),

    /// Must be in a workspace
    InWorkspace,

    /// Must be on a specific platform
    Platform(String),

    /// Custom condition with expression
    Expression(String),
}

impl Condition {
    /// Check if this condition is met in the given context
    pub fn is_met(&self, context: &crate::ProjectContext) -> bool {
        match self {
            Condition::FileExists(path) => {
                let full_path = if path.is_absolute() {
                    path.clone()
                } else {
                    context.workspace_root.join(path)
                };
                full_path.exists()
            }
            Condition::FilePattern(pattern) => {
                Self::check_file_pattern(&context.workspace_root, pattern)
            }
            Condition::CursorInFunction(name) => {
                if let Some(file_context) = &context.current_file {
                    if let Some(symbol) = &file_context.cursor_symbol {
                        return symbol.name == *name && symbol.kind == crate::SymbolKind::Function;
                    }
                }
                false
            }
            Condition::CursorInStruct(name) => {
                if let Some(file_context) = &context.current_file {
                    if let Some(symbol) = &file_context.cursor_symbol {
                        return symbol.name == *name && symbol.kind == crate::SymbolKind::Struct;
                    }
                }
                false
            }
            Condition::CursorInTest => {
                if let Some(file_context) = &context.current_file {
                    if let Some(symbol) = &file_context.cursor_symbol {
                        return symbol.kind == crate::SymbolKind::Test;
                    }
                }
                false
            }
            Condition::HasDependency(dep) => context.dependencies.iter().any(|d| d.name == *dep),
            Condition::InWorkspace => context.workspace_members.len() > 1,
            Condition::Platform(platform) => {
                cfg!(target_os = "windows") && platform == "windows"
                    || cfg!(target_os = "macos") && platform == "macos"
                    || cfg!(target_os = "linux") && platform == "linux"
            }
            Condition::Expression(_expr) => {
                // TODO: Implement expression evaluation
                false
            }
        }
    }

    /// Check if any files matching the given pattern exist in the workspace
    fn check_file_pattern(workspace_root: &Path, pattern: &str) -> bool {
        // Create the full pattern by joining with workspace root
        let full_pattern = workspace_root.join(pattern);
        let pattern_str = full_pattern.to_string_lossy();

        // Use glob to find matching files
        match glob::glob(&pattern_str) {
            Ok(paths) => {
                // Check if any paths match
                paths.filter_map(Result::ok).next().is_some()
            }
            Err(_) => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_command_builder() {
        let cmd = Command::builder("test-build", "cargo")
            .label("Build Project")
            .description("Build the project in debug mode")
            .arg("build")
            .category(CommandCategory::Build)
            .priority(10)
            .tag("cargo")
            .build();

        assert_eq!(cmd.id, "test-build");
        assert_eq!(cmd.command, "cargo");
        assert_eq!(cmd.args, vec!["build"]);
        assert_eq!(cmd.category, CommandCategory::Build);
        assert_eq!(cmd.priority, 10);
        assert!(cmd.tags.contains(&"cargo".to_string()));
    }

    #[test]
    fn test_command_line() {
        let cmd = Command::builder("test", "cargo")
            .args(vec!["test".to_string(), "--release".to_string()])
            .build();

        assert_eq!(cmd.command_line(), "cargo test --release");
    }

    #[tokio::test]
    async fn test_command_execution() {
        let cmd = Command::builder("echo-test", "echo").arg("hello").build();

        let result = cmd.execute().await.unwrap();
        assert!(result.is_success());
        assert_eq!(result.stdout.trim(), "hello");
    }

    #[tokio::test]
    async fn test_leptos_watch_command() {
        // Create a command that mimics the leptos-watch command from LeptosProvider
        let command = CommandBuilder::new("leptos-watch", "cargo-leptos")
            .label("Leptos Dev Watch")
            .description("Development server with auto-reload (recommended for development)")
            .arg("watch")
            .category(CommandCategory::Run)
            .priority(95)
            .tag("dev")
            .tag("watch")
            .tag("leptos")
            .estimated_duration(5)
            .build();

        // Test that the command is properly configured
        assert_eq!(command.command, "cargo-leptos");
        assert_eq!(command.args, vec!["watch"]);
        assert!(command.tags.contains(&"dev".to_string()));
        assert!(command.tags.contains(&"watch".to_string()));
        assert!(command.tags.contains(&"leptos".to_string()));

        // Note: Actual execution would fail without cargo-leptos installed
        // This test just verifies the command structure
    }
}