Skip to content

Add tracked command abstraction to bootstrap #126564

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
92 changes: 92 additions & 0 deletions src/bootstrap/src/utils/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::ffi::OsStr;
use std::path::Path;
use std::process::{Command, ExitStatus, Output};

/// Wrapper around `std::process::Command` whose execution will be eventually
/// tracked.
#[derive(Debug)]
pub struct TrackedCommand {
cmd: Command,
}

impl TrackedCommand {
pub fn new<P: AsRef<OsStr>>(program: P) -> Self {
Self { cmd: Command::new(program) }
}

pub fn current_dir<P: AsRef<Path>>(mut self, path: P) -> Self {
self.cmd.current_dir(path);
self
}

pub fn arg<A: AsRef<OsStr>>(mut self, arg: A) -> Self {
self.cmd.arg(arg);
self
}

/// Runs the command, ensuring that it has succeeded.
#[track_caller]
pub fn run(&mut self) -> CommandOutput {
let output = self.run_maybe();
if !output.is_success() {
panic!(
"`{:?}`\nwas supposed to succeed, but it failed with {}\nStdout: {}\nStderr: {}",
self.cmd,
output.status,
output.stdout(),
output.stderr()
)
}
output
}

/// Runs the command.
#[track_caller]
pub fn run_maybe(&mut self) -> CommandOutput {
let output = self.cmd.output().expect("Cannot execute process");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: it might be nice to report the complete command invocation that failed

output.into()
}

/// Runs the command, ensuring that it has succeeded, and returns its stdout.
#[track_caller]
pub fn run_output(&mut self) -> String {
self.run().stdout()
}
}

/// Creates a new tracked command.
pub fn cmd<P: AsRef<OsStr>>(program: P) -> TrackedCommand {
TrackedCommand::new(program)
}

/// Represents the output of an executed process.
#[allow(unused)]
pub struct CommandOutput {
status: ExitStatus,
stdout: Vec<u8>,
stderr: Vec<u8>,
}

impl CommandOutput {
pub fn is_success(&self) -> bool {
self.status.success()
}

pub fn is_failure(&self) -> bool {
!self.is_success()
}

pub fn stdout(&self) -> String {
String::from_utf8(self.stdout.clone()).expect("Cannot parse process stdout as UTF-8")
}

pub fn stderr(&self) -> String {
String::from_utf8(self.stderr.clone()).expect("Cannot parse process stderr as UTF-8")
}
}

impl From<Output> for CommandOutput {
fn from(output: Output) -> Self {
Self { status: output.status, stdout: output.stdout, stderr: output.stderr }
}
}
2 changes: 2 additions & 0 deletions src/bootstrap/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod cache;
pub(crate) mod cc_detect;
pub(crate) mod change_tracker;
pub(crate) mod channel;
pub(crate) mod cmd;
pub(crate) mod dylib;
pub(crate) mod exec;
pub(crate) mod helpers;
Expand All @@ -14,3 +15,4 @@ pub(crate) mod job;
pub(crate) mod metrics;
pub(crate) mod render_tests;
pub(crate) mod tarball;
mod git;