use crate::cmd::run_cmd_directly;
use crate::errors::*;
use crate::parse::Opts;
use std::path::PathBuf;
use std::process::Command;
pub fn check_env(global_opts: &Opts) -> Result<(), Error> {
#[cfg(unix)]
let shell_exec = "sh";
#[cfg(windows)]
let shell_exec = "powershell";
#[cfg(unix)]
let shell_param = "-c";
#[cfg(windows)]
let shell_param = "-command";
let cargo_cmd = global_opts.cargo_engine_path.to_string() + " --version";
let cargo_res = Command::new(shell_exec)
.args([shell_param, &cargo_cmd])
.output()
.map_err(|err| Error::CargoNotPresent { source: err })?;
let exit_code = match cargo_res.status.code() {
Some(exit_code) => exit_code,
None if cargo_res.status.success() => 0,
None => 1,
};
if exit_code != 0 {
return Err(Error::CargoNotPresent {
source: std::io::Error::new(std::io::ErrorKind::NotFound, "non-zero exit code"),
});
}
let rustup_cmd = global_opts.rustup_path.to_string() + " target list";
let rustup_res = Command::new(shell_exec)
.args([shell_param, &rustup_cmd])
.output();
if let Ok(rustup_res) = rustup_res {
let exit_code = match rustup_res.status.code() {
Some(exit_code) => exit_code,
None if rustup_res.status.success() => 0,
None => 1,
};
if exit_code == 0 {
let stdout = String::from_utf8_lossy(&rustup_res.stdout);
let has_wasm_target = stdout.contains("wasm32-unknown-unknown (installed)");
if !has_wasm_target {
let exit_code = run_cmd_directly(
"rustup target add wasm32-unknown-unknown".to_string(),
&PathBuf::from("."),
vec![],
)?;
if exit_code != 0 {
return Err(Error::RustupTargetAddFailed { code: exit_code });
}
}
}
}
Ok(())
}