homeboy 0.80.0

CLI for multi-component deployment and development workflow automation
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
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
//! Symbol reference graph — trace imports, callers, and dependencies across a codebase.
//!
//! Core primitive for understanding how symbols connect across files.
//! Used by: fixer (caller rewriting after dedup), move_items (import rewriting),
//! impact tracing (changed-since), dead code detection.
//!
//! # Architecture
//!
//! ```text
//! utils/grammar.rs           (structural parsing, import pattern matching)
//!//! core/symbol_graph.rs       (this file: reference graph, import rewriting)
//!//! consumers:                 fixer, move_items, impact, dead_code
//! ```
//!
//! Grammar-driven — no extension subprocesses needed. As long as a language
//! has an `import` pattern in its grammar.toml, the full graph works.

use std::collections::HashMap;
use std::path::Path;

use crate::engine::codebase_scan::{self, ExtensionFilter, ScanConfig};
use crate::extension::grammar::{self, Grammar};

// ============================================================================
// Types
// ============================================================================

/// A parsed import statement in a source file.
#[derive(Debug, Clone)]
pub struct ImportRef {
    /// Relative file path from project root.
    pub file: String,
    /// 1-indexed line number of the import statement.
    pub line: usize,
    /// The raw module path from the import (e.g., `crate::core::fixer`).
    pub module_path: String,
    /// Individual symbol names imported (e.g., `["module_path_from_file", "insertion"]`).
    /// Empty if the import is a wildcard or module-level.
    pub imported_names: Vec<String>,
    /// The full original line text.
    pub original_text: String,
}

/// A file that references a symbol, with details about how.
#[derive(Debug, Clone)]
pub struct CallerRef {
    /// Relative file path that contains the reference.
    pub file: String,
    /// The import statement that brings the symbol in (if any).
    pub import: Option<ImportRef>,
    /// Whether the file also calls the symbol (found in content).
    pub has_call_site: bool,
}

/// Result of rewriting an import line.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ImportRewrite {
    /// Relative file path.
    pub file: String,
    /// 1-indexed line number.
    pub line: usize,
    /// Original line text.
    pub original: String,
    /// Replacement line text.
    pub replacement: String,
}

/// Result of a symbol rewrite operation.
#[derive(Debug, Clone)]
pub struct RewriteResult {
    /// Files that had imports rewritten.
    pub rewrites: Vec<ImportRewrite>,
    /// Files where the symbol is called but no import was found to rewrite.
    /// These may need manual attention.
    pub unresolved_callers: Vec<String>,
    /// Whether changes were written to disk.
    pub applied: bool,
}

// ============================================================================
// Module path utilities
// ============================================================================

/// Convert a file path to a Rust module path.
///
/// `"src/core/code_audit/conventions.rs"` → `"core::code_audit::conventions"`
/// `"src/core/code_audit/mod.rs"` → `"core::code_audit"`
/// `"lib/utils.rs"` → `"lib::utils"`
pub fn module_path_from_file(file_path: &str) -> String {
    let p = file_path.strip_prefix("src/").unwrap_or(file_path);
    let p = p.strip_suffix(".rs").unwrap_or(p);
    let p = p.strip_suffix("/mod").unwrap_or(p);
    p.replace('/', "::")
}

// ============================================================================
// Import parsing — grammar-driven
// ============================================================================

/// Parse all import statements from a source file using its grammar.
///
/// Returns structured `ImportRef`s with module paths and imported names.
/// For Rust, handles both simple (`use crate::mod::Item;`) and grouped
/// (`use crate::mod::{A, B};`) imports.
pub fn parse_imports(content: &str, grammar: &Grammar, relative_path: &str) -> Vec<ImportRef> {
    let symbols = grammar::extract(content, grammar);
    let lines: Vec<&str> = content.lines().collect();
    let language_id = grammar.language.id.as_str();

    symbols
        .iter()
        .filter(|s| s.concept == "import")
        .filter_map(|s| {
            let raw_path = s.get("path")?;
            let line_text = lines.get(s.line.saturating_sub(1)).unwrap_or(&"");

            let (module_path, imported_names) = match language_id {
                "rust" => parse_rust_import_path(raw_path),
                "php" | "wordpress" => parse_php_import_path(raw_path),
                _ => (raw_path.to_string(), vec![]),
            };

            Some(ImportRef {
                file: relative_path.to_string(),
                line: s.line,
                module_path,
                imported_names,
                original_text: line_text.to_string(),
            })
        })
        .collect()
}

