feat: implement From<Command> for std::process::Command, closes #4673 (#4836)

This commit is contained in:
Lucas Fernandes Nogueira
2022-08-02 18:53:48 -03:00
committed by GitHub
parent 5c5c42edb6
commit 9f1d34c288
2 changed files with 29 additions and 23 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Implement `From<api::process::Command> for std::process::Command`.

View File

@@ -68,26 +68,6 @@ pub enum CommandEvent {
Terminated(TerminatedPayload),
}
macro_rules! get_std_command {
($self: ident) => {{
let mut command = StdCommand::new($self.program);
command.args(&$self.args);
command.stdout(Stdio::piped());
command.stdin(Stdio::piped());
command.stderr(Stdio::piped());
if $self.env_clear {
command.env_clear();
}
command.envs($self.env);
if let Some(current_dir) = $self.current_dir {
command.current_dir(current_dir);
}
#[cfg(windows)]
command.creation_flags(CREATE_NO_WINDOW);
command
}};
}
/// The type to spawn commands.
#[derive(Debug)]
pub struct Command {
@@ -164,6 +144,26 @@ fn relative_command_path(command: String) -> crate::Result<String> {
}
}
impl From<Command> for StdCommand {
fn from(cmd: Command) -> StdCommand {
let mut command = StdCommand::new(cmd.program);
command.args(cmd.args);
command.stdout(Stdio::piped());
command.stdin(Stdio::piped());
command.stderr(Stdio::piped());
if cmd.env_clear {
command.env_clear();
}
command.envs(cmd.env);
if let Some(current_dir) = cmd.current_dir {
command.current_dir(current_dir);
}
#[cfg(windows)]
command.creation_flags(CREATE_NO_WINDOW);
command
}
}
impl Command {
/// Creates a new Command for launching the given program.
pub fn new<S: Into<String>>(program: S) -> Self {
@@ -252,7 +252,8 @@ impl Command {
/// });
/// ```
pub fn spawn(self) -> crate::api::Result<(Receiver<CommandEvent>, CommandChild)> {
let mut command = get_std_command!(self);
let encoding = self.encoding;
let mut command: StdCommand = self.into();
let (stdout_reader, stdout_writer) = pipe()?;
let (stderr_reader, stderr_writer) = pipe()?;
let (stdin_reader, stdin_writer) = pipe()?;
@@ -274,14 +275,14 @@ impl Command {
guard.clone(),
stdout_reader,
CommandEvent::Stdout,
self.encoding,
encoding,
);
spawn_pipe_reader(
tx.clone(),
guard.clone(),
stderr_reader,
CommandEvent::Stderr,
self.encoding,
encoding,
);
spawn(move || {