Skip to content

Commit 6ccd0ea

Browse files
committed
Improve task::run() handling and reuse in multiple places
1 parent da5a36e commit 6ccd0ea

File tree

4 files changed

+29
-36
lines changed

4 files changed

+29
-36
lines changed

xbuild/src/cargo/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use artifact::{Artifact, CrateType};
1212

1313
use self::config::LocalizedConfig;
1414
use self::manifest::Manifest;
15-
use crate::{CompileTarget, Opt};
15+
use crate::{task, CompileTarget, Opt};
1616

1717
pub struct Cargo {
1818
package: String,
@@ -468,9 +468,7 @@ impl CargoBuild {
468468
self.cc_triple_env("CFLAGS", &self.c_flags.clone());
469469
// These strings already end with a space if they're non-empty:
470470
self.cc_triple_env("CXXFLAGS", &format!("{}{}", self.c_flags, self.cxx_flags));
471-
if !self.cmd.status()?.success() {
472-
std::process::exit(1);
473-
}
471+
task::run(&mut self.cmd, true)?;
474472
Ok(())
475473
}
476474
}

xbuild/src/download.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{BuildEnv, Platform};
1+
use crate::{task, BuildEnv, Platform};
22
use anyhow::Result;
33
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
44
use mvn::Download;
@@ -115,13 +115,10 @@ impl<'a> DownloadManager<'a> {
115115
}
116116

117117
fn rustup_target(&self, target: &str) -> Result<()> {
118-
let status = Command::new("rustup")
119-
.arg("target")
120-
.arg("add")
121-
.arg(target)
122-
.status()?;
123-
anyhow::ensure!(status.success(), "failure running rustup target add");
124-
Ok(())
118+
task::run(
119+
Command::new("rustup").arg("target").arg("add").arg(target),
120+
true,
121+
)
125122
}
126123

127124
pub fn prefetch(&self) -> Result<()> {

xbuild/src/gradle/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,16 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R
200200

201201
let opt = env.target().opt();
202202
let format = env.target().format();
203-
let mut cmd = Command::new("gradle");
204-
cmd.current_dir(&gradle);
205-
cmd.arg(match format {
206-
Format::Aab => "bundle",
207-
Format::Apk => "assemble",
208-
_ => unreachable!(),
209-
});
210-
task::run(cmd, true)?;
203+
task::run(
204+
Command::new("gradle")
205+
.current_dir(&gradle)
206+
.arg(match format {
207+
Format::Aab => "bundle",
208+
Format::Apk => "assemble",
209+
_ => unreachable!(),
210+
}),
211+
true,
212+
)?;
211213
let output = gradle
212214
.join("app")
213215
.join("build")

xbuild/src/task.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use console::{style, Term};
33
use std::process::Command;
44
use std::time::Instant;
@@ -66,36 +66,32 @@ impl TaskRunner {
6666
}
6767
}
6868

69-
pub fn run(mut command: Command, verbose: bool) -> Result<()> {
70-
fn print_error(command: &Command, status: Option<i32>) {
71-
let program = command.get_program().to_str().unwrap();
72-
let args = command
73-
.get_args()
74-
.map(|arg| arg.to_str().unwrap())
75-
.collect::<Vec<_>>()
76-
.join(" ");
69+
pub fn run(command: &mut Command, verbose: bool) -> Result<()> {
70+
fn format_error(command: &Command, status: Option<i32>) -> String {
7771
let status = if let Some(code) = status {
7872
format!(" exited with {code}")
7973
} else {
8074
Default::default()
8175
};
82-
println!("{} {} {} {}", style("[ERROR]").red(), program, args, status);
76+
format!("{} `{:?}`{}", style("[ERROR]").red(), command, status)
8377
}
8478
if !verbose {
85-
let output = command.output()?;
79+
let output = command
80+
.output()
81+
.with_context(|| format_error(command, None))?;
8682
if !output.status.success() {
87-
print_error(&command, output.status.code());
8883
let stdout = std::str::from_utf8(&output.stdout)?;
8984
print!("{stdout}");
9085
let stderr = std::str::from_utf8(&output.stderr)?;
9186
print!("{stderr}");
92-
std::process::exit(1);
87+
anyhow::bail!("{}", format_error(command, output.status.code()));
9388
}
9489
} else {
95-
let status = command.status()?;
90+
let status = command
91+
.status()
92+
.with_context(|| format_error(command, None))?;
9693
if !status.success() {
97-
print_error(&command, status.code());
98-
std::process::exit(1);
94+
anyhow::bail!("{}", format_error(command, status.code()));
9995
}
10096
}
10197
Ok(())

0 commit comments

Comments
 (0)