Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error and message handling when Commands fail #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions xbuild/src/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(())
}
}
Expand Down
10 changes: 2 additions & 8 deletions xbuild/src/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BuildEnv, Platform};
use crate::{task, BuildEnv, Platform};
use anyhow::Result;
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use mvn::Download;
Expand Down Expand Up @@ -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<()> {
Expand Down
17 changes: 9 additions & 8 deletions xbuild/src/gradle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 9 additions & 26 deletions xbuild/src/task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use console::{style, Term};
use std::process::Command;
use std::time::Instant;
Expand Down Expand Up @@ -66,37 +66,20 @@ impl TaskRunner {
}
}

pub fn run(mut command: Command, verbose: bool) -> Result<()> {
fn print_error(command: &Command, status: Option<i32>) {
let program = command.get_program().to_str().unwrap();
let args = command
.get_args()
.map(|arg| arg.to_str().unwrap())
.collect::<Vec<_>>()
.join(" ");
pub fn run(command: &mut Command) -> Result<()> {
fn format_error(command: &Command, status: Option<i32>) -> 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(())
}
Loading