nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
Documentation
//! `nv doctor` — environment and SDK health check.

use std::path::PathBuf;
use std::process::Command as ProcessCommand;

use crate::build::probe::{format_tier3_doctor_section, SystemProbe};

pub(crate) fn probe_tool(cmd: &str, args: &[&str]) -> Option<String> {
    let out = ProcessCommand::new(cmd).args(args).output().ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&out.stdout);
    let line = text.lines().next().unwrap_or("ok").trim().to_string();
    Some(if line.is_empty() { "ok".into() } else { line })
}

pub(crate) fn cmd_doctor() {
    println!("Native View Engine — Environment Doctor\n");
    let tools: &[(&str, &[&str])] = &[
        ("rustc", &["--version"]),
        ("cargo", &["--version"]),
        ("kache", &["--version"]),
        ("clang++", &["--version"]),
        ("cmake", &["--version"]),
        ("ninja", &["--version"]),
        ("ccache", &["--version"]),
        ("ld64.lld", &["--version"]),
        ("xcodebuild", &["-version"]),
        ("swift", &["--version"]),
        ("flutter", &["--version"]),
        ("adb", &["version"]),
        ("wasm-bindgen", &["--version"]),
    ];

    for (name, args) in tools {
        match probe_tool(name, args) {
            Some(ver) => println!("  [OK]   {name:<14} {ver}"),
            None => println!("  [MISS] {name:<14} not found in PATH"),
        }
    }

    if let Ok(out) = ProcessCommand::new("rustup")
        .args(["target", "list", "--installed"])
        .output()
    {
        let targets: Vec<String> = String::from_utf8_lossy(&out.stdout)
            .lines()
            .map(str::trim)
            .filter(|l| !l.is_empty())
            .map(ToOwned::to_owned)
            .collect();
        println!(
            "\n  Installed Rust targets ({}): {}",
            targets.len(),
            targets.join(", ")
        );
    }

    print!("{}", format_tier3_doctor_section("nightly", &SystemProbe));

    match std::env::var("RN_REACT_COMMON_DIR") {
        Ok(raw) if !raw.trim().is_empty() => {
            let rn_path = PathBuf::from(raw.trim());
            if rn_path.exists() {
                println!("  [OK]   RN Common      {} (layout-yoga RN Yoga, camera JSI)", rn_path.display());
            } else {
                println!(
                    "  [WARN] RN Common      RN_REACT_COMMON_DIR does not exist: {}",
                    rn_path.display()
                );
            }
        }
        _ => {
            println!(
                "  [WARN] RN Common      RN_REACT_COMMON_DIR not set (optional: layout-yoga RN Yoga, camera JSI)"
            );
        }
    }
}

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

    #[test]
    fn probe_tool_detects_present_and_missing_binaries() {
        assert!(probe_tool("rustc", &["--version"]).is_some());
        assert!(probe_tool("__definitely_missing_nv_binary__", &["--version"]).is_none());
        assert!(probe_tool("rustc", &["--definitely-invalid-flag-xyz"]).is_none());
    }
}