homeboy 0.124.11

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
mod default_pattern_for_file;
mod types;
mod version;

pub use default_pattern_for_file::*;
pub use types::*;
pub use version::*;

use crate::component::{self, Component, VersionTarget};
use crate::config::{from_str, set_json_pointer, to_string_pretty};
use crate::engine::hooks::{self, HookFailureMode};
use crate::engine::local_files::{self, FileSystem};
use crate::engine::text;
use crate::error::{Error, Result};
use crate::extension::ExtensionManifest;
use crate::release::changelog;
use serde_json::Value;
use std::path::Path;

pub(crate) fn replace_versions(
    content: &str,
    pattern: &str,
    new_version: &str,
) -> Option<(String, usize)> {
    text::replace_all(content, pattern, new_version)
}

fn find_version_pattern_in_extension(
    extension: &ExtensionManifest,
    filename: &str,
) -> Option<String> {
    for vp in extension.version_patterns() {
        if filename.ends_with(&vp.extension) {
            return Some(vp.pattern.clone());
        }
    }
    None
}

/// Update version in a file, handling both JSON and text-based version files.
/// Returns the number of replacements made.
pub(crate) fn update_version_in_file(
    path: &str,
    pattern: &str,
    old_version: &str,
    new_version: &str,
) -> Result<usize> {
    // JSON files with default pattern use structured update
    if Path::new(path).extension().is_some_and(|ext| ext == "json")
        && default_pattern_for_file(path).as_deref() == Some(pattern)
    {
        let content = local_files::local().read(Path::new(path))?;
        let mut json: Value = from_str(&content)?;
        let Some(current) = json.get("version").and_then(|v: &Value| v.as_str()) else {
            return Err(Error::config_missing_key("version", Some(path.to_string())));
        };

        if current != old_version {
            return Err(Error::internal_unexpected(format!(
                "Version mismatch in {}: found {}, expected {}",
                path, current, old_version
            )));
        }

        set_json_pointer(
            &mut json,
            "/version",
            serde_json::Value::String(new_version.to_string()),
        )?;
        let output = to_string_pretty(&json)?;
        local_files::local().write(Path::new(path), &output)?;
        return Ok(1);
    }

    // Text files use regex replacement
    let content = local_files::read_file(Path::new(path), "read version file")?;

    let versions = parse_versions(&content, pattern).ok_or_else(|| {
        Error::validation_invalid_argument(
            "versionPattern",
            format!("Invalid version regex pattern '{}'", pattern),
            None,
            Some(vec![pattern.to_string()]),
        )
    })?;

    if versions.is_empty() {
        return Err(Error::internal_unexpected(format!(
            "Could not find version in {}",
            path
        )));
    }

    // Validate all found versions match expected
    for v in &versions {
        if v != old_version {
            return Err(Error::internal_unexpected(format!(
                "Version mismatch in {}: found {}, expected {}",
                path, v, old_version
            )));
        }
    }

    let (new_content, replaced_count) = replace_versions(&content, pattern, new_version)
        .ok_or_else(|| {
            Error::validation_invalid_argument(
                "versionPattern",
                format!("Invalid version regex pattern '{}'", pattern),
                None,
                Some(vec![pattern.to_string()]),
            )
        })?;

    local_files::write_file(Path::new(path), &new_content, "write version file")?;

    Ok(replaced_count)
}

/// Read version from a local file for a component's version target.
/// Returns None if file doesn't exist or version can't be parsed.
pub(crate) fn read_local_version(
    local_path: &str,
    version_target: &VersionTarget,
) -> Option<String> {
    let path = resolve_version_file_path(local_path, &version_target.file);
    let content = local_files::local().read(Path::new(&path)).ok()?;

    let pattern: String = version_target
        .pattern
        .clone()
        .or_else(|| default_pattern_for_file(&version_target.file))?;

    parse_version(&content, &pattern)
}