/// Parse a Rust `use` path into module path + imported names.
///
/// `"crate::core::fixer::module_path_from_file"` → (`"crate::core::fixer"`, `["module_path_from_file"]`)
/// `"crate::core::fixer::{insertion, Fix}"` → (`"crate::core::fixer"`, `["insertion", "Fix"]`)
/// `"std::path::Path"` → (`"std::path"`, `["Path"]`)
fn parse_rust_import_path(raw: &str) -> (String, Vec<String>) {
    // Handle grouped imports: crate::mod::{A, B}
    if let Some(brace_start) = raw.find("::{") {
        let module = &raw[..brace_start];
        let inner = raw[brace_start + 3..]
            .trim_end_matches('}')
            .split(',')
            .map(|s| {
                let s = s.trim();
                // Handle `Name as Alias` — use the original name
                if let Some(pos) = s.find(" as ") {
                    s[..pos].trim().to_string()
                } else {
                    s.to_string()
                }
            })
            .filter(|s| !s.is_empty() && s != "self")
            .collect();
        (module.to_string(), inner)
    } else {
        // Simple import: the last segment is the imported name
        if let Some(last_sep) = raw.rfind("::") {
            let module = &raw[..last_sep];
            let name = &raw[last_sep + 2..];
            if name == "self" || name == "*" {
                (raw.to_string(), vec![])
            } else {
                (module.to_string(), vec![name.to_string()])
            }
        } else {
            // No :: at all — top-level import
            (raw.to_string(), vec![])
        }
    }
}

/// Parse a PHP `use` path into module path + imported name.
///
/// `"App\\Models\\User"` → (`"App\\Models"`, `["User"]`)
fn parse_php_import_path(raw: &str) -> (String, Vec<String>) {
    if let Some(last_sep) = raw.rfind('\\') {
        let module = &raw[..last_sep];
        let name = &raw[last_sep + 1..];
        (module.to_string(), vec![name.to_string()])
    } else {
        (raw.to_string(), vec![])
    }
}

// ============================================================================
// Symbol tracing
// ============================================================================

