cargo-winapp 0.1.1

Cargo subcommand for Windows app development with winapp CLI
//! cargo winapp build - Build and apply debug identity

use crate::util;
use anyhow::{Context, Result};
use std::io::Write;
use std::process::Command;

pub fn run(cargo_args: Vec<String>, verbose: bool) -> Result<()> {
    crate::info!("[INFO] Building...");

    let info = util::load_project_info()?;

    // Build and get the executable path
    let build_result = util::build_and_get_exe(&info.manifest_dir, &cargo_args, verbose)?;
    let exe_path = &build_result.exe_path;

    crate::info!("[INFO] Applying debug identity to {:?}...", exe_path);

    // Apply debug identity
    let mut cmd = Command::new("winapp");
    cmd.arg("create-debug-identity")
        .arg(exe_path)
        .current_dir(&info.manifest_dir);

    if verbose {
        cmd.arg("--verbose");
    }

    let status = cmd.status().context(
        "Failed to run winapp. Is it installed? Run: winget install microsoft.winappcli --source winget",
    )?;

    if !status.success() {
        anyhow::bail!("winapp create-debug-identity failed");
    }

    crate::info!("[INFO] Build complete with debug identity.");

    // Always output the path to stdout (machine-readable)
    let stdout = std::io::stdout();
    let mut handle = stdout.lock();
    writeln!(handle, "{}", exe_path.display())?;

    Ok(())
}