Struct cargo_util::ProcessBuilder
source · pub struct ProcessBuilder {
program: OsString,
args: Vec<OsString>,
env: BTreeMap<String, Option<OsString>>,
cwd: Option<OsString>,
wrappers: Vec<OsString>,
jobserver: Option<Client>,
display_env_vars: bool,
retry_with_argfile: bool,
stdin: Option<Vec<u8>>,
}
Expand description
A builder object for an external process, similar to std::process::Command
.
Fields§
§program: OsString
The program to execute.
args: Vec<OsString>
A list of arguments to pass to the program.
env: BTreeMap<String, Option<OsString>>
Any environment variables that should be set for the program.
cwd: Option<OsString>
The directory to run the program from.
wrappers: Vec<OsString>
A list of wrappers that wrap the original program when calling
ProcessBuilder::wrapped
. The last one is the outermost one.
jobserver: Option<Client>
The make
jobserver. See the jobserver crate for
more information.
display_env_vars: bool
true
to include environment variable in display.
retry_with_argfile: bool
true
to retry with an argfile if hitting “command line too big” error.
See ProcessBuilder::retry_with_argfile
for more information.
stdin: Option<Vec<u8>>
Data to write to stdin.
Implementations§
source§impl ProcessBuilder
impl ProcessBuilder
sourcepub fn new<T: AsRef<OsStr>>(cmd: T) -> ProcessBuilder
pub fn new<T: AsRef<OsStr>>(cmd: T) -> ProcessBuilder
Creates a new ProcessBuilder
with the given executable path.
sourcepub fn program<T: AsRef<OsStr>>(&mut self, program: T) -> &mut ProcessBuilder
pub fn program<T: AsRef<OsStr>>(&mut self, program: T) -> &mut ProcessBuilder
(chainable) Sets the executable for the process.
sourcepub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut ProcessBuilder
pub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut ProcessBuilder
(chainable) Adds arg
to the args list.
sourcepub fn args<T: AsRef<OsStr>>(&mut self, args: &[T]) -> &mut ProcessBuilder
pub fn args<T: AsRef<OsStr>>(&mut self, args: &[T]) -> &mut ProcessBuilder
(chainable) Adds multiple args
to the args list.
sourcepub fn args_replace<T: AsRef<OsStr>>(
&mut self,
args: &[T]
) -> &mut ProcessBuilder
pub fn args_replace<T: AsRef<OsStr>>( &mut self, args: &[T] ) -> &mut ProcessBuilder
(chainable) Replaces the args list with the given args
.
sourcepub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut ProcessBuilder
pub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut ProcessBuilder
(chainable) Sets the current working directory of the process.
sourcepub fn env<T: AsRef<OsStr>>(&mut self, key: &str, val: T) -> &mut ProcessBuilder
pub fn env<T: AsRef<OsStr>>(&mut self, key: &str, val: T) -> &mut ProcessBuilder
(chainable) Sets an environment variable for the process.
sourcepub fn env_remove(&mut self, key: &str) -> &mut ProcessBuilder
pub fn env_remove(&mut self, key: &str) -> &mut ProcessBuilder
(chainable) Unsets an environment variable for the process.
sourcepub fn get_program(&self) -> &OsString
pub fn get_program(&self) -> &OsString
Gets the executable name.
sourcepub fn get_env(&self, var: &str) -> Option<OsString>
pub fn get_env(&self, var: &str) -> Option<OsString>
Gets an environment variable as the process will see it (will inherit from environment unless explicitally unset).
sourcepub fn get_envs(&self) -> &BTreeMap<String, Option<OsString>>
pub fn get_envs(&self) -> &BTreeMap<String, Option<OsString>>
Gets all environment variables explicitly set or unset for the process (not inherited vars).
sourcepub fn inherit_jobserver(&mut self, jobserver: &Client) -> &mut Self
pub fn inherit_jobserver(&mut self, jobserver: &Client) -> &mut Self
Sets the make
jobserver. See the jobserver crate for
more information.
sourcepub fn display_env_vars(&mut self) -> &mut Self
pub fn display_env_vars(&mut self) -> &mut Self
Enables environment variable display.
sourcepub fn retry_with_argfile(&mut self, enabled: bool) -> &mut Self
pub fn retry_with_argfile(&mut self, enabled: bool) -> &mut Self
Enables retrying with an argfile if hitting “command line too big” error
This is primarily for the @path
arg of rustc and rustdoc, which treat
each line as an command-line argument, so LF
and CRLF
bytes are not
valid as an argument for argfile at this moment.
For example, RUSTDOCFLAGS="--crate-version foo\nbar" cargo doc
is
valid when invoking from command-line but not from argfile.
To sum up, the limitations of the argfile are:
- Must be valid UTF-8 encoded.
- Must not contain any newlines in each argument.
Ref:
sourcepub fn stdin<T: Into<Vec<u8>>>(&mut self, stdin: T) -> &mut Self
pub fn stdin<T: Into<Vec<u8>>>(&mut self, stdin: T) -> &mut Self
Sets a value that will be written to stdin of the process on launch.
fn should_retry_with_argfile(&self, err: &Error) -> bool
sourcepub fn status(&self) -> Result<ExitStatus>
pub fn status(&self) -> Result<ExitStatus>
Like Command::status
but with a better error message.
fn _status(&self) -> Result<ExitStatus>
sourcepub fn exec(&self) -> Result<()>
pub fn exec(&self) -> Result<()>
Runs the process, waiting for completion, and mapping non-success exit codes to an error.
sourcepub fn exec_replace(&self) -> Result<()>
pub fn exec_replace(&self) -> Result<()>
Replaces the current process with the target process.
On Unix, this executes the process using the Unix syscall execvp
, which will block
this process, and will only return if there is an error.
On Windows this isn’t technically possible. Instead we emulate it to the best of our ability. One aspect we fix here is that we specify a handler for the Ctrl-C handler. In doing so (and by effectively ignoring it) we should emulate proxying Ctrl-C handling to the application at hand, which will either terminate or handle it itself. According to Microsoft’s documentation at https://docs.microsoft.com/en-us/windows/console/ctrl-c-and-ctrl-break-signals. the Ctrl-C signal is sent to all processes attached to a terminal, which should include our child process. If the child terminates then we’ll reap them in Cargo pretty quickly, and if the child handles the signal then we won’t terminate (and we shouldn’t!) until the process itself later exits.
sourcepub fn output(&self) -> Result<Output>
pub fn output(&self) -> Result<Output>
Like Command::output
but with a better error message.
fn _output(&self) -> Result<Output>
sourcepub fn exec_with_output(&self) -> Result<Output>
pub fn exec_with_output(&self) -> Result<Output>
Executes the process, returning the stdio output, or an error if non-zero exit status.
sourcepub fn exec_with_streaming(
&self,
on_stdout_line: &mut dyn FnMut(&str) -> Result<()>,
on_stderr_line: &mut dyn FnMut(&str) -> Result<()>,
capture_output: bool
) -> Result<Output>
pub fn exec_with_streaming( &self, on_stdout_line: &mut dyn FnMut(&str) -> Result<()>, on_stderr_line: &mut dyn FnMut(&str) -> Result<()>, capture_output: bool ) -> Result<Output>
Executes a command, passing each line of stdout and stderr to the supplied callbacks, which can mutate the string data.
If any invocations of these function return an error, it will be propagated.
If capture_output
is true, then all the output will also be buffered
and stored in the returned Output
object. If it is false, no caching
is done, and the callbacks are solely responsible for handling the
output.
sourcefn build_command_with_argfile(&self) -> Result<(Command, NamedTempFile)>
fn build_command_with_argfile(&self) -> Result<(Command, NamedTempFile)>
Builds the command with an @<path>
argfile that contains all the
arguments. This is primarily served for rustc/rustdoc command family.
sourcefn build_command_without_args(&self) -> Command
fn build_command_without_args(&self) -> Command
Builds a command from ProcessBuilder
for everything but not args
.
sourcepub fn build_command(&self) -> Command
pub fn build_command(&self) -> Command
Converts ProcessBuilder
into a std::process::Command
, and handles
the jobserver, if present.
Note that this method doesn’t take argfile fallback into account. The caller should handle it by themselves.
sourcepub fn wrapped(self, wrapper: Option<impl AsRef<OsStr>>) -> Self
pub fn wrapped(self, wrapper: Option<impl AsRef<OsStr>>) -> Self
Wraps an existing command with the provided wrapper, if it is present and valid.
Examples
use cargo_util::ProcessBuilder;
// Running this would execute `rustc`
let cmd = ProcessBuilder::new("rustc");
// Running this will execute `sccache rustc`
let cmd = cmd.wrapped(Some("sccache"));
Trait Implementations§
source§impl Clone for ProcessBuilder
impl Clone for ProcessBuilder
source§fn clone(&self) -> ProcessBuilder
fn clone(&self) -> ProcessBuilder
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ProcessBuilder
impl Debug for ProcessBuilder
Auto Trait Implementations§
impl RefUnwindSafe for ProcessBuilder
impl Send for ProcessBuilder
impl Sync for ProcessBuilder
impl Unpin for ProcessBuilder
impl UnwindSafe for ProcessBuilder
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,
source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 160 bytes