codeprysm-cli 0.1.0

CLI for CodePrism code analysis and search
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! Components command - Component management and analysis
//!
//! Provides commands for listing components, analyzing dependencies,
//! and determining affected components from file changes.

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use codeprysm_backend::Backend;

use super::create_backend;
use crate::GlobalOptions;

/// Component management commands
#[derive(Subcommand, Debug)]
pub enum ComponentsCommand {
    /// List all detected components
    List(ListArgs),

    /// Show dependencies for a component
    Deps(DepsArgs),

    /// Show components affected by file changes
    Affected(AffectedArgs),

    /// Visualize component dependency graph
    Graph(GraphVisualizationArgs),
}

#[derive(Args, Debug)]
pub struct ListArgs {
    /// Filter by component type (e.g., rust, npm, python)
    #[arg(long, short = 't')]
    component_type: Option<String>,

    /// Show only workspace root components
    #[arg(long)]
    roots_only: bool,

    /// Output as JSON
    #[arg(long)]
    json: bool,
}

#[derive(Args, Debug)]
pub struct DepsArgs {
    /// Component name or path pattern
    name: String,

    /// Show transitive dependencies (full dependency tree)
    #[arg(long, short = 'a')]
    all: bool,

    /// Show reverse dependencies (what depends on this)
    #[arg(long, short = 'r')]
    reverse: bool,

    /// Maximum depth for transitive dependencies
    #[arg(long, short = 'd', default_value = "10")]
    depth: usize,

    /// Output as JSON
    #[arg(long)]
    json: bool,
}

#[derive(Args, Debug)]
pub struct AffectedArgs {
    /// Paths to check for affected components (files or directories)
    #[arg(required = true)]
    paths: Vec<PathBuf>,

    /// Compare against a git ref (e.g., HEAD, main)
    #[arg(long)]
    base: Option<String>,

    /// Include transitive dependents (components that depend on affected)
    #[arg(long, short = 'a')]
    all: bool,

    /// Output as JSON
    #[arg(long)]
    json: bool,
}

#[derive(Args, Debug)]
pub struct GraphVisualizationArgs {
    /// Output format: dot, mermaid, or ascii
    #[arg(long, short = 'f', default_value = "ascii")]
    format: GraphFormat,

    /// Include only these components (comma-separated)
    #[arg(long)]
    include: Option<String>,

    /// Exclude these components (comma-separated)
    #[arg(long)]
    exclude: Option<String>,

    /// Maximum depth from root components
    #[arg(long, short = 'd')]
    depth: Option<usize>,
}

#[derive(Debug, Clone, Copy, clap::ValueEnum)]
pub enum GraphFormat {
    /// Graphviz DOT format
    Dot,
    /// Mermaid diagram format
    Mermaid,
    /// ASCII art tree
    Ascii,
}

/// Execute component commands
pub async fn execute(cmd: ComponentsCommand, global: GlobalOptions) -> Result<()> {
    match cmd {
        ComponentsCommand::List(args) => execute_list(args, global).await,
        ComponentsCommand::Deps(args) => execute_deps(args, global).await,
        ComponentsCommand::Affected(args) => execute_affected(args, global).await,
        ComponentsCommand::Graph(args) => execute_graph(args, global).await,
    }
}

async fn execute_list(args: ListArgs, global: GlobalOptions) -> Result<()> {
    let backend = create_backend(&global).await?;

    // Find all component nodes
    let nodes = backend
        .find_nodes("*", Some("Container"), 1000)
        .await
        .context("Failed to query components")?;

    let components: Vec<_> = nodes
        .into_iter()
        .filter(|n| n.kind.as_deref() == Some("component"))
        .filter(|n| {
            if args.roots_only {
                n.metadata
                    .get("is_workspace_root")
                    .map(|v| v == "true")
                    .unwrap_or(false)
            } else {
                true
            }
        })
        .filter(|n| {
            if let Some(ref comp_type) = args.component_type {
                // Filter by inferred component type from manifest_path
                if let Some(manifest) = n.metadata.get("manifest_path") {
                    manifest.contains(comp_type)
                } else {
                    false
                }
            } else {
                true
            }
        })
        .collect();

    if args.json {
        let json_output: Vec<_> = components
            .iter()
            .map(|c| {
                serde_json::json!({
                    "name": c.name,
                    "path": c.file_path,
                    "is_workspace_root": c.metadata.get("is_workspace_root").map(|v| v == "true").unwrap_or(false),
                    "manifest_path": c.metadata.get("manifest_path"),
                })
            })
            .collect();
        println!("{}", serde_json::to_string_pretty(&json_output)?);
    } else {
        if components.is_empty() {
            if !global.quiet {
                println!("No components found.");
                println!(
                    "\nHint: Components are detected from manifest files (Cargo.toml, package.json, etc.)"
                );
                println!(
                    "Run 'codeprysm update' to refresh the graph if you've added manifest files."
                );
            }
            return Ok(());
        }

        if !global.quiet {
            println!("Found {} components:\n", components.len());
        }

        for comp in &components {
            let root_marker = if comp
                .metadata
                .get("is_workspace_root")
                .map(|v| v == "true")
                .unwrap_or(false)
            {
                " [root]"
            } else {
                ""
            };

            println!(
                "  {}{} - {}",
                comp.name,
                root_marker,
                comp.file_path.as_deref().unwrap_or("unknown")
            );
        }
    }

    Ok(())
}