/// Pre-validate all version targets match the expected version.
/// This is a read-only operation that ensures all targets are in sync
/// BEFORE any file modifications (like changelog finalization) occur.
fn pre_validate_version_targets(
    targets: &[VersionTarget],
    local_path: &str,
    expected_version: &str,
) -> Result<Vec<VersionTargetInfo>> {
    let mut target_infos = Vec::new();

    for target in targets {
        let version_pattern = resolve_target_pattern(target)?;
        let full_path = resolve_version_file_path(local_path, &target.file);
        let content = local_files::local().read(Path::new(&full_path))?;

        let versions = parse_versions(&content, &version_pattern).ok_or_else(|| {
            Error::validation_invalid_argument(
                "versionPattern",
                format!("Invalid version regex pattern '{}'", version_pattern),
                None,
                Some(vec![version_pattern.clone()]),
            )
        })?;

        if versions.is_empty() {
            return Err(Error::internal_unexpected(format!(
                "Could not find version in {}",
                target.file
            )));
        }

        // Validate all versions in this file match expected
        let found = text::require_identical(&versions, &target.file)?;
        if found != expected_version {
            return Err(Error::validation_invalid_argument(
                "version",
                format!(
                    "Version mismatch in {}: found {}, expected {}",
                    target.file, found, expected_version
                ),
                None,
                Some(vec![
                    format!(
                        "All version targets must be at {} before release proceeds.",
                        expected_version
                    ),
                    format!(
                        "Update {} from {} to {}, then re-run `homeboy release`.",
                        target.file, found, expected_version
                    ),
                ]),
            ));
        }

        target_infos.push(VersionTargetInfo {
            file: target.file.clone(),
            pattern: version_pattern,
            full_path,
            match_count: versions.len(),
            warning: None,
        });
    }

    Ok(target_infos)
}

