embeddenator-workspace 0.20.0

Workspace management utilities for embeddenator development
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
use clap::{Parser, Subcommand};
use colored::Colorize;
use embeddenator_workspace::{
    BumpType, HealthCheckType, HealthChecker, PatchManager, VersionManager,
};
use std::process::{Command, ExitCode};

#[derive(Parser)]
#[command(name = "embeddenator-workspace")]
#[command(about = "Workspace management utilities for embeddenator development")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate documentation (rustdoc + mdBook)
    Docs,
    /// Run rustdoc
    Rustdoc,
    /// Run mdBook
    Mdbook,
    /// Bump version across all packages
    BumpVersion {
        /// Bump major version (X.0.0)
        #[arg(long, group = "bump_type")]
        major: bool,
        /// Bump minor version (0.X.0)
        #[arg(long, group = "bump_type")]
        minor: bool,
        /// Bump patch version (0.0.X)
        #[arg(long, group = "bump_type")]
        patch: bool,
        /// Bump prerelease version (0.0.0-alpha.X)
        #[arg(long, group = "bump_type")]
        prerelease: bool,
        /// Show what would be changed without making changes
        #[arg(long)]
        dry_run: bool,
    },
    /// Check version consistency across packages
    CheckVersions {
        /// Show detailed information
        #[arg(long)]
        verbose: bool,
    },
    /// Apply local path patches for git dependencies
    PatchLocal {
        /// Workspace root directory (defaults to current directory)
        #[arg(long)]
        workspace_root: Option<String>,
        /// Verify patches with cargo metadata
        #[arg(long)]
        verify: bool,
    },
    /// Remove local path patches and restore git dependencies
    PatchReset {
        /// Workspace root directory (defaults to current directory)
        #[arg(long)]
        workspace_root: Option<String>,
        /// Clean cargo cache after removing patches
        #[arg(long)]
        clean: bool,
    },
    /// Check workspace health (git status, versions, tests, docs, specs)
    Health {
        /// Workspace root directory (defaults to current directory)
        #[arg(long)]
        workspace_root: Option<String>,
        /// Show detailed information
        #[arg(long)]
        verbose: bool,
        /// Output as JSON instead of terminal/markdown
        #[arg(long)]
        json: bool,
        /// Write markdown report to file
        #[arg(long)]
        output: Option<String>,
        /// Run specific checks only (git, version, tests, docs, specs)
        #[arg(long, value_delimiter = ',')]
        check: Vec<String>,
    },
}

fn main() -> ExitCode {
    let cli = Cli::parse();

    match cli.command {
        Commands::Docs => docs(),
        Commands::Health {
            workspace_root,
            verbose,
            json,
            output,
            check,
        } => health(workspace_root, verbose, json, output, check),
        Commands::Rustdoc => rustdoc(),
        Commands::Mdbook => mdbook(),
        Commands::BumpVersion {
            major,
            minor,
            patch,
            prerelease,
            dry_run,
        } => bump_version(major, minor, patch, prerelease, dry_run),
        Commands::CheckVersions { verbose } => check_versions(verbose),
        Commands::PatchLocal {
            workspace_root,
            verify,
        } => patch_local(workspace_root, verify),
        Commands::PatchReset {
            workspace_root,
            clean,
        } => patch_reset(workspace_root, clean),
    }
}

fn bump_version(
    major: bool,
    minor: bool,
    patch: bool,
    _prerelease: bool,
    dry_run: bool,
) -> ExitCode {
    // Determine bump type (default to prerelease if none specified)
    let bump_type = if major {
        BumpType::Major
    } else if minor {
        BumpType::Minor
    } else if patch {
        BumpType::Patch
    } else {
        BumpType::Prerelease
    };

    // Find workspace root (go up until we find update_all.sh or are at root)
    let workspace_root = std::env::current_dir().expect("Failed to get current directory");
    let workspace_root = find_workspace_root(&workspace_root).unwrap_or(workspace_root);

    let manager = VersionManager::new(&workspace_root);

    if dry_run {
        println!(
            "{}",
            "Dry run mode - no changes will be made".yellow().bold()
        );
    }

    println!(
        "{} {:?} version bump...",
        "Performing".cyan().bold(),
        bump_type
    );

    match manager.bump_versions(bump_type, dry_run) {
        Ok(changes) => {
            if changes.is_empty() {
                println!("{}", "No packages found to update".yellow());
                return ExitCode::from(1);
            }

            println!("\n{}", "Version Changes:".green().bold());
            for change in &changes {
                println!(
                    "  {} {} → {}",
                    change.package.bright_white().bold(),
                    change.old_version.to_string().red(),
                    change.new_version.to_string().green()
                );
            }

            if !dry_run {
                println!(
                    "\n{} {} package(s) updated",
                    "✓".green().bold(),
                    changes.len()
                );
                println!(
                    "\n{} git commit -am \"chore: bump version to {}\"",
                    "Next:".cyan().bold(),
                    changes[0].new_version
                );
            } else {
                println!(
                    "\n{} {} package(s) would be updated",
                    "Info:".blue().bold(),
                    changes.len()
                );
            }

            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            ExitCode::from(1)
        }
    }
}

