Jelajahi Sumber

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

Lucas Fernandes Nogueira 3 tahun lalu
induk
melakukan
9f1d34c288
2 mengubah file dengan 29 tambahan dan 23 penghapusan
  1. 5 0
      .changes/command-into-stdcommand.md
  2. 24 23
      core/tauri/src/api/process/command.rs

+ 5 - 0
.changes/command-into-stdcommand.md

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

+ 24 - 23
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<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 || {