| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
use alloc::{ |
| 4 |
format, |
| 5 |
string::{String, ToString as _}, |
| 6 |
}; |
| 7 |
use core::{fmt, str}; |
| 8 |
use std::{ |
| 9 |
ffi::OsStr, |
| 10 |
process::{Command, ExitStatus, Output}, |
| 11 |
}; |
| 12 |
|
| 13 |
use crate::error::{Context as _, Error, Result}; |
| 14 |
|
| 15 |
macro_rules! cmd { |
| 16 |
($program:expr $(, $arg:expr)* $(,)?) => {{ |
| 17 |
let mut _cmd = std::process::Command::new($program); |
| 18 |
$( |
| 19 |
_cmd.arg($arg); |
| 20 |
)* |
| 21 |
$crate::process::ProcessBuilder::from_std(_cmd) |
| 22 |
}}; |
| 23 |
} |
| 24 |
|
| 25 |
// A builder for an external process, inspired by https://github.com/rust-lang/cargo/blob/0.47.0/src/cargo/util/process_builder.rs |
| 26 |
#[must_use] |
| 27 |
pub(crate) struct ProcessBuilder { |
| 28 |
cmd: Command, |
| 29 |
} |
| 30 |
|
| 31 |
impl ProcessBuilder { |
| 32 |
pub(crate) fn from_std(cmd: Command) -> Self { |
| 33 |
Self { cmd } |
| 34 |
} |
| 35 |
|
| 36 |
/// Adds an argument to pass to the program. |
| 37 |
pub(crate) fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self { |
| 38 |
self.cmd.arg(arg.as_ref()); |
| 39 |
self |
| 40 |
} |
| 41 |
|
| 42 |
/// Adds multiple arguments to pass to the program. |
| 43 |
pub(crate) fn args(&mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> &mut Self { |
| 44 |
self.cmd.args(args); |
| 45 |
self |
| 46 |
} |
| 47 |
|
| 48 |
/// Executes a process, captures its stdio output, returning the captured |
| 49 |
/// output, or an error if non-zero exit status. |
| 50 |
pub(crate) fn run_with_output(&mut self) -> Result<Output> { |
| 51 |
let output = self.cmd.output().with_context(|| { |
| 52 |
process_error(format!("could not execute process {self}"), None, None) |
| 53 |
})?; |
| 54 |
if output.status.success() { |
| 55 |
Ok(output) |
| 56 |
} else { |
| 57 |
Err(process_error( |
| 58 |
format!("process didn't exit successfully: {self}"), |
| 59 |
Some(output.status), |
| 60 |
Some(&output), |
| 61 |
)) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/// Executes a process, captures its stdio output, returning the captured |
| 66 |
/// standard output as a `String`. |
| 67 |
pub(crate) fn read(&mut self) -> Result<String> { |
| 68 |
let mut output = String::from_utf8(self.run_with_output()?.stdout) |
| 69 |
.with_context(|| format!("failed to parse output from {self}"))?; |
| 70 |
while output.ends_with('\n') || output.ends_with('\r') { |
| 71 |
output.pop(); |
| 72 |
} |
| 73 |
Ok(output) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Based on https://github.com/rust-lang/cargo/blob/0.47.0/src/cargo/util/process_builder.rs |
| 78 |
impl fmt::Display for ProcessBuilder { |
| 79 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 80 |
if !f.alternate() { |
| 81 |
f.write_str("`")?; |
| 82 |
} |
| 83 |
|
| 84 |
f.write_str(&self.cmd.get_program().to_string_lossy())?; |
| 85 |
|
| 86 |
for arg in self.cmd.get_args() { |
| 87 |
write!(f, " {}", arg.to_string_lossy())?; |
| 88 |
} |
| 89 |
|
| 90 |
if !f.alternate() { |
| 91 |
f.write_str("`")?; |
| 92 |
} |
| 93 |
|
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Based on https://github.com/rust-lang/cargo/blob/0.47.0/src/cargo/util/errors.rs |
| 99 |
/// Creates a new process error. |
| 100 |
/// |
| 101 |
/// `status` can be `None` if the process did not launch. |
| 102 |
/// `output` can be `None` if the process did not launch, or output was not captured. |
| 103 |
fn process_error(mut msg: String, status: Option<ExitStatus>, output: Option<&Output>) -> Error { |
| 104 |
match status { |
| 105 |
Some(s) => { |
| 106 |
msg.push_str(" ("); |
| 107 |
msg.push_str(&s.to_string()); |
| 108 |
msg.push(')'); |
| 109 |
} |
| 110 |
None => msg.push_str(" (never executed)"), |
| 111 |
} |
| 112 |
|
| 113 |
if let Some(out) = output { |
| 114 |
match str::from_utf8(&out.stdout) { |
| 115 |
Ok(s) if !s.trim_start().is_empty() => { |
| 116 |
msg.push_str("\n--- stdout\n"); |
| 117 |
msg.push_str(s); |
| 118 |
} |
| 119 |
Ok(_) | Err(_) => {} |
| 120 |
} |
| 121 |
match str::from_utf8(&out.stderr) { |
| 122 |
Ok(s) if !s.trim_start().is_empty() => { |
| 123 |
msg.push_str("\n--- stderr\n"); |
| 124 |
msg.push_str(s); |
| 125 |
} |
| 126 |
Ok(_) | Err(_) => {} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
Error::new(msg) |
| 131 |
} |