fn check_versions(verbose: bool) -> ExitCode {
    let workspace_root = std::env::current_dir().expect("Failed to get current directory");
    let workspace_root = find_workspace_root(&workspace_root).unwrap_or(workspace_root);

    let manager = VersionManager::new(&workspace_root);

    println!("{}", "Checking version consistency...".cyan().bold());

    match manager.check_consistency() {
        Ok(report) => {
            println!(
                "\n{} {} package(s) scanned",
                "Scanned:".blue().bold(),
                report.total_packages
            );

            if report.has_issues() {
                println!("\n{}", "Issues Found:".red().bold());

                // Report version drift
                for issue in &report.issues {
                    println!("  {} {}", "•".red(), issue);
                }

                // Report dependency inconsistencies
                if !report.inconsistencies.is_empty() {
                    println!("\n{}", "Dependency Inconsistencies:".yellow().bold());
                    for inc in &report.inconsistencies {
                        println!(
                            "  {} {} depends on {} {} (expected: {})",
                            "•".yellow(),
                            inc.package.bright_white(),
                            inc.dependency,
                            inc.found.to_string().red(),
                            inc.expected.to_string().green()
                        );
                    }
                }

                println!(
                    "\n{} Run 'embeddenator-workspace bump-version --prerelease' to fix",
                    "Suggestion:".cyan().bold()
                );

                ExitCode::from(1)
            } else {
                println!("\n{} All versions are consistent!", "✓".green().bold());

                if verbose {
                    println!("\n{}", "Package Versions:".blue().bold());
                    // This would require additional data from the report
                    // For now, just show success
                }

                ExitCode::SUCCESS
            }
        }
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            ExitCode::from(1)
        }
    }
}

fn find_workspace_root(start: &std::path::Path) -> Option<std::path::PathBuf> {
    let mut current = start.to_path_buf();
    loop {
        if current.join("update_all.sh").exists() || current.join("embeddenator").is_dir() {
            return Some(current);
        }
        if !current.pop() {
            return None;
        }
    }
}

fn run(cmd: &mut Command) -> ExitCode {
    match cmd.status() {
        Ok(st) if st.success() => ExitCode::SUCCESS,
        Ok(st) => ExitCode::from(st.code().unwrap_or(1) as u8),
        Err(e) => {
            eprintln!("Failed to run command: {e}");
            ExitCode::from(1)
        }
    }
}

fn rustdoc() -> ExitCode {
    let mut cmd = Command::new("bash");
    cmd.arg("./generate_docs.sh");
    run(&mut cmd)
}

fn mdbook() -> ExitCode {
    let mut cmd = Command::new("bash");
    cmd.arg("./scripts/docs/build_mdbook.sh");
    run(&mut cmd)
}

fn docs() -> ExitCode {
    let rc = rustdoc();
    if rc != ExitCode::SUCCESS {
        return rc;
    }

    // mdBook is optional; if not installed, the script exits nonzero. Treat that as non-fatal.
    let mut cmd = Command::new("bash");
    cmd.arg("./scripts/docs/build_mdbook.sh");
    match cmd.status() {
        Ok(st) if st.success() => ExitCode::SUCCESS,
        Ok(_) => {
            eprintln!("Note: mdBook not built (mdbook not installed?)");
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("Note: mdBook not built: {e}");
            ExitCode::SUCCESS
        }
    }
}