/// Validate and finalize changelog for a version operation.
/// Ensures changelog is in sync with current version and has valid unreleased content.
///
/// When `generated_entries` is provided (from the release pipeline's commit analysis),
/// entries are generated and finalized into a versioned section in a single disk write —
/// no intermediate `## Unreleased` section is ever persisted.
///
/// When `generated_entries` is None, falls back to finalizing an existing
/// `## Unreleased` section for internal recovery paths.
pub(crate) fn validate_and_finalize_changelog(
    component: &Component,
    current_version: &str,
    new_version: &str,
    generated_entries: Option<&std::collections::HashMap<String, Vec<String>>>,
) -> Result<ChangelogValidationResult> {
    let settings = changelog::resolve_effective_settings(Some(component));
    let changelog_path = changelog::resolve_changelog_path(component)?;

    // resolve_changelog_path() already handles fallback discovery when the
    // configured path doesn't exist, so the path here should be valid.
    let changelog_content = match local_files::local().read(&changelog_path) {
        Ok(content) => content,
        Err(e) => {
            let error_str = e.to_string();
            if error_str.contains("File not found") || error_str.contains("No such file") {
                return Err(Error::validation_invalid_argument(
                    "changelog",
                    format!("Changelog file not found: {}", changelog_path.display()),
                    None,
                    Some(vec![format!(
                        "Configure the component's changelog target, then re-run release:\n  homeboy component set {} --changelog-target \"CHANGELOG.md\"",
                        component.id
                    )]),
                ));
            }
            return Err(e);
        }
    };

    // Bootstrap case: a fresh changelog with no finalized versions is a
    // legitimate first-release state, not an error. The finalize step below
    // writes the first version section. See #1172.
    let latest_changelog_version = changelog::get_latest_finalized_version(&changelog_content);

    // Reject if changelog is ahead of files (version gap). Skipped on the
    // bootstrap case (no prior finalized version).
    if let Some(ref prev) = latest_changelog_version {
        let changelog_ver = semver::Version::parse(prev);
        let file_ver = semver::Version::parse(current_version);
        if let (Ok(clv), Ok(fv)) = (changelog_ver, file_ver) {
            if clv > fv {
                return Err(Error::validation_invalid_argument(
                    "version",
                    format!(
                        "Version mismatch: changelog is at {} but files are at {}. Setting version would create a version gap.",
                        prev, current_version
                    ),
                    None,
                    Some(vec![
                        format!("The changelog has a finalized section for {} but the version files are still at {}.", prev, current_version),
                        "This usually means a previous release was partially prepared.".to_string(),
                        String::new(),
                        "To resolve:".to_string(),
                        format!("  1. Update all version_targets to {} (to match the changelog), commit, and re-run", prev),
                        format!("  2. Or revert the changelog {} section and re-run to let homeboy regenerate it", prev),
                    ]),
                ));
            }
        }
    }

    let (finalized_changelog, changelog_changed) = if let Some(entries) = generated_entries {
        // Atomic path: generate entries and finalize into versioned section in one pass.
        // No ## Unreleased section is ever written to disk.
        let entries_ref: std::collections::HashMap<&str, Vec<String>> = entries
            .iter()
            .map(|(k, v)| (k.as_str(), v.clone()))
            .collect();
        changelog::finalize_with_generated_entries(
            &changelog_content,
            &settings.next_section_aliases,
            &entries_ref,
            new_version,
        )?
    } else {
        // Legacy path: finalize an existing ## Unreleased section.
        changelog::finalize_next_section(
            &changelog_content,
            &settings.next_section_aliases,
            new_version,
            false,
        )?
    };

    if changelog_changed {
        local_files::local().write(&changelog_path, &finalized_changelog)?;
    } else if component.changelog_target.is_some() {
        let changelog_file = component
            .changelog_target
            .as_deref()
            .unwrap_or("CHANGELOG.md");
        let version_targets_list: Vec<String> = component
            .version_targets
            .as_ref()
            .map(|targets| targets.iter().map(|t| format!("  - {}", t.file)).collect())
            .unwrap_or_default();

        return Err(Error::validation_invalid_argument(
            "changelog",
            format!(
                "Configured changelog target '{}' was not updated for release {}",
                changelog_file, new_version
            ),
            None,
            Some(vec![
                format!(
                    "Planned release: {} → {} (bump: {})",
                    current_version, new_version,
                    if current_version == new_version { "none" } else { "auto" }
                ),
                "Pre-flight contract — before running `homeboy release`, the target component must have:".to_string(),
                format!("  1. A `## [{}]` section at the top of {}", new_version, changelog_file),
                "  2. Every version_targets file updated to match".to_string(),
                "  3. All changes committed".to_string(),
                String::new(),
                "To preview the target version without running the pipeline:".to_string(),
                format!("  homeboy release {} --dry-run", component.id),
                String::new(),
                "Or let homeboy manage the changelog automatically:".to_string(),
                format!("  homeboy release {} (homeboy generates entries from conventional commits)", component.id),
                if !version_targets_list.is_empty() {
                    format!("Version target files:\n{}", version_targets_list.join("\n"))
                } else {
                    String::new()
                },
            ].into_iter().filter(|s| !s.is_empty()).collect()),
        ));
    }

    Ok(ChangelogValidationResult {
        changelog_path: changelog_path.to_string_lossy().to_string(),
        changelog_finalized: true,
        changelog_changed,
    })
}

pub fn validate_baseline_alignment(
    version: Option<&ComponentVersionSnapshot>,
    baseline_ref: Option<&str>,
) -> Option<String> {
    let version_snapshot = version?;
    let baseline = baseline_ref?;
    let baseline_version = baseline.strip_prefix('v').unwrap_or(baseline);

    if version_snapshot.version != baseline_version {
        Some(format!(
            "Version mismatch: source files show {} but git baseline is {}. Consider creating a tag or bumping the version.",
            version_snapshot.version, baseline
        ))
    } else {
        None
    }
}