/// Find all files that reference a symbol exported from a given module.
///
/// Walks the codebase, parses imports in each file, and checks:
/// 1. Does the file import `symbol_name` from `source_module`?
/// 2. Does the file content mention `symbol_name`? (call site check)
///
/// Returns `CallerRef` for each file that references the symbol.
pub fn trace_symbol_callers(
    symbol_name: &str,
    source_module: &str,
    root: &Path,
    file_extensions: &[&str],
) -> Vec<CallerRef> {
    let config = ScanConfig {
        extensions: ExtensionFilter::Only(file_extensions.iter().map(|e| e.to_string()).collect()),
        skip_hidden: true,
        ..Default::default()
    };

    let files = codebase_scan::walk_files(root, &config);
    let mut callers = Vec::new();

    for file_path in &files {
        let rel_path = file_path
            .strip_prefix(root)
            .unwrap_or(file_path)
            .to_string_lossy()
            .to_string();

        let content = match std::fs::read_to_string(file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        // Quick pre-filter: does the file mention the symbol at all?
        if !content.contains(symbol_name) {
            continue;
        }

        let ext = file_path.extension().and_then(|e| e.to_str()).unwrap_or("");

        // Parse imports using grammar
        let matching_import = if let Some(grammar) = load_grammar_for_ext(ext) {
            let imports = parse_imports(&content, &grammar, &rel_path);
            imports.into_iter().find(|imp| {
                imp.imported_names.contains(&symbol_name.to_string())
                    && import_matches_module(&imp.module_path, source_module)
            })
        } else {
            None
        };

        let has_import = matching_import.is_some();
        let has_call_site = content.contains(&format!("{}(", symbol_name))
            || content.contains(&format!("{}::", symbol_name))
            || content.contains(&format!(".{}", symbol_name));

        if has_import || has_call_site {
            callers.push(CallerRef {
                file: rel_path,
                import: matching_import,
                has_call_site,
            });
        }
    }

    callers
}

/// Check if an import's module path matches a source module.
///
/// Handles both absolute (`crate::core::fixer`) and the import's
/// own module path (`core::fixer` without crate:: prefix).
fn import_matches_module(import_module: &str, source_module: &str) -> bool {
    // Direct match
    if import_module == source_module {
        return true;
    }
    // Match with crate:: prefix
    let with_crate = format!("crate::{}", source_module);
    if import_module == with_crate {
        return true;
    }
    // Match without crate:: prefix
    let without_crate = source_module
        .strip_prefix("crate::")
        .unwrap_or(source_module);
    if import_module == without_crate {
        return true;
    }
    // Import has crate:: but source doesn't
    let import_without = import_module
        .strip_prefix("crate::")
        .unwrap_or(import_module);
    import_without == source_module || import_without == without_crate
}

// ============================================================================
// Import rewriting
// ============================================================================

/// Rewrite all imports of a symbol from one module to another.
///
/// Walks the codebase, finds files that import `symbol_name` from
/// `old_module`, and rewrites those imports to point to `new_module`.
///
/// If `write` is true, applies changes to disk.
pub fn rewrite_imports(
    symbol_name: &str,
    old_module: &str,
    new_module: &str,
    root: &Path,
    file_extensions: &[&str],
    write: bool,
) -> RewriteResult {
    let callers = trace_symbol_callers(symbol_name, old_module, root, file_extensions);
    let mut rewrites = Vec::new();
    let mut unresolved = Vec::new();

    for caller in &callers {
        if let Some(ref import) = caller.import {
            if let Some(rewrite) = compute_import_rewrite(import, symbol_name, new_module) {
                rewrites.push(rewrite);
            }
        } else if caller.has_call_site {
            // File calls the symbol but has no matching import — may use
            // a wildcard import, re-export, or local definition.
            unresolved.push(caller.file.clone());
        }
    }

    if write {
        apply_rewrites(&rewrites, root);
    }

    RewriteResult {
        rewrites,
        unresolved_callers: unresolved,
        applied: write,
    }
}

/// Compute the rewritten import line for a single file.
fn compute_import_rewrite(
    import: &ImportRef,
    symbol_name: &str,
    new_module: &str,
) -> Option<ImportRewrite> {
    let original = &import.original_text;
    // Determine the indentation
    let indent = &original[..original.len() - original.trim_start().len()];

    if import.imported_names.len() == 1 {
        // Simple import — rewrite the whole line
        let new_module_with_crate = if new_module.starts_with("crate::") {
            new_module.to_string()
        } else {
            format!("crate::{}", new_module)
        };
        let replacement = format!("{}use {}::{};", indent, new_module_with_crate, symbol_name);
        Some(ImportRewrite {
            file: import.file.clone(),
            line: import.line,
            original: original.to_string(),
            replacement,
        })
    } else if import.imported_names.len() > 1 {
        // Grouped import — need to remove the symbol from the group and add a new import
        // For now, rewrite the whole line to split out the symbol.
        // This handles: `use crate::mod::{A, B, symbol};` → remove symbol from group + add new import.

        // Build the group without the moved symbol
        let remaining: Vec<&String> = import
            .imported_names
            .iter()
            .filter(|n| n.as_str() != symbol_name)
            .collect();

        if remaining.is_empty() {
            // All names were just this symbol — replace the whole line
            let new_module_with_crate = if new_module.starts_with("crate::") {
                new_module.to_string()
            } else {
                format!("crate::{}", new_module)
            };
            let replacement = format!("{}use {}::{};", indent, new_module_with_crate, symbol_name);
            Some(ImportRewrite {
                file: import.file.clone(),
                line: import.line,
                original: original.to_string(),
                replacement,
            })
        } else {
            // Keep remaining in the original group, add new import on a new line
            let old_module_with_crate = if import.module_path.starts_with("crate::") {
                import.module_path.clone()
            } else {
                format!("crate::{}", import.module_path)
            };
            let new_module_with_crate = if new_module.starts_with("crate::") {
                new_module.to_string()
            } else {
                format!("crate::{}", new_module)
            };

            let remaining_str = if remaining.len() == 1 {
                format!("{}use {}::{};", indent, old_module_with_crate, remaining[0])
            } else {
                format!(
                    "{}use {}::{{{}}};",
                    indent,
                    old_module_with_crate,
                    remaining
                        .iter()
                        .map(|s| s.as_str())
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            };

            let replacement = format!(
                "{}\n{}use {}::{};",
                remaining_str, indent, new_module_with_crate, symbol_name
            );

            Some(ImportRewrite {
                file: import.file.clone(),
                line: import.line,
                original: original.to_string(),
                replacement,
            })
        }
    } else {
        // Module-level or wildcard import — can't determine which symbols
        None
    }
}

/// Apply import rewrites to disk.
fn apply_rewrites(rewrites: &[ImportRewrite], root: &Path) {
    // Group rewrites by file
    let mut by_file: HashMap<&str, Vec<&ImportRewrite>> = HashMap::new();
    for rewrite in rewrites {
        by_file
            .entry(rewrite.file.as_str())
            .or_default()
            .push(rewrite);
    }

    for (file, file_rewrites) in &by_file {
        let abs_path = root.join(file);
        let Ok(content) = std::fs::read_to_string(&abs_path) else {
            continue;
        };

        let mut lines: Vec<String> = content.lines().map(String::from).collect();

        // Apply rewrites in reverse line order to avoid index shifting
        let mut sorted_rewrites: Vec<&&ImportRewrite> = file_rewrites.iter().collect();
        sorted_rewrites.sort_by(|a, b| b.line.cmp(&a.line));

        for rewrite in sorted_rewrites {
            let idx = rewrite.line.saturating_sub(1);
            if idx < lines.len() {
                // The replacement may contain newlines (for grouped import splits)
                let replacement_lines: Vec<&str> = rewrite.replacement.lines().collect();
                lines.splice(idx..=idx, replacement_lines.iter().map(|s| s.to_string()));
            }
        }

        let mut modified = lines.join("\n");
        if content.ends_with('\n') && !modified.ends_with('\n') {
            modified.push('\n');
        }

        let _ = std::fs::write(&abs_path, &modified);
    }
}

// ============================================================================
// Grammar loading (shared with core_fingerprint)
// ============================================================================

/// Load a grammar for a file extension.
///
/// This is a shared utility — same as `core_fingerprint::load_grammar_for_ext`
/// but accessible from the symbol_graph module without circular dependency.
fn load_grammar_for_ext(ext: &str) -> Option<Grammar> {
    let matched = crate::extension::find_extension_for_file_ext(ext, "fingerprint")?;
    let extension_path = matched.extension_path.as_deref()?;

    let grammar_path = Path::new(extension_path).join("grammar.toml");
    if grammar_path.exists() {
        return grammar::load_grammar(&grammar_path).ok();
    }

    let grammar_json_path = Path::new(extension_path).join("grammar.json");
    if grammar_json_path.exists() {
        return grammar::load_grammar_json(&grammar_json_path).ok();
    }

    None
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn module_path_from_file_strips_src_and_extension() {
        assert_eq!(
            module_path_from_file("src/core/code_audit/conventions.rs"),
            "core::code_audit::conventions"
        );
    }

    #[test]
    fn module_path_from_file_handles_mod_rs() {
        assert_eq!(
            module_path_from_file("src/core/code_audit/mod.rs"),
            "core::code_audit"
        );
    }

    #[test]
    fn module_path_from_file_no_src_prefix() {
        assert_eq!(module_path_from_file("lib/utils.rs"), "lib::utils");
    }

    #[test]
    fn parse_rust_import_simple() {
        let (module, names) = parse_rust_import_path("crate::core::fixer::module_path_from_file");
        assert_eq!(module, "crate::core::fixer");
        assert_eq!(names, vec!["module_path_from_file"]);
    }

    #[test]
    fn parse_rust_import_grouped() {
        let (module, names) = parse_rust_import_path("crate::core::fixer::{insertion, Fix}");
        assert_eq!(module, "crate::core::fixer");
        assert_eq!(names, vec!["insertion", "Fix"]);
    }

    #[test]
    fn parse_rust_import_self() {
        // `use crate::core::fixer::self;` — the whole path is the module, no named imports
        let (module, names) = parse_rust_import_path("crate::core::fixer::self");
        assert_eq!(module, "crate::core::fixer::self");
        assert!(names.is_empty());
    }

    #[test]
    fn parse_rust_import_wildcard() {
        // `use super::*;` — wildcard, no specific named imports
        let (module, names) = parse_rust_import_path("super::*");
        assert_eq!(module, "super::*");
        assert!(names.is_empty());
    }

    #[test]
    fn parse_rust_import_alias() {
        let (module, names) = parse_rust_import_path("crate::mod::{Foo as Bar, Baz}");
        assert_eq!(module, "crate::mod");
        assert_eq!(names, vec!["Foo", "Baz"]);
    }

    #[test]
    fn parse_php_import() {
        let (module, names) = parse_php_import_path("App\\Models\\User");
        assert_eq!(module, "App\\Models");
        assert_eq!(names, vec!["User"]);
    }

    #[test]
    fn import_matches_module_variants() {
        // Direct match
        assert!(import_matches_module("core::fixer", "core::fixer"));
        // With crate prefix
        assert!(import_matches_module("crate::core::fixer", "core::fixer"));
        // Source has crate prefix
        assert!(import_matches_module("core::fixer", "crate::core::fixer"));
        // Both have crate prefix
        assert!(import_matches_module(
            "crate::core::fixer",
            "crate::core::fixer"
        ));
        // No match
        assert!(!import_matches_module("core::fixer", "core::other"));
    }

    #[test]
    fn parse_imports_with_rust_grammar() {
        let grammar_path =
            std::path::Path::new("/root/.config/homeboy/extensions/rust/grammar.toml");
        if !grammar_path.exists() {
            return; // Skip if grammar not installed
        }
        let grammar = crate::extension::grammar::load_grammar(grammar_path).unwrap();

        let content = r#"use std::path::Path;
use crate::core::fixer::{insertion, Fix};
use crate::extension::grammar;

pub fn hello() {}
"#;

        let imports = parse_imports(content, &grammar, "src/example.rs");

        assert_eq!(imports.len(), 3);

        // First import: std::path::Path
        assert_eq!(imports[0].module_path, "std::path");
        assert_eq!(imports[0].imported_names, vec!["Path"]);
        assert_eq!(imports[0].line, 1);

        // Second import: grouped
        assert_eq!(imports[1].module_path, "crate::core::fixer");
        assert_eq!(imports[1].imported_names, vec!["insertion", "Fix"]);

        // Third import: module-level use
        assert_eq!(imports[2].module_path, "crate::extension");
        assert_eq!(imports[2].imported_names, vec!["grammar"]);
    }

    #[test]
    fn compute_rewrite_simple_import() {
        let import = ImportRef {
            file: "src/core/refactor/move_items.rs".to_string(),
            line: 22,
            module_path: "crate::core::refactor::move_items".to_string(),
            imported_names: vec!["walk_source_files".to_string()],
            original_text: "use crate::core::refactor::move_items::walk_source_files;".to_string(),
        };

        let rewrite =
            compute_import_rewrite(&import, "walk_source_files", "core::refactor::transform")
                .unwrap();
        assert_eq!(
            rewrite.replacement,
            "use crate::core::refactor::transform::walk_source_files;"
        );
    }

    #[test]
    fn compute_rewrite_grouped_import() {
        let import = ImportRef {
            file: "src/example.rs".to_string(),
            line: 5,
            module_path: "crate::core::fixer".to_string(),
            imported_names: vec![
                "insertion".to_string(),
                "module_path_from_file".to_string(),
                "Fix".to_string(),
            ],
            original_text: "use crate::core::fixer::{insertion, module_path_from_file, Fix};"
                .to_string(),
        };

        let rewrite =
            compute_import_rewrite(&import, "module_path_from_file", "core::symbol_graph").unwrap();

        // Should keep remaining in old group and add new import
        assert!(rewrite
            .replacement
            .contains("use crate::core::fixer::{insertion, Fix};"));
        assert!(rewrite
            .replacement
            .contains("use crate::core::symbol_graph::module_path_from_file;"));
    }
}