fn patch_local(workspace_root: Option<String>, verify: bool) -> ExitCode {
    let workspace_root = resolve_workspace_root(workspace_root);

    println!(
        "{} Scanning for patchable dependencies in {}...",
        "Discovering:".cyan().bold(),
        workspace_root.display().to_string().bright_white()
    );

    let manager = PatchManager::new(&workspace_root);

    match manager.discover_patchable_dependencies() {
        Ok(deps) => {
            if deps.is_empty() {
                println!(
                    "{} No git dependencies with local equivalents found",
                    "Info:".blue().bold()
                );
                return ExitCode::SUCCESS;
            }

            println!(
                "\n{} Found {} patchable dependencies:",
                "Discovered:".green().bold(),
                deps.len()
            );

            for dep in &deps {
                println!(
                    "  {} {} → {}",
                    "•".green(),
                    dep.name.bright_white().bold(),
                    dep.local_path.display().to_string().dimmed()
                );
            }

            println!(
                "\n{} Applying patches to .cargo/config.toml...",
                "Patching:".cyan().bold()
            );

            match manager.apply_patches(&deps, verify) {
                Ok(report) => {
                    report.print();

                    if report.verification_error.is_some() {
                        ExitCode::from(1)
                    } else {
                        println!(
                            "\n{} Local development mode enabled!",
                            "Success:".green().bold()
                        );
                        println!(
                            "{} Run 'embeddenator-workspace patch-reset' to restore git dependencies",
                            "Note:".cyan().bold()
                        );
                        ExitCode::SUCCESS
                    }
                }
                Err(e) => {
                    eprintln!("{} {}", "Error:".red().bold(), e);
                    ExitCode::from(1)
                }
            }
        }
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            ExitCode::from(1)
        }
    }
}

fn patch_reset(workspace_root: Option<String>, clean: bool) -> ExitCode {
    let workspace_root = resolve_workspace_root(workspace_root);

    println!(
        "{} Removing patches from {}...",
        "Resetting:".cyan().bold(),
        workspace_root.display().to_string().bright_white()
    );

    let manager = PatchManager::new(&workspace_root);

    match manager.remove_patches() {
        Ok(report) => {
            report.print();

            if clean && report.removed_count > 0 {
                match manager.clean_cache() {
                    Ok(_) => {
                        println!("{} Cargo cache cleaned", "✓".green().bold());
                    }
                    Err(e) => {
                        eprintln!(
                            "{} Failed to clean cache: {}",
                            "Warning:".yellow().bold(),
                            e
                        );
                    }
                }
            }

            if report.removed_count > 0 {
                println!("\n{} Git dependencies restored!", "Success:".green().bold());
            }

            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            ExitCode::from(1)
        }
    }
}

fn resolve_workspace_root(workspace_root: Option<String>) -> std::path::PathBuf {
    workspace_root
        .map(std::path::PathBuf::from)
        .or_else(|| {
            let current = std::env::current_dir().ok()?;
            find_workspace_root(&current)
        })
        .unwrap_or_else(|| std::env::current_dir().expect("Failed to get current directory"))
}

fn health(
    workspace_root: Option<String>,
    verbose: bool,
    json: bool,
    output: Option<String>,
    check: Vec<String>,
) -> ExitCode {
    let workspace_root = resolve_workspace_root(workspace_root);

    println!(
        "{} Checking workspace health in {}...",
        "Analyzing:".cyan().bold(),
        workspace_root.display().to_string().bright_white()
    );

    let checker = HealthChecker::new(&workspace_root);

    // Parse check types
    let check_types = if check.is_empty() {
        // Run all checks
        vec![
            HealthCheckType::Git,
            HealthCheckType::Version,
            HealthCheckType::Tests,
            HealthCheckType::Docs,
            HealthCheckType::Specs,
        ]
    } else {
        let mut types = Vec::new();
        for check_str in &check {
            match check_str.parse::<HealthCheckType>() {
                Ok(t) => types.push(t),
                Err(_) => {
                    eprintln!(
                        "{} Unknown check type: '{}'. Valid types: git, version, tests, docs, specs",
                        "Error:".red().bold(),
                        check_str
                    );
                    return ExitCode::from(1);
                }
            }
        }
        types
    };

    // Run checks asynchronously
    let runtime = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
    let report = match runtime.block_on(checker.check_selected(&check_types, verbose)) {
        Ok(report) => report,
        Err(e) => {
            eprintln!("{} {}", "Error:".red().bold(), e);
            return ExitCode::from(1);
        }
    };

    // Output results
    if json {
        match serde_json::to_string_pretty(&report) {
            Ok(json_output) => {
                println!("{}", json_output);
            }
            Err(e) => {
                eprintln!(
                    "{} Failed to serialize to JSON: {}",
                    "Error:".red().bold(),
                    e
                );
                return ExitCode::from(1);
            }
        }
    } else {
        report.print_terminal(verbose);
    }

    // Write markdown report if requested
    if let Some(output_path) = output {
        let markdown = report.to_markdown();
        match std::fs::write(&output_path, markdown) {
            Ok(_) => {
                println!(
                    "\n{} Report written to {}",
                    "Saved:".green().bold(),
                    output_path.bright_white()
                );
            }
            Err(e) => {
                eprintln!("{} Failed to write report: {}", "Error:".red().bold(), e);
                return ExitCode::from(1);
            }
        }
    }

    // Exit with appropriate code
    if report.has_failures() {
        ExitCode::from(1)
    } else {
        ExitCode::SUCCESS
    }
}