Skip to content

Commit

Permalink
refactor(bundler): use the plist crate to create and merge Info.pli…
Browse files Browse the repository at this point in the history
…st (#4412)
  • Loading branch information
lucasfernog committed Jun 21, 2022
1 parent 079b1cc commit 45076b3
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changes/refactor-bundler-plist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-bundler": patch
---

Use the plist crate instead of the `PlistBuddy` binary to merge user Info.plist file.
1 change: 1 addition & 0 deletions tooling/bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ zip = "0.6"
[target."cfg(target_os = \"macos\")".dependencies]
icns = "0.3"
time = { version = "0.3", features = [ "formatting" ] }
plist = "1"

[target."cfg(any(target_os = \"macos\", target_os = \"windows\"))".dependencies]
regex = "1"
Expand Down
171 changes: 57 additions & 114 deletions tooling/bundler/src/bundle/macos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ use super::{
icon::create_icns_file,
sign::{notarize, notarize_auth_args, sign},
};
use crate::{bundle::common::CommandExt, Settings};
use crate::Settings;

use anyhow::Context;
use log::{info, warn};

use std::{
fs,
io::prelude::*,
path::{Path, PathBuf},
process::Command,
};

/// Bundles the project.
Expand Down Expand Up @@ -124,130 +122,75 @@ fn create_info_plist(
.format(&format)
.map_err(time::error::Error::from)?;

let bundle_plist_path = bundle_dir.join("Info.plist");
let file = &mut common::create_file(&bundle_plist_path)?;
write!(
file,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \
\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
<plist version=\"1.0\">\n\
<dict>\n"
)?;
write!(
file,
" <key>CFBundleDevelopmentRegion</key>\n \
<string>English</string>\n"
)?;
write!(
file,
" <key>CFBundleDisplayName</key>\n <string>{}</string>\n",
settings.product_name()
)?;
write!(
file,
" <key>CFBundleExecutable</key>\n <string>{}</string>\n",
settings.main_binary_name()
)?;
let mut plist = plist::Dictionary::new();
plist.insert("CFBundleDevelopmentRegion".into(), "English".into());
plist.insert("CFBundleDisplayName".into(), settings.product_name().into());
plist.insert(
"CFBundleExecutable".into(),
settings.main_binary_name().into(),
);
if let Some(path) = bundle_icon_file {
write!(
file,
" <key>CFBundleIconFile</key>\n <string>{}</string>\n",
path.file_name().expect("No file name").to_string_lossy()
)?;
plist.insert(
"CFBundleIconFile".into(),
path
.file_name()
.expect("No file name")
.to_string_lossy()
.into_owned()
.into(),
);
}
write!(
file,
" <key>CFBundleIdentifier</key>\n <string>{}</string>\n",
settings.bundle_identifier()
)?;
write!(
file,
" <key>CFBundleInfoDictionaryVersion</key>\n \
<string>6.0</string>\n"
)?;
write!(
file,
" <key>CFBundleName</key>\n <string>{}</string>\n",
settings.product_name()
)?;
write!(
file,
" <key>CFBundlePackageType</key>\n <string>APPL</string>\n"
)?;
write!(
file,
" <key>CFBundleShortVersionString</key>\n <string>{}</string>\n",
settings.version_string()
)?;
write!(
file,
" <key>CFBundleVersion</key>\n <string>{}</string>\n",
build_number
)?;
write!(file, " <key>CSResourcesFileMapped</key>\n <true/>\n")?;
plist.insert(
"CFBundleIdentifier".into(),
settings.bundle_identifier().into(),
);
plist.insert("CFBundleInfoDictionaryVersion".into(), "6.0".into());
plist.insert("CFBundleName".into(), settings.product_name().into());
plist.insert("CFBundlePackageType".into(), "APPL".into());
plist.insert(
"CFBundleShortVersionString".into(),
settings.version_string().into(),
);
plist.insert("CFBundleVersion".into(), build_number.into());
plist.insert("CSResourcesFileMapped".into(), true.into());
if let Some(category) = settings.app_category() {
write!(
file,
" <key>LSApplicationCategoryType</key>\n \
<string>{}</string>\n",
category.macos_application_category_type()
)?;
plist.insert(
"LSApplicationCategoryType".into(),
category.macos_application_category_type().into(),
);
}
if let Some(version) = &settings.macos().minimum_system_version {
write!(
file,
" <key>LSMinimumSystemVersion</key>\n \
<string>{}</string>\n",
version
)?;
if let Some(version) = settings.macos().minimum_system_version.clone() {
plist.insert("LSMinimumSystemVersion".into(), version.into());
}
write!(file, " <key>LSRequiresCarbon</key>\n <true/>\n")?;
write!(file, " <key>NSHighResolutionCapable</key>\n <true/>\n")?;
plist.insert("LSRequiresCarbon".into(), true.into());
plist.insert("NSHighResolutionCapable".into(), true.into());
if let Some(copyright) = settings.copyright_string() {
write!(
file,
" <key>NSHumanReadableCopyright</key>\n \
<string>{}</string>\n",
copyright
)?;
plist.insert("NSHumanReadableCopyright".into(), copyright.into());
}

if let Some(exception_domain) = &settings.macos().exception_domain {
write!(
file,
" <key>NSAppTransportSecurity</key>\n \
<dict>\n \
<key>NSExceptionDomains</key>\n \
<dict>\n \
<key>{}</key>\n \
<dict>\n \
<key>NSExceptionAllowsInsecureHTTPLoads</key>\n \
<true/>\n \
<key>NSIncludesSubdomains</key>\n \
<true/>\n \
</dict>\n \
</dict>\n \
</dict>",
exception_domain
)?;
}
if let Some(exception_domain) = settings.macos().exception_domain.clone() {
let mut security = plist::Dictionary::new();
let mut domain = plist::Dictionary::new();
domain.insert("NSExceptionAllowsInsecureHTTPLoads".into(), true.into());
domain.insert("NSIncludesSubdomains".into(), true.into());

write!(file, "</dict>\n</plist>\n")?;
file.flush()?;
let mut exception_domains = plist::Dictionary::new();
exception_domains.insert(exception_domain, domain.into());
security.insert("NSExceptionDomains".into(), exception_domains.into());
plist.insert("NSAppTransportSecurity".into(), security.into());
}

if let Some(user_plist_path) = &settings.macos().info_plist_path {
// TODO: use the plist crate instead
Command::new("/usr/libexec/PlistBuddy")
.args(&[
"-c".into(),
format!("Merge {}", user_plist_path.display()),
bundle_plist_path.display().to_string(),
])
.output_ok()
.context("error running PlistBuddy")?;
let user_plist = plist::Value::from_file(user_plist_path)?;
if let Some(dict) = user_plist.into_dictionary() {
for (key, value) in dict {
plist.insert(key, value);
}
}
}

plist::Value::Dictionary(plist).to_file_xml(bundle_dir.join("Info.plist"))?;

Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions tooling/bundler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub enum Error {
#[cfg(target_os = "macos")]
#[error("`{0}`")]
TimeError(#[from] time::error::Error),
/// Plist error.
#[cfg(target_os = "macos")]
#[error(transparent)]
Plist(#[from] plist::Error),
}

/// Convenient type alias of Result type.
Expand Down
36 changes: 36 additions & 0 deletions tooling/cli/Cargo.lock

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

2 changes: 1 addition & 1 deletion tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
"type": "boolean"
},
"theme": {
"description": "The initial window theme. Defaults to the system theme. Only implemented on Windows and macOs 10.14+.",
"description": "The initial window theme. Defaults to the system theme. Only implemented on Windows and macOS 10.14+.",
"anyOf": [
{
"$ref": "#/definitions/Theme"
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::helpers::{
app_paths::{app_dir, tauri_dir},
command_env,
config::{get as get_config, AppUrl, ShellAllowlistOpen, WindowUrl},
config::{get as get_config, AppUrl, WindowUrl},
manifest::rewrite_manifest,
updater_signature::sign_file_from_env_variables,
};
Expand Down Expand Up @@ -314,6 +314,7 @@ pub fn command(options: Options) -> Result<()> {
// set env vars used by the bundler
#[cfg(target_os = "linux")]
{
use crate::helpers::config::ShellAllowlistOpen;
if matches!(
config_.tauri.allowlist.shell.open,
ShellAllowlistOpen::Flag(true) | ShellAllowlistOpen::Validate(_)
Expand Down

0 comments on commit 45076b3

Please sign in to comment.