//! See <https://github.com/matklad/cargo-xtask/>.
//!
//! This binary defines various auxiliary build commands, which are not
//! expressible with just `cargo`. Notably, it provides tests via `cargo test -p xtask`
//! for code generation and `cargo xtask install` for installation of
//! rust-analyzer server and client.
//!
//! This binary is integrated into the `cargo` command line by using an alias in
//! `.cargo/config`.
mod build_py;
mod flags;
// mod vendor;

use std::env;
use std::path::{Path, PathBuf};

use anyhow::Result;
use xshell::Shell;

// mod cache;
mod msvcrt;

fn main() -> Result<()> {
    let mut sh = Shell::new()?;
    sh.change_dir(project_root());

    let flags = flags::Xtask::from_env()?;
    match flags.subcommand {
        flags::XtaskCmd::Verilogae(cmd) => cmd.run(&mut sh),
        flags::XtaskCmd::GenMsvcrt(cmd) => cmd.run(&sh),
    }
}

fn project_root() -> PathBuf {
    Path::new(
        &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
    )
    .ancestors()
    .nth(1)
    .unwrap()
    .to_path_buf()
}
