Skip to content

Commit

Permalink
feat: implement From<Command> for std::process::Command, closes #4673 (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 2, 2022
1 parent 5c5c42e commit 9f1d34c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changes/command-into-stdcommand.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Implement `From<api::process::Command> for std::process::Command`.
47 changes: 24 additions & 23 deletions core/tauri/src/api/process/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()?;
Expand All @@ -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 || {
Expand Down

0 comments on commit 9f1d34c

Please sign in to comment.