From 9f1d34c288cbe64f0453cf210bc9488bb42ed19a Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 2 Aug 2022 18:53:48 -0300 Subject: [PATCH] feat: implement From for std::process::Command, closes #4673 (#4836) --- .changes/command-into-stdcommand.md | 5 +++ core/tauri/src/api/process/command.rs | 47 ++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) create mode 100644 .changes/command-into-stdcommand.md diff --git a/.changes/command-into-stdcommand.md b/.changes/command-into-stdcommand.md new file mode 100644 index 000000000..0bb818ba7 --- /dev/null +++ b/.changes/command-into-stdcommand.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Implement `From for std::process::Command`. diff --git a/core/tauri/src/api/process/command.rs b/core/tauri/src/api/process/command.rs index 965d34ce7..0c2ce8ab0 100644 --- a/core/tauri/src/api/process/command.rs +++ b/core/tauri/src/api/process/command.rs @@ -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 { } } +impl From 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>(program: S) -> Self { @@ -252,7 +252,8 @@ impl Command { /// }); /// ``` pub fn spawn(self) -> crate::api::Result<(Receiver, 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 || {