Skip to content

Commit a331ba6

Browse files
committed
Add BootstrapCommand and run_cmd
1 parent cdddcd3 commit a331ba6

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/bootstrap/exec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::process::Command;
2+
3+
/// Wrapper around `std::process::Command`.
4+
#[derive(Debug)]
5+
pub struct BootstrapCommand<'a> {
6+
pub command: &'a mut Command,
7+
/// Report failure later instead of immediately.
8+
pub delay_failure: bool,
9+
}
10+
11+
impl<'a> BootstrapCommand<'a> {
12+
pub fn delay_failure(self) -> Self {
13+
Self { delay_failure: true, ..self }
14+
}
15+
}
16+
17+
impl<'a> From<&'a mut Command> for BootstrapCommand<'a> {
18+
fn from(command: &'a mut Command) -> Self {
19+
Self { command, delay_failure: false }
20+
}
21+
}

src/bootstrap/lib.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod config;
5050
mod dist;
5151
mod doc;
5252
mod download;
53+
mod exec;
5354
mod flags;
5455
mod format;
5556
mod install;
@@ -90,6 +91,7 @@ mod job {
9091
pub use crate::builder::PathSet;
9192
use crate::cache::{Interned, INTERNER};
9293
pub use crate::config::Config;
94+
use crate::exec::BootstrapCommand;
9395
pub use crate::flags::Subcommand;
9496
use termcolor::{ColorChoice, StandardStream, WriteColor};
9597

@@ -1003,17 +1005,32 @@ impl Build {
10031005

10041006
/// Runs a command, printing out contextual info if it fails, and delaying errors until the build finishes.
10051007
pub(crate) fn run_delaying_failure(&self, cmd: &mut Command) -> bool {
1006-
if !self.fail_fast {
1007-
#[allow(deprecated)] // can't use Build::try_run, that's us
1008-
if self.config.try_run(cmd).is_err() {
1009-
let mut failures = self.delayed_failures.borrow_mut();
1010-
failures.push(format!("{cmd:?}"));
1011-
return false;
1008+
let cmd: BootstrapCommand<'_> = cmd.into();
1009+
self.run_cmd(cmd.delay_failure())
1010+
}
1011+
1012+
/// A centralized function for running commands that do not return output.
1013+
pub(crate) fn run_cmd<'a, C: Into<BootstrapCommand<'a>>>(&self, cmd: C) -> bool {
1014+
let command = cmd.into();
1015+
self.verbose(&format!("running: {command:?}"));
1016+
1017+
#[allow(deprecated)] // can't use Build::try_run, that's us
1018+
let result = self.config.try_run(command.command);
1019+
1020+
match result {
1021+
Ok(_) => true,
1022+
Err(_) => {
1023+
if command.delay_failure {
1024+
let mut failures = self.delayed_failures.borrow_mut();
1025+
failures.push(format!("{command:?}"));
1026+
return false;
1027+
}
1028+
if self.fail_fast {
1029+
exit!(1);
1030+
}
1031+
false
10121032
}
1013-
} else {
1014-
self.run(cmd);
10151033
}
1016-
true
10171034
}
10181035

10191036
pub fn is_verbose_than(&self, level: usize) -> bool {

0 commit comments

Comments
 (0)