1use std::process::Stdio;
2
3use cargo_metadata::{CargoOpt, MetadataCommand};
4use color_eyre::{eyre::bail, Result};
5
6pub fn cargo_build(bin: &Option<String>) -> Result<()> {
7 let mut command = std::process::Command::new("cargo");
8 command.args(["build", "--release"]);
9
10 if let Some(x) = bin {
11 command.args(["--bin", &x.clone()]);
12 }
13
14 let res = command.stderr(Stdio::inherit()).output()?;
15 if !res.status.success() {
16 bail!(
17 "Cargo could not build the project: {}",
18 String::from_utf8(res.stderr)?,
19 )
20 }
21 Ok(())
22}
23
24pub fn get_bin() -> Result<String> {
26 let metadata = MetadataCommand::new()
27 .manifest_path("./Cargo.toml")
28 .features(CargoOpt::AllFeatures)
29 .no_deps()
30 .exec()?;
31
32 let mut targets = Vec::new();
33 for package in metadata.packages {
34 targets.push(package.targets);
35 }
36
37 let targets: Vec<_> = targets
38 .iter()
39 .flatten()
40 .filter(|x| !x.src_path.clone().into_string().contains(".cargo/registry"))
41 .filter(|x| x.kind.contains(&"bin".to_string()))
42 .collect();
43
44 Ok(format!(
45 "target/release/{}",
46 targets.get(0).expect("no target found").name.clone()
47 ))
48}