Skip to content

Commit

Permalink
fix(core): sidecar command path (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 22, 2021
1 parent 7f998d0 commit 99307d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/sidecar-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fixes `sidecar` Command API.
35 changes: 30 additions & 5 deletions core/tauri/src/api/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use shared_child::SharedChild;
use tauri_utils::platform;

/// Payload for the `Terminated` command event.
#[derive(Serialize)]
#[derive(Debug, Clone, Serialize)]
pub struct TerminatedPayload {
/// Exit code of the process.
pub code: Option<i32>,
Expand All @@ -32,7 +32,7 @@ pub struct TerminatedPayload {
}

/// A event sent to the command callback.
#[derive(Serialize)]
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "event", content = "payload")]
pub enum CommandEvent {
/// Stderr line.
Expand Down Expand Up @@ -88,6 +88,30 @@ impl CommandChild {
}
}

#[cfg(not(windows))]
fn relative_command_path(command: String) -> crate::Result<String> {
match std::env::current_exe()?.parent() {
Some(exe_dir) => Ok(format!(
"{}/{}",
exe_dir.to_string_lossy().to_string(),
command
)),
None => Err(super::Error::Command("Could not evaluate executable dir".to_string()).into()),
}
}

#[cfg(windows)]
fn relative_command_path(command: String) -> crate::Result<String> {
match std::env::current_exe()?.parent() {
Some(exe_dir) => Ok(format!(
"{}/{}.exe",
exe_dir.to_string_lossy().to_string(),
command
)),
None => Err(super::Error::Command("Could not evaluate executable dir".to_string()).into()),
}
}

impl Command {
/// Creates a new Command for launching the given program.
pub fn new<S: Into<String>>(program: S) -> Self {
Expand All @@ -98,12 +122,13 @@ impl Command {
}

/// Creates a new Command for launching the given sidecar program.
pub fn new_sidecar<S: Into<String>>(program: S) -> Self {
Self::new(format!(
pub fn new_sidecar<S: Into<String>>(program: S) -> crate::Result<Self> {
let program = format!(
"{}-{}",
program.into(),
platform::target_triple().expect("unsupported platform")
))
);
Ok(Self::new(relative_command_path(program)?))
}

/// Append args to the command.
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/// The error types.
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Command error.
#[error("Command Error: {0}")]
Command(String),
/// The extract archive error.
#[error("Extract Error: {0}")]
Extract(String),
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/endpoints/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Cmd {
#[cfg(shell_execute)]
{
let mut command = if sidecar {
Command::new_sidecar(program)
Command::new_sidecar(program)?
} else {
Command::new(program)
};
Expand Down

0 comments on commit 99307d0

Please sign in to comment.