homeboy 0.76.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
//! Struct field propagation — add missing fields to struct instantiations after
//! a struct definition changes.
//!
//! Scans the codebase for instantiations of a named struct, detects which fields
//! are missing, and inserts them with sensible defaults. Uses the Rust extension's
//! `propagate_struct_fields` refactor script to do the actual analysis.

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

use serde::Serialize;

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

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

/// A struct field discovered during propagation.
#[derive(Debug, Clone, Serialize)]
pub struct PropagateField {
    pub name: String,
    #[serde(rename = "type")]
    pub field_type: String,
    pub default: String,
}

/// A single edit to insert a missing field at a specific line.
#[derive(Debug, Clone, Serialize)]
pub struct PropagateEdit {
    pub file: String,
    pub line: usize,
    pub insert_text: String,
    pub description: String,
}

/// Result of a propagation analysis.
#[derive(Debug, Serialize)]
pub struct PropagateResult {
    pub struct_name: String,
    pub definition_file: String,
    pub fields: Vec<PropagateField>,
    pub files_scanned: usize,
    pub instantiations_found: usize,
    pub instantiations_needing_fix: usize,
    pub edits: Vec<PropagateEdit>,
    pub applied: bool,
}

// ============================================================================
// Public API
// ============================================================================

/// Configuration for a propagation run.
pub struct PropagateConfig<'a> {
    pub struct_name: &'a str,
    /// Explicit definition file path (auto-detected if `None`).
    pub definition_file: Option<&'a str>,
    pub root: &'a Path,
    pub write: bool,
}

/// Run struct field propagation: find instantiations with missing fields and
/// optionally insert defaults.
pub fn propagate(config: &PropagateConfig) -> Result<PropagateResult, Error> {
    let root = config.root;
    let struct_name = config.struct_name;

    // Step 1: Find the struct definition file
    let def_file = if let Some(f) = config.definition_file {
        PathBuf::from(f)
    } else {
        find_struct_definition(struct_name, root)?
    };

    let def_path = if def_file.is_absolute() {
        def_file.clone()
    } else {
        root.join(&def_file)
    };

    let def_content = std::fs::read_to_string(&def_path).map_err(|e| {
        Error::internal_io(
            e.to_string(),
            Some(format!(
                "read struct definition from {}",
                def_path.display()
            )),
        )
    })?;

    // Step 2: Extract the struct source block
    let struct_source = extract_struct_source(struct_name, &def_content).ok_or_else(|| {
        Error::validation_invalid_argument(
            "struct_name",
            format!(
                "Could not find struct `{}` in {}",
                struct_name,
                def_path.display()
            ),
            None,
            None,
        )
    })?;

    // Step 3: Find the extension for .rs files
    let ext_manifest = extension::find_extension_for_file_ext("rs", "refactor").ok_or_else(|| {
        Error::validation_invalid_argument(
            "extension",
            "No extension with refactor capability found for .rs files. Install the Rust extension.",
            None,
            None,
        )
    })?;

    // Step 4: Walk all .rs files using canonical scanner
    let scan_config = ScanConfig {
        extensions: ExtensionFilter::Only(vec!["rs".to_string()]),
        skip_hidden: true,
        ..Default::default()
    };
    let rs_files = codebase_scan::walk_files(root, &scan_config);

    let def_relative = def_file
        .strip_prefix(root)
        .unwrap_or(&def_file)
        .to_string_lossy()
        .to_string();

    let mut all_edits: Vec<PropagateEdit> = Vec::new();
    let mut total_instantiations = 0usize;
    let mut total_needing_fix = 0usize;
    let mut files_scanned = 0usize;

    crate::log_status!(
        "propagate",
        "Scanning {} .rs files for {} instantiations",
        rs_files.len(),
        struct_name
    );

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

        let Ok(file_content) = std::fs::read_to_string(file_path) else {
            continue;
        };

        // Quick check: skip files that don't mention the struct name
        if !file_content.contains(struct_name) {
            continue;
        }

        files_scanned += 1;

        let cmd = serde_json::json!({
            "command": "propagate_struct_fields",
            "struct_name": struct_name,
            "struct_source": struct_source,
            "file_content": file_content,
            "file_path": relative,
        });

        let Some(result) = extension::run_refactor_script(&ext_manifest, &cmd) else {
            crate::log_status!("warning", "Extension returned no result for {}", relative);
            continue;
        };

        if let Some(found) = result.get("instantiations_found").and_then(|v| v.as_u64()) {
            total_instantiations += found as usize;
        }
        if let Some(needing) = result
            .get("instantiations_needing_fix")
            .and_then(|v| v.as_u64())
        {
            total_needing_fix += needing as usize;
        }

        if let Some(edits) = result.get("edits").and_then(|v| v.as_array()) {
            for edit in edits {
                let file = edit
                    .get("file")
                    .and_then(|v| v.as_str())
                    .unwrap_or(&relative)
                    .to_string();
                let line = edit.get("line").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
                let insert_text = edit
                    .get("insert_text")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                let description = edit
                    .get("description")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();

                all_edits.push(PropagateEdit {
                    file,
                    line,
                    insert_text,
                    description,
                });
            }
        }
    }

    // Step 5: Apply edits if write mode
    let applied = if config.write && !all_edits.is_empty() {
        apply_propagate_edits(&all_edits, root)?;
        true
    } else {
        false
    };

    // Extract field info from collected edits
    let fields = extract_fields_from_edits(&all_edits);

    Ok(PropagateResult {
        struct_name: struct_name.to_string(),
        definition_file: def_relative,
        fields,
        files_scanned,
        instantiations_found: total_instantiations,
        instantiations_needing_fix: total_needing_fix,
        edits: all_edits,
        applied,
    })
}

