Skip to content

Commit

Permalink
feat(core): inject src-tauri/Info.plist file on dev and merge on bund…
Browse files Browse the repository at this point in the history
…le, closes #1570 #2338 (#2444)
  • Loading branch information
lucasfernog committed Aug 16, 2021
1 parent 957e863 commit 537ab1b
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/bundler-merge-info-plist.md
@@ -0,0 +1,5 @@
---
"tauri-bundler": patch
---

Merge Tauri-generated Info.plist with the contents of `src-tauri/Info.plist` if it exists.
6 changes: 6 additions & 0 deletions .changes/embed-plist.md
@@ -0,0 +1,6 @@
---
"tauri-codegen": patch
"tauri": patch
---

Embed Info.plist file contents on binary on dev.
20 changes: 20 additions & 0 deletions core/tauri-codegen/src/context.rs
Expand Up @@ -144,13 +144,33 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
quote!(None)
};

#[cfg(target_os = "macos")]
let info_plist = {
if dev {
let info_plist_path = config_parent.join("Info.plist");
if info_plist_path.exists() {
let info_plist_path = info_plist_path.display().to_string();
quote!({
tauri::embed_plist::embed_info_plist!(#info_plist_path);
})
} else {
quote!(())
}
} else {
quote!(())
}
};
#[cfg(not(target_os = "macos"))]
let info_plist = quote!(());

// double braces are purposeful to force the code into a block expression
Ok(quote!(#root::Context::new(
#config,
::std::sync::Arc::new(#assets),
#default_window_icon,
#system_tray_icon,
#package_info,
#info_plist,
)))
}

Expand Down
3 changes: 3 additions & 0 deletions core/tauri/Cargo.toml
Expand Up @@ -76,6 +76,9 @@ futures-lite = "1.12"
gtk = { version = "0.14", features = [ "v3_20" ] }
glib = "0.14"

[target."cfg(target_os = \"macos\")".dependencies]
embed_plist = "1.2"

[build-dependencies]
cfg_aliases = "0.1.1"

Expand Down
20 changes: 18 additions & 2 deletions core/tauri/src/lib.rs
Expand Up @@ -19,6 +19,9 @@
#![warn(missing_docs, rust_2018_idioms)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

#[cfg(target_os = "macos")]
#[doc(hidden)]
pub use embed_plist;
/// The Tauri error enum.
pub use error::Error;
pub use tauri_macros::{command, generate_handler};
Expand Down Expand Up @@ -60,7 +63,7 @@ use crate::{
runtime::window::PendingWindow,
};
use serde::Serialize;
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, fmt, sync::Arc};

// Export types likely to be used by the application.
pub use runtime::menu::CustomMenuItem;
Expand Down Expand Up @@ -146,13 +149,24 @@ macro_rules! tauri_build_context {
/// # Stability
/// This is the output of the `tauri::generate_context!` macro, and is not considered part of the stable API.
/// Unless you know what you are doing and are prepared for this type to have breaking changes, do not create it yourself.
#[derive(Debug)]
pub struct Context<A: Assets> {
pub(crate) config: Config,
pub(crate) assets: Arc<A>,
pub(crate) default_window_icon: Option<Vec<u8>>,
pub(crate) system_tray_icon: Option<Icon>,
pub(crate) package_info: crate::api::PackageInfo,
pub(crate) _info_plist: (),
}

impl<A: Assets> fmt::Debug for Context<A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("config", &self.config)
.field("default_window_icon", &self.default_window_icon)
.field("system_tray_icon", &self.system_tray_icon)
.field("package_info", &self.package_info)
.finish()
}
}

impl<A: Assets> Context<A> {
Expand Down Expand Up @@ -224,13 +238,15 @@ impl<A: Assets> Context<A> {
default_window_icon: Option<Vec<u8>>,
system_tray_icon: Option<Icon>,
package_info: crate::api::PackageInfo,
info_plist: (),
) -> Self {
Self {
config,
assets,
default_window_icon,
system_tray_icon,
package_info,
_info_plist: info_plist,
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions examples/api/src-tauri/Info.plist
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Request camera access for WebRTC</string>
<key>NSMicrophoneUsageDescription</key>
<string>Request microphone access for WebRTC</string>
</dict>
</plist>
25 changes: 24 additions & 1 deletion tooling/bundler/src/bundle/macos/app.rs
Expand Up @@ -181,7 +181,9 @@ fn create_info_plist(
settings: &Settings,
) -> crate::Result<()> {
let build_number = chrono::Utc::now().format("%Y%m%d.%H%M%S");
let file = &mut common::create_file(&bundle_dir.join("Info.plist"))?;

let bundle_plist_path = bundle_dir.join("Info.plist");
let file = &mut common::create_file(&bundle_plist_path)?;
let use_bootstrapper = settings.macos().use_bootstrapper.unwrap_or_default();
write!(
file,
Expand Down Expand Up @@ -296,6 +298,27 @@ fn create_info_plist(

write!(file, "</dict>\n</plist>\n")?;
file.flush()?;

if let Some(user_plist_path) = &settings.macos().info_plist_path {
let mut cmd = Command::new("/usr/libexec/PlistBuddy");
cmd.args(&[
"-c".into(),
format!("Merge {}", user_plist_path.display()),
bundle_plist_path.display().to_string(),
]);

common::execute_with_verbosity(&mut cmd, settings).map_err(|_| {
crate::Error::ShellScriptError(format!(
"error running /usr/libexec/PlistBuddy{}",
if settings.is_verbose() {
""
} else {
", try running with --verbose to see command output"
}
))
})?;
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions tooling/bundler/src/bundle/settings.rs
Expand Up @@ -170,6 +170,8 @@ pub struct MacOsSettings {
pub signing_identity: Option<String>,
/// Path to the entitlements.plist file.
pub entitlements: Option<String>,
/// Path to the Info.plist file for the bundle.
pub info_plist_path: Option<PathBuf>,
}

/// Settings specific to the WiX implementation.
Expand Down
8 changes: 8 additions & 0 deletions tooling/cli.rs/src/interface/rust.rs
Expand Up @@ -436,6 +436,14 @@ fn tauri_config_to_bundle_settings(
exception_domain: config.macos.exception_domain,
signing_identity,
entitlements: config.macos.entitlements,
info_plist_path: {
let path = tauri_dir().join("Info.plist");
if path.exists() {
Some(path)
} else {
None
}
},
},
windows: WindowsSettings {
timestamp_url: config.windows.timestamp_url,
Expand Down

0 comments on commit 537ab1b

Please sign in to comment.