use crate::Env;
use std::path::PathBuf;
pub fn current_binary(_env: &Env) -> std::io::Result<PathBuf> {
#[cfg(target_os = "linux")]
if let Some(app_image_path) = &_env.appimage {
return Ok(PathBuf::from(app_image_path));
}
tauri_utils::platform::current_exe()
}
pub fn restart(env: &Env) -> ! {
use std::process::{exit, Command};
if let Ok(path) = current_binary(env) {
#[cfg(target_os = "macos")]
restart_macos_app(&path, env);
if let Err(e) = Command::new(path)
.args(env.args_os.iter().skip(1).collect::<Vec<_>>())
.spawn()
{
log::error!("failed to restart app: {e}");
}
}
exit(0);
}
#[cfg(target_os = "macos")]
fn restart_macos_app(current_binary: &std::path::Path, env: &Env) {
use std::process::{exit, Command};
if let Some(macos_directory) = current_binary.parent() {
if macos_directory.components().next_back()
!= Some(std::path::Component::Normal(std::ffi::OsStr::new("MacOS")))
{
return;
}
if let Some(contents_directory) = macos_directory.parent() {
if contents_directory.components().next_back()
!= 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_os.iter().skip(1).collect::<Vec<_>>())
.spawn()
{
log::error!("failed to restart app: {e}");
}
exit(0);
}
}
}
}
}