-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::ffi::OsStr; | ||
use std::path::Path; | ||
use std::process::{Command, ExitStatus, Output}; | ||
|
||
use crate::Context; | ||
|
||
/// 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, ctx: &Context) -> CommandOutput { | ||
let output = self.run_maybe(ctx); | ||
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, _ctx: &Context) -> CommandOutput { | ||
let output = self.cmd.output().expect("Cannot execute process"); | ||
output.into() | ||
} | ||
|
||
/// Runs the command, ensuring that it has succeeded, and returns its stdout. | ||
#[track_caller] | ||
pub fn run_output(&mut self, ctx: &Context) -> String { | ||
self.run(ctx).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 } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::utils::cmd::{cmd, TrackedCommand}; | ||
use std::path::Path; | ||
|
||
/// Command that checks whether git works at the given `directory`. | ||
pub fn cmd_works(directory: &Path) -> TrackedCommand { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cmd_git(Some(directory)).arg("rev-parse") | ||
} | ||
|
||
/// Command that finds the currently checked out SHA at the given `directory`. | ||
pub fn cmd_get_current_sha(directory: &Path) -> TrackedCommand { | ||
cmd_git(Some(directory)).arg("rev-parse").arg("HEAD") | ||
} | ||
|
||
pub fn cmd_git(directory: Option<&Path>) -> TrackedCommand { | ||
let mut cmd = cmd("git"); | ||
if let Some(directory) = directory { | ||
cmd = cmd.current_dir(directory); | ||
} | ||
cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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