/// Bump a component's version and finalize changelog.
/// bump_type: "patch", "minor", or "major"
pub(crate) fn bump_component_version(
    component: &Component,
    bump_type: &str,
    changelog_entries: Option<&std::collections::HashMap<String, Vec<String>>>,
) -> Result<BumpResult> {
    // Validate local_path is absolute and exists before any file operations
    component::validate_local_path(component)?;

    let targets = component
        .version_targets
        .as_ref()
        .ok_or_else(|| Error::config_missing_key("versionTargets", Some(component.id.clone())))?;

    if targets.is_empty() {
        return Err(Error::config_invalid_value(
            "versionTargets",
            None,
            format!("Component '{}' has empty versionTargets", component.id),
        ));
    }

    // Read current version from primary target
    let primary = &targets[0];
    let primary_pattern = resolve_target_pattern(primary)?;
    let primary_full_path = resolve_version_file_path(&component.local_path, &primary.file);

    let primary_content = local_files::local().read(Path::new(&primary_full_path))?;
    let primary_versions = parse_versions(&primary_content, &primary_pattern).ok_or_else(|| {
        Error::validation_invalid_argument(
            "versionPattern",
            format!("Invalid version regex pattern '{}'", primary_pattern),
            None,
            Some(vec![primary_pattern.clone()]),
        )
    })?;

    if primary_versions.is_empty() {
        return Err(build_version_parse_error(
            &primary.file,
            &primary_pattern,
            &primary_content,
        ));
    }

    let old_version = text::require_identical(&primary_versions, &primary.file)?;
    let new_version = increment_version(&old_version, bump_type).ok_or_else(|| {
        Error::validation_invalid_argument(
            "version",
            format!("Invalid version format: {}", old_version),
            None,
            Some(vec![old_version.clone()]),
        )
    })?;

    // Pre-validate ALL version targets BEFORE any file modifications.
    // This prevents changelog finalization when version files are out of sync.
    let target_infos = pre_validate_version_targets(targets, &component.local_path, &old_version)?;

    // Now safe to finalize changelog - all targets validated
    let changelog_validation =
        validate_and_finalize_changelog(component, &old_version, &new_version, changelog_entries)?;

    // Update all version files (validation already done, just write new versions)
    for info in &target_infos {
        let replaced_count =
            update_version_in_file(&info.full_path, &info.pattern, &old_version, &new_version)?;

        if replaced_count != info.match_count {
            return Err(Error::internal_unexpected(format!(
                "Unexpected replacement count in {}",
                info.file
            )));
        }
    }

    // If any version target is Cargo.toml, regenerate Cargo.lock so it stays in sync.
    // Without this, the release commit only includes Cargo.toml and post-release hooks
    // like `cargo publish` fail because Cargo.lock is dirty.
    let has_cargo_target = target_infos.iter().any(|t| t.file.ends_with("Cargo.toml"));
    if has_cargo_target {
        log_status!(
            "version",
            "Regenerating Cargo.lock after Cargo.toml version bump"
        );
        let lockfile_result = std::process::Command::new("cargo")
            .args(["generate-lockfile"])
            .current_dir(&component.local_path)
            .output();
        if let Err(e) = lockfile_result {
            log_status!("warning", "Failed to regenerate Cargo.lock: {}", e);
        }
    }

    // Replace @since placeholder tags with the new version (extension-driven).
    let since_tags_replaced = replace_since_tag_placeholders(component, &new_version)?;

    // Run lifecycle hooks that may update/stage generated artifacts impacted by the bump.
    // This must happen AFTER version targets are updated so artifacts match the new version.
    hooks::run_hooks(
        component,
        hooks::events::PRE_VERSION_BUMP,
        HookFailureMode::Fatal,
    )?;
    hooks::run_hooks(
        component,
        hooks::events::POST_VERSION_BUMP,
        HookFailureMode::Fatal,
    )?;

    Ok(BumpResult {
        old_version,
        new_version,
        targets: target_infos,
        since_tags_replaced,
        changelog_path: changelog_validation.changelog_path,
        changelog_finalized: changelog_validation.changelog_finalized,
        changelog_changed: changelog_validation.changelog_changed,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::component::Component;
    use regex::Regex;
    use std::fs;

    fn make_test_component(temp_dir: &tempfile::TempDir) -> Component {
        Component {
            id: "test-component".to_string(),
            local_path: temp_dir.path().to_string_lossy().to_string(),
            remote_path: String::new(),
            changelog_target: Some("CHANGELOG.md".to_string()),
            ..Default::default()
        }
    }

    #[test]
    fn since_tag_regex_matches_placeholders() {
        let pattern_str = format!(r"@since\s+({})", DEFAULT_SINCE_PLACEHOLDER);
        let regex = Regex::new(&pattern_str).unwrap();

        // Should match placeholders
        assert!(regex.is_match("@since 0.0.0"));
        assert!(regex.is_match("@since NEXT"));
        assert!(regex.is_match("@since TBD"));
        assert!(regex.is_match("@since TODO"));
        assert!(regex.is_match("@since UNRELEASED"));
        assert!(regex.is_match("@since x.x.x"));
        assert!(regex.is_match(" * @since TBD"));
        assert!(regex.is_match(" * @since  NEXT")); // extra space

        // Should NOT match real versions
        assert!(!regex.is_match("@since 1.2.3"));
        assert!(!regex.is_match("@since 0.1.0"));
    }

    #[test]
    fn since_tag_replacement_preserves_context() {
        let pattern_str = format!(r"@since\s+({})", DEFAULT_SINCE_PLACEHOLDER);
        let regex = Regex::new(&pattern_str).unwrap();

        let input = " * @since TBD\n * @since 1.0.0\n * @since NEXT\n";
        let result = regex.replace_all(input, |caps: &regex::Captures| {
            let full = caps.get(0).unwrap().as_str();
            let placeholder = caps.get(1).unwrap().as_str();
            full.replacen(placeholder, "2.0.0", 1)
        });

        assert_eq!(
            result,
            " * @since 2.0.0\n * @since 1.0.0\n * @since 2.0.0\n"
        );
    }

    #[test]
    fn validate_and_finalize_changelog_fails_when_configured_target_stays_stale() {
        let temp_dir = tempfile::tempdir().unwrap();
        let changelog_path = temp_dir.path().join("CHANGELOG.md");
        fs::write(
            &changelog_path,
            "# Changelog\n\n## [0.4.16] - 2026-03-01\n\n### Fixed\n- old entry\n",
        )
        .unwrap();

        let component = make_test_component(&temp_dir);
        validate_and_finalize_changelog(&component, "0.4.16", "0.4.17", None)
            .expect_err("configured stale changelog should block release");

        let unchanged = fs::read_to_string(&changelog_path).unwrap();
        assert!(unchanged.contains("## [0.4.16] - 2026-03-01"));
        assert!(!unchanged.contains("0.4.17"));
    }

    /// Bootstrap with generated entries (the release pipeline's common path):
    /// no prior finalized version on disk, entries coming from conventional
    /// commits. `validate_and_finalize_changelog` should write the first
    /// versioned section without complaining about the missing baseline.
    #[test]
    fn validate_and_finalize_changelog_bootstraps_first_release_with_generated_entries() {
        let temp_dir = tempfile::tempdir().unwrap();
        let changelog_path = temp_dir.path().join("CHANGELOG.md");
        // Minimal changelog produced by the pipeline's auto-init preflight.
        fs::write(&changelog_path, "# Changelog\n\n## Unreleased\n\n").unwrap();

        let component = make_test_component(&temp_dir);

        let mut entries: std::collections::HashMap<String, Vec<String>> =
            std::collections::HashMap::new();
        entries.insert(
            "added".to_string(),
            vec!["initial public release".to_string()],
        );

        let result = validate_and_finalize_changelog(&component, "0.1.0", "0.1.0", Some(&entries))
            .expect("bootstrap finalize should succeed");

        assert!(result.changelog_finalized);
        assert!(result.changelog_changed);

        let finalized = fs::read_to_string(&changelog_path).unwrap();
        assert!(
            finalized.contains("## [0.1.0]"),
            "expected first version section, got: {}",
            finalized
        );
    }
}