Skip to content

Commit

Permalink
fix: use Info.plist to determine binary name
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 14, 2024
1 parent 09e8953 commit 92ad508
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ nix = { version = "0.26.0", default-features = false, features = [ "user", "sock

[target."cfg(target_os = \"macos\")".dependencies]
embed_plist = "1.2"
plist = "1"
cocoa = "0.24" # wry still uses 0.24
objc = "0.2" # Do not update without consensus

Expand Down
80 changes: 43 additions & 37 deletions core/tauri/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,9 @@ pub fn restart(env: &Env) {

if let Ok(path) = current_binary(env) {
// on macOS on updates the binary name might have changed
// so we'll read the Contents/MacOS folder instead to infer the actual binary path
// so we'll read the Contents/Info.plist file to determine the binary path
#[cfg(target_os = "macos")]
if let Some(parent) = path.parent() {
if parent.components().last()
== Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS")))
{
let macos_binaries = std::fs::read_dir(parent)
.map(|dir| {
dir
.into_iter()
.flatten()
.map(|entry| entry.path())
.collect::<Vec<_>>()
})
.unwrap_or_default();
match macos_binaries.len() {
0 => {
// should never happen, but let's not panic here since it's a crucial feature for updates
exit(1);
}
1 => {
// we have one binary (no sidecar) so we should use it to restart
if let Err(e) = Command::new(macos_binaries.first().unwrap())
.args(&env.args)
.spawn()
{
eprintln!("failed to restart app: {e}");
}

exit(0);
}
_ => {
// in case of sidecars we don't have enough information here to decide what's the right binary name
// so let's hope the binary name didn't change by running the Command::spawn below
}
}
}
}
restart_macos_app(&path, env);

if let Err(e) = Command::new(path).args(&env.args).spawn() {
eprintln!("failed to restart app: {e}");
Expand All @@ -129,3 +94,44 @@ pub fn restart(env: &Env) {

exit(0);
}

#[cfg(target_os = "macos")]
fn restart_macos_app(current_binary: &PathBuf, env: &Env) {
use std::process::{exit, Command};

if let Some(macos_directory) = current_binary.parent() {
if macos_directory.components().last()
!= Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS")))
{
return;
}

if let Some(contents_directory) = macos_directory.parent() {
if contents_directory.components().last()
!= Some(std::path::Component::Normal(std::ffi::OsStr::new(
"Contents",
)))
{
return;
}

if let Ok(info_plist) =
plist::from_file::<_, plist::Dictionary>(contents_directory.join("Info.plist"))
{
if let Some(binary_name) = info_plist
.get("CFBundleExecutable")
.and_then(|v| v.as_string())
{
if let Err(e) = Command::new(macos_directory.join(binary_name))
.args(&env.args)
.spawn()
{
eprintln!("failed to restart app: {e}");
}

exit(0);
}
}
}
}
}
1 change: 1 addition & 0 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92ad508

Please sign in to comment.