Skip to content

Commit

Permalink
Implement run_cmd in terms of run_tracked
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Jun 20, 2024
1 parent 0de7b92 commit 5c4318d
Showing 1 changed file with 1 addition and 72 deletions.
73 changes: 1 addition & 72 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,79 +1034,8 @@ impl Build {

/// A centralized function for running commands that do not return output.
pub(crate) fn run_cmd<'a, C: Into<BootstrapCommand<'a>>>(&self, cmd: C) -> bool {
if self.config.dry_run() {
return true;
}

let command = cmd.into();
self.verbose(|| println!("running: {command:?}"));

let output_mode = command.output_mode.unwrap_or_else(|| match self.is_verbose() {
true => OutputMode::PrintAll,
false => OutputMode::PrintOutput,
});
let (output, print_error) = match output_mode {
mode @ (OutputMode::PrintAll | OutputMode::PrintOutput) => (
command.command.status().map(|status| Output {
status,
stdout: Vec::new(),
stderr: Vec::new(),
}),
matches!(mode, OutputMode::PrintAll),
),
OutputMode::PrintOnFailure => (command.command.output(), true),
};

let output = match output {
Ok(output) => output,
Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", command, e)),
};
let result = if !output.status.success() {
if print_error {
println!(
"\n\nCommand did not execute successfully.\
\nExpected success, got: {}",
output.status,
);

if !self.is_verbose() {
println!("Add `-v` to see more details.\n");
}

self.verbose(|| {
println!(
"\nSTDOUT ----\n{}\n\
STDERR ----\n{}\n",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
)
});
}
Err(())
} else {
Ok(())
};

match result {
Ok(_) => true,
Err(_) => {
match command.failure_behavior {
BehaviorOnFailure::DelayFail => {
if self.fail_fast {
exit!(1);
}

let mut failures = self.delayed_failures.borrow_mut();
failures.push(format!("{command:?}"));
}
BehaviorOnFailure::Exit => {
exit!(1);
}
BehaviorOnFailure::Ignore => {}
}
false
}
}
self.run_tracked(command).is_success()
}

/// Check if verbosity is greater than the `level`
Expand Down

0 comments on commit 5c4318d

Please sign in to comment.