Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit defd698

Browse files
committedJan 3, 2025
Improve task::run() handling and reuse in multiple places
1 parent 8636fc1 commit defd698

File tree

4 files changed

+30
-37
lines changed

4 files changed

+30
-37
lines changed
 

‎xbuild/src/cargo/mod.rs

+2-4
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,
@@ -469,9 +469,7 @@ impl CargoBuild {
469469
self.cc_triple_env("CFLAGS", &self.c_flags.clone());
470470
// These strings already end with a space if they're non-empty:
471471
self.cc_triple_env("CXXFLAGS", &format!("{}{}", self.c_flags, self.cxx_flags));
472-
if !self.cmd.status()?.success() {
473-
std::process::exit(1);
474-
}
472+
task::run(&mut self.cmd, true)?;
475473
Ok(())
476474
}
477475
}

‎xbuild/src/download.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::{BuildEnv, Platform};
2-
use anyhow::{Context as _, Result};
1+
use crate::{task, BuildEnv, Platform};
2+
use anyhow::Result;
33
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
44
use mvn::Download;
55
use reqwest::blocking::Client;
@@ -115,13 +115,10 @@ impl<'a> DownloadManager<'a> {
115115
}
116116

117117
fn rustup_target(&self, target: &str) -> Result<()> {
118-
let mut command = Command::new("rustup");
119-
command.arg("target").arg("add").arg(target);
120-
let status = command
121-
.status()
122-
.with_context(|| format!("Failed to run `{:?}`", command))?;
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

+10-8
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,16 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
157157

158158
let opt = env.target().opt();
159159
let format = env.target().format();
160-
let mut cmd = Command::new("gradle");
161-
cmd.current_dir(&gradle);
162-
cmd.arg(match format {
163-
Format::Aab => "bundle",
164-
Format::Apk => "assemble",
165-
_ => unreachable!(),
166-
});
167-
task::run(cmd, true)?;
160+
task::run(
161+
Command::new("gradle")
162+
.current_dir(&gradle)
163+
.arg(match format {
164+
Format::Aab => "bundle",
165+
Format::Apk => "assemble",
166+
_ => unreachable!(),
167+
}),
168+
true,
169+
)?;
168170
let output = gradle
169171
.join("app")
170172
.join("build")

‎xbuild/src/task.rs

+12-16
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)
Please sign in to comment.