// ============================================================================
// Internal helpers
// ============================================================================

/// Find the file containing a struct definition by scanning the codebase.
fn find_struct_definition(struct_name: &str, root: &Path) -> Result<PathBuf, Error> {
    let pattern = format!("pub struct {} ", struct_name);
    let pattern_brace = format!("pub struct {} {{", struct_name);
    let pattern_crate = format!("pub(crate) struct {} ", struct_name);
    let pattern_crate_brace = format!("pub(crate) struct {} {{", struct_name);

    let scan_config = ScanConfig {
        extensions: ExtensionFilter::Only(vec!["rs".to_string()]),
        skip_hidden: true,
        ..Default::default()
    };
    let files = codebase_scan::walk_files(root, &scan_config);

    for file_path in &files {
        let Ok(content) = std::fs::read_to_string(file_path) else {
            continue;
        };
        if content.contains(&pattern)
            || content.contains(&pattern_brace)
            || content.contains(&pattern_crate)
            || content.contains(&pattern_crate_brace)
        {
            return Ok(file_path.clone());
        }
    }

    Err(Error::validation_invalid_argument(
        "struct_name",
        format!(
            "Could not find struct `{}` in any .rs file under {}",
            struct_name,
            root.display()
        ),
        None,
        Some(vec![format!(
            "homeboy refactor propagate --struct {} --definition src/path/to/file.rs",
            struct_name
        )]),
    ))
}

/// Extract the full struct source block (including doc comments and attributes)
/// from file content.
fn extract_struct_source(struct_name: &str, content: &str) -> Option<String> {
    let lines: Vec<&str> = content.lines().collect();

    let struct_pattern = format!("struct {} ", struct_name);
    let struct_pattern_brace = format!("struct {} {{", struct_name);
    let mut start_line = None;

    for (i, line) in lines.iter().enumerate() {
        if line.contains(&struct_pattern) || line.contains(&struct_pattern_brace) {
            // Walk backwards to include attributes and doc comments
            let mut actual_start = i;
            for j in (0..i).rev() {
                let trimmed = lines[j].trim();
                if trimmed.starts_with('#')
                    || trimmed.starts_with("///")
                    || trimmed.starts_with("//!")
                {
                    actual_start = j;
                } else if trimmed.is_empty() {
                    if j > 0
                        && (lines[j - 1].trim().starts_with('#')
                            || lines[j - 1].trim().starts_with("///"))
                    {
                        actual_start = j;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
            start_line = Some(actual_start);
            break;
        }
    }

    let start = start_line?;

    // Find the closing brace
    let mut depth = 0i32;
    let mut found_open = false;
    let mut end_line = start;

    for (i, line_content) in lines.iter().enumerate().skip(start) {
        for ch in line_content.chars() {
            if ch == '{' {
                depth += 1;
                found_open = true;
            } else if ch == '}' {
                depth -= 1;
            }
        }
        if found_open && depth == 0 {
            end_line = i;
            break;
        }
    }

    Some(lines[start..=end_line].join("\n"))
}

/// Apply propagate edits to disk. Edits are line-based insertions, applied
/// bottom-to-top to preserve line numbers.
fn apply_propagate_edits(edits: &[PropagateEdit], root: &Path) -> Result<(), Error> {
    let mut edits_by_file: HashMap<&str, Vec<&PropagateEdit>> = HashMap::new();
    for edit in edits {
        edits_by_file.entry(&edit.file).or_default().push(edit);
    }

    for (file, file_edits) in &edits_by_file {
        let file_path = root.join(file);
        let content = std::fs::read_to_string(&file_path)
            .map_err(|e| Error::internal_io(e.to_string(), Some(format!("read {}", file))))?;

        let lines: Vec<&str> = content.lines().collect();
        let mut sorted_edits: Vec<&&PropagateEdit> = file_edits.iter().collect();
        sorted_edits.sort_by(|a, b| b.line.cmp(&a.line));

        let mut mutable_lines: Vec<String> = lines.iter().map(|l| l.to_string()).collect();

        for edit in &sorted_edits {
            let insert_idx = edit.line.saturating_sub(1);
            if insert_idx <= mutable_lines.len() {
                mutable_lines.insert(insert_idx, edit.insert_text.clone());
            }
        }

        let new_content = mutable_lines.join("\n");

        let final_content = if content.ends_with('\n') && !new_content.ends_with('\n') {
            format!("{}\n", new_content)
        } else {
            new_content
        };

        std::fs::write(&file_path, &final_content)
            .map_err(|e| Error::internal_io(e.to_string(), Some(format!("write {}", file))))?;

        crate::log_status!("write", "{} ({} edits)", file, file_edits.len());
    }

    Ok(())
}

/// Extract field information from propagation edits.
///
/// Each edit's `description` contains the field name (between backticks) and the
/// `insert_text` contains the default value (after the colon).
fn extract_fields_from_edits(edits: &[PropagateEdit]) -> Vec<PropagateField> {
    let mut seen = HashSet::new();
    edits
        .iter()
        .filter_map(|e| {
            let start = e.description.find('`')? + 1;
            let end = e.description[start..].find('`')? + start;
            let field_name = &e.description[start..end];
            if seen.insert(field_name.to_string()) {
                let trimmed = e.insert_text.trim().trim_end_matches(',');
                let colon_pos = trimmed.find(':')?;
                let default = trimmed[colon_pos + 1..].trim().to_string();
                Some(PropagateField {
                    name: field_name.to_string(),
                    field_type: String::new(),
                    default,
                })
            } else {
                None
            }
        })
        .collect()
}