async fn execute_deps(args: DepsArgs, global: GlobalOptions) -> Result<()> {
    let backend = create_backend(&global).await?;

    // Find the component by name pattern
    let nodes = backend
        .find_nodes(&args.name, Some("Container"), 10)
        .await
        .context("Failed to find component")?;

    let component = nodes
        .into_iter()
        .find(|n| n.kind.as_deref() == Some("component"))
        .ok_or_else(|| anyhow::anyhow!("Component '{}' not found", args.name))?;

    let direction = if args.reverse { "incoming" } else { "outgoing" };

    if args.all {
        // Transitive dependencies
        let mut visited = HashSet::new();
        let mut deps = Vec::new();

        collect_transitive_deps(
            &*backend,
            &component.id,
            direction,
            args.depth,
            0,
            &mut visited,
            &mut deps,
        )
        .await?;

        if args.json {
            println!("{}", serde_json::to_string_pretty(&deps)?);
        } else {
            let dep_type = if args.reverse {
                "reverse dependencies (what depends on this)"
            } else {
                "dependencies"
            };

            if !global.quiet {
                println!("{} for '{}':\n", dep_type, component.name);
            }

            if deps.is_empty() {
                println!("  (none)");
            } else {
                print_dependency_tree(&deps, 0);
            }
        }
    } else {
        // Direct dependencies only
        let edges = backend
            .get_edges(&component.id, Some("DependsOn"), direction)
            .await
            .context("Failed to get dependencies")?;

        if args.json {
            let json_output: Vec<_> = edges
                .iter()
                .map(|e| {
                    let dep_id = if args.reverse { &e.from_id } else { &e.to_id };
                    serde_json::json!({
                        "id": dep_id,
                        "version_spec": e.metadata.get("version_spec"),
                        "is_dev": e.metadata.get("is_dev_dependency").map(|v| v == "true").unwrap_or(false),
                    })
                })
                .collect();
            println!("{}", serde_json::to_string_pretty(&json_output)?);
        } else {
            let dep_type = if args.reverse {
                "Dependents of"
            } else {
                "Dependencies of"
            };

            if !global.quiet {
                println!("{} '{}':\n", dep_type, component.name);
            }

            if edges.is_empty() {
                println!("  (none)");
            } else {
                for edge in &edges {
                    let dep_id = if args.reverse {
                        &edge.from_id
                    } else {
                        &edge.to_id
                    };
                    let version = edge
                        .metadata
                        .get("version_spec")
                        .map(|v| format!(" ({})", v))
                        .unwrap_or_default();
                    let dev_marker = if edge
                        .metadata
                        .get("is_dev_dependency")
                        .map(|v| v == "true")
                        .unwrap_or(false)
                    {
                        " [dev]"
                    } else {
                        ""
                    };

                    // Extract just the component name from the node ID
                    let name = dep_id.rsplit(':').next().unwrap_or(dep_id);
                    println!("  {}{}{}", name, version, dev_marker);
                }
            }
        }
    }

    Ok(())
}

/// Dependency info for tree building
#[derive(Debug, serde::Serialize)]
struct DepInfo {
    name: String,
    id: String,
    depth: usize,
    version_spec: Option<String>,
    is_dev: bool,
    children: Vec<DepInfo>,
}

