diff --git a/xbuild/src/cargo/mod.rs b/xbuild/src/cargo/mod.rs index be2cd71d..3ac11ea4 100644 --- a/xbuild/src/cargo/mod.rs +++ b/xbuild/src/cargo/mod.rs @@ -12,7 +12,7 @@ pub use artifact::{Artifact, CrateType}; use self::config::LocalizedConfig; use self::manifest::Manifest; -use crate::{CompileTarget, Opt}; +use crate::{task, CompileTarget, Opt}; pub struct Cargo { package: String, @@ -469,9 +469,7 @@ impl CargoBuild { self.cc_triple_env("CFLAGS", &self.c_flags.clone()); // These strings already end with a space if they're non-empty: self.cc_triple_env("CXXFLAGS", &format!("{}{}", self.c_flags, self.cxx_flags)); - if !self.cmd.status()?.success() { - std::process::exit(1); - } + task::run(&mut self.cmd)?; Ok(()) } } diff --git a/xbuild/src/download.rs b/xbuild/src/download.rs index 6cf31bd1..027f18f8 100644 --- a/xbuild/src/download.rs +++ b/xbuild/src/download.rs @@ -1,4 +1,4 @@ -use crate::{BuildEnv, Platform}; +use crate::{task, BuildEnv, Platform}; use anyhow::Result; use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; use mvn::Download; @@ -115,13 +115,7 @@ impl<'a> DownloadManager<'a> { } fn rustup_target(&self, target: &str) -> Result<()> { - let status = Command::new("rustup") - .arg("target") - .arg("add") - .arg(target) - .status()?; - anyhow::ensure!(status.success(), "failure running rustup target add"); - Ok(()) + task::run(Command::new("rustup").arg("target").arg("add").arg(target)) } pub fn prefetch(&self) -> Result<()> { diff --git a/xbuild/src/gradle/mod.rs b/xbuild/src/gradle/mod.rs index 013bbdca..0a38231b 100644 --- a/xbuild/src/gradle/mod.rs +++ b/xbuild/src/gradle/mod.rs @@ -157,14 +157,15 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> { let opt = env.target().opt(); let format = env.target().format(); - let mut cmd = Command::new("gradle"); - cmd.current_dir(&gradle); - cmd.arg(match format { - Format::Aab => "bundle", - Format::Apk => "assemble", - _ => unreachable!(), - }); - task::run(cmd, true)?; + task::run( + Command::new("gradle") + .current_dir(&gradle) + .arg(match format { + Format::Aab => "bundle", + Format::Apk => "assemble", + _ => unreachable!(), + }), + )?; let output = gradle .join("app") .join("build") diff --git a/xbuild/src/task.rs b/xbuild/src/task.rs index d67654df..84e5f026 100644 --- a/xbuild/src/task.rs +++ b/xbuild/src/task.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use console::{style, Term}; use std::process::Command; use std::time::Instant; @@ -66,37 +66,20 @@ impl TaskRunner { } } -pub fn run(mut command: Command, verbose: bool) -> Result<()> { - fn print_error(command: &Command, status: Option) { - let program = command.get_program().to_str().unwrap(); - let args = command - .get_args() - .map(|arg| arg.to_str().unwrap()) - .collect::>() - .join(" "); +pub fn run(command: &mut Command) -> Result<()> { + fn format_error(command: &Command, status: Option) -> String { let status = if let Some(code) = status { format!(" exited with {}", code) } else { Default::default() }; - println!("{} {} {} {}", style("[ERROR]").red(), program, args, status); + format!("{} `{:?}`{}", style("[ERROR]").red(), command, status) } - if !verbose { - let output = command.output()?; - if !output.status.success() { - print_error(&command, output.status.code()); - let stdout = std::str::from_utf8(&output.stdout)?; - print!("{}", stdout); - let stderr = std::str::from_utf8(&output.stderr)?; - print!("{}", stderr); - std::process::exit(1); - } - } else { - let status = command.status()?; - if !status.success() { - print_error(&command, status.code()); - std::process::exit(1); - } + let status = command + .status() + .with_context(|| format_error(command, None))?; + if !status.success() { + anyhow::bail!("{}", format_error(command, status.code())); } Ok(()) }