fn collect_transitive_deps<'a, B: Backend + 'a>(
    backend: &'a B,
    node_id: &'a str,
    direction: &'a str,
    max_depth: usize,
    current_depth: usize,
    visited: &'a mut HashSet<String>,
    deps: &'a mut Vec<DepInfo>,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
    Box::pin(async move {
        if current_depth >= max_depth || visited.contains(node_id) {
            return Ok(());
        }
        visited.insert(node_id.to_string());

        let edges = backend
            .get_edges(node_id, Some("DependsOn"), direction)
            .await?;

        for edge in edges {
            let dep_id = if direction == "incoming" {
                &edge.from_id
            } else {
                &edge.to_id
            };

            let name = dep_id.rsplit(':').next().unwrap_or(dep_id).to_string();

            let mut dep = DepInfo {
                name,
                id: dep_id.clone(),
                depth: current_depth + 1,
                version_spec: edge.metadata.get("version_spec").cloned(),
                is_dev: edge
                    .metadata
                    .get("is_dev_dependency")
                    .map(|v| v == "true")
                    .unwrap_or(false),
                children: Vec::new(),
            };

            // Recurse for transitive dependencies
            let mut children = Vec::new();
            collect_transitive_deps(
                backend,
                dep_id,
                direction,
                max_depth,
                current_depth + 1,
                visited,
                &mut children,
            )
            .await?;
            dep.children = children;

            deps.push(dep);
        }

        Ok(())
    })
}

fn print_dependency_tree(deps: &[DepInfo], indent: usize) {
    for dep in deps {
        let prefix = "  ".repeat(indent);
        let version = dep
            .version_spec
            .as_ref()
            .map(|v| format!(" ({})", v))
            .unwrap_or_default();
        let dev = if dep.is_dev { " [dev]" } else { "" };

        println!("{}{}{}{}", prefix, dep.name, version, dev);

        if !dep.children.is_empty() {
            print_dependency_tree(&dep.children, indent + 1);
        }
    }
}

async fn execute_affected(args: AffectedArgs, global: GlobalOptions) -> Result<()> {
    let backend = create_backend(&global).await?;
    let workspace = super::resolve_workspace(&global).await?;

    // Determine which files changed
    let changed_files: Vec<PathBuf> = if let Some(ref base) = args.base {
        // Use git diff to find changed files
        let output = std::process::Command::new("git")
            .args(["diff", "--name-only", base])
            .current_dir(&workspace)
            .output()
            .context("Failed to run git diff")?;

        if !output.status.success() {
            anyhow::bail!(
                "git diff failed: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(|l| workspace.join(l))
            .collect()
    } else {
        // Use the provided paths directly
        args.paths
            .iter()
            .map(|p| {
                if p.is_absolute() {
                    p.clone()
                } else {
                    workspace.join(p)
                }
            })
            .collect()
    };

    if changed_files.is_empty() {
        if !global.quiet {
            println!("No changed files found.");
        }
        return Ok(());
    }

    // Find components that contain the changed files
    let mut affected_components = HashSet::new();

    // Get all components
    let nodes = backend
        .find_nodes("*", Some("Container"), 1000)
        .await
        .context("Failed to query components")?;

    let components: Vec<_> = nodes
        .into_iter()
        .filter(|n| n.kind.as_deref() == Some("component"))
        .collect();

    // For each changed file, find which component contains it
    for file in &changed_files {
        let relative = file
            .strip_prefix(&workspace)
            .unwrap_or(file)
            .to_string_lossy();

        // Find the component whose path is a prefix of this file
        for comp in &components {
            if let Some(ref comp_path) = comp.file_path {
                if relative.starts_with(comp_path.trim_end_matches('/')) {
                    affected_components.insert(comp.id.clone());
                }
            }
        }
    }

    // If --all, also find transitive dependents
    if args.all {
        let mut to_check: Vec<_> = affected_components.iter().cloned().collect();
        let mut checked = HashSet::new();

        while let Some(comp_id) = to_check.pop() {
            if checked.contains(&comp_id) {
                continue;
            }
            checked.insert(comp_id.clone());

            let edges = backend
                .get_edges(&comp_id, Some("DependsOn"), "incoming")
                .await?;

            for edge in edges {
                if !affected_components.contains(&edge.from_id) {
                    affected_components.insert(edge.from_id.clone());
                    to_check.push(edge.from_id);
                }
            }
        }
    }

    // Map IDs back to component info
    let affected: Vec<_> = components
        .iter()
        .filter(|c| affected_components.contains(&c.id))
        .collect();

    if args.json {
        let json_output: Vec<_> = affected
            .iter()
            .map(|c| {
                serde_json::json!({
                    "name": c.name,
                    "path": c.file_path,
                })
            })
            .collect();
        println!("{}", serde_json::to_string_pretty(&json_output)?);
    } else {
        if !global.quiet {
            println!(
                "Affected components ({} from {} changed files):\n",
                affected.len(),
                changed_files.len()
            );
        }

        if affected.is_empty() {
            println!("  (none)");
        } else {
            for comp in &affected {
                println!(
                    "  {} - {}",
                    comp.name,
                    comp.file_path.as_deref().unwrap_or("unknown")
                );
            }
        }
    }

    Ok(())
}

async fn execute_graph(args: GraphVisualizationArgs, global: GlobalOptions) -> Result<()> {
    let backend = create_backend(&global).await?;

    // Get all components
    let nodes = backend
        .find_nodes("*", Some("Container"), 1000)
        .await
        .context("Failed to query components")?;

    let components: Vec<_> = nodes
        .into_iter()
        .filter(|n| n.kind.as_deref() == Some("component"))
        .collect();

    // Apply include/exclude filters
    let include_set: Option<HashSet<_>> = args
        .include
        .as_ref()
        .map(|s| s.split(',').map(|n| n.trim().to_string()).collect());

    let exclude_set: HashSet<_> = args
        .exclude
        .as_ref()
        .map(|s| s.split(',').map(|n| n.trim().to_string()).collect())
        .unwrap_or_default();

    let filtered_components: Vec<_> = components
        .iter()
        .filter(|c| {
            if let Some(ref include) = include_set {
                include.contains(&c.name)
            } else {
                true
            }
        })
        .filter(|c| !exclude_set.contains(&c.name))
        .collect();

    // Build edges map
    let mut edges: Vec<(String, String)> = Vec::new();

    for comp in &filtered_components {
        let deps = backend
            .get_edges(&comp.id, Some("DependsOn"), "outgoing")
            .await?;

        for edge in deps {
            let target_name = edge.to_id.rsplit(':').next().unwrap_or(&edge.to_id);
            edges.push((comp.name.clone(), target_name.to_string()));
        }
    }

    match args.format {
        GraphFormat::Dot => {
            println!("digraph components {{");
            println!("  rankdir=LR;");
            println!("  node [shape=box];");
            for comp in &filtered_components {
                println!("  \"{}\";", comp.name);
            }
            for (from, to) in &edges {
                println!("  \"{}\" -> \"{}\";", from, to);
            }
            println!("}}");
        }
        GraphFormat::Mermaid => {
            println!("graph LR");
            for (from, to) in &edges {
                println!("  {} --> {}", from.replace('-', "_"), to.replace('-', "_"));
            }
        }
        GraphFormat::Ascii => {
            if filtered_components.is_empty() {
                println!("No components found.");
                return Ok(());
            }

            // Build dependency tree from roots
            let roots: Vec<_> = filtered_components
                .iter()
                .filter(|c| {
                    c.metadata
                        .get("is_workspace_root")
                        .map(|v| v == "true")
                        .unwrap_or(false)
                })
                .collect();

            if roots.is_empty() {
                // Just list all components
                println!("Components (no root detected):");
                for comp in &filtered_components {
                    println!("  {}", comp.name);
                }
            } else {
                println!("Component dependency tree:\n");

                // Build adjacency list
                let mut adj: HashMap<String, Vec<String>> = HashMap::new();
                for (from, to) in &edges {
                    adj.entry(from.clone()).or_default().push(to.clone());
                }

                for root in &roots {
                    print_ascii_tree(&root.name, &adj, 0, &mut HashSet::new());
                }
            }
        }
    }

    Ok(())
}

fn print_ascii_tree(
    name: &str,
    adj: &HashMap<String, Vec<String>>,
    depth: usize,
    visited: &mut HashSet<String>,
) {
    let indent = "  ".repeat(depth);
    let marker = if depth == 0 { "" } else { "|- " };

    if visited.contains(name) {
        println!("{}{}{} (circular)", indent, marker, name);
        return;
    }
    visited.insert(name.to_string());

    println!("{}{}{}", indent, marker, name);

    if let Some(deps) = adj.get(name) {
        for dep in deps {
            print_ascii_tree(dep, adj, depth + 1, visited);
        }
    }

    visited.remove(name);
}