Skip to content

Commit

Permalink
consistency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
amr-crabnebula committed Sep 13, 2023
1 parent 95b5a8d commit f3c6a84
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 249 deletions.
1 change: 0 additions & 1 deletion 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 crates/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
]
},
"deb": {
"description": "Platform-specific configurations. Debian-specific settings.",
"description": "Debian-specific settings.",
"anyOf": [
{
"$ref": "#/definitions/DebianConfig"
Expand Down
1 change: 0 additions & 1 deletion crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ pub struct Config {
pub external_binaries: Option<Vec<String>>,
/// Signing configuration.
pub signing: Option<SigningConfig>,
/// Platform-specific configurations.
/// Debian-specific settings.
pub deb: Option<DebianConfig>,
/// WiX configuration.
Expand Down
1 change: 0 additions & 1 deletion crates/packager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@ libflate = "2.0"
icns = { package = "tauri-icns", version = "0.1" }
time = { version = "0.3", features = [ "formatting" ] }
plist = "1"
dirs-next = "2"
image = "0.24"
tempfile = "3"
2 changes: 1 addition & 1 deletion crates/packager/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
]
},
"deb": {
"description": "Platform-specific configurations. Debian-specific settings.",
"description": "Debian-specific settings.",
"anyOf": [
{
"$ref": "#/definitions/DebianConfig"
Expand Down
179 changes: 83 additions & 96 deletions crates/packager/src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,53 @@
use std::{
fs::{copy, create_dir, create_dir_all, read_link},
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use crate::{
config::{Config, ConfigExt, ConfigExtInternal},
sign::{notarize, notarize_auth, sign},
util::create_icns_file,
sign,
};

use log::{info, warn};

pub fn package(config: &Config) -> crate::Result<Vec<PathBuf>> {
// we should use the bundle name (App name) as a MacOS standard.
// version or platform shouldn't be included in the App name.
let app_product_name = format!("{}.app", config.product_name);
let app_bundle_path = config.out_dir().join(&app_product_name);

let app_bundle_path = config
.out_dir()
.join("bundle/macos")
.join(&app_product_name);
log::info!(action = "Packaging"; "{} ({})", app_product_name, app_bundle_path.display());

info!(action = "Bundling"; "{} ({})", app_product_name, app_bundle_path.display());
let contents_directory = app_bundle_path.join("Contents");
std::fs::create_dir_all(&contents_directory)?;

let bundle_directory = app_bundle_path.join("Contents");
create_dir_all(&bundle_directory)?;
let resources_dir = contents_directory.join("Resources");
let bin_dir = contents_directory.join("MacOS");

let resources_dir = bundle_directory.join("Resources");
let bin_dir = bundle_directory.join("MacOS");
let bundle_icon_file = util::create_icns_file(&resources_dir, config)?;

let bundle_icon_file = create_icns_file(&resources_dir, config)?;
log::debug!("creating info.plist");
create_info_plist(&contents_directory, bundle_icon_file, config)?;

create_info_plist(&bundle_directory, bundle_icon_file, config)?;

copy_frameworks_to_bundle(&bundle_directory, config)?;
log::debug!("copying frameworks");
copy_frameworks_to_bundle(&contents_directory, config)?;

log::debug!("copying resources");
config.copy_resources(&resources_dir)?;

log::debug!("copying external binaries");
config.copy_external_binaries(&bin_dir)?;

copy_binaries_to_bundle(&bundle_directory, config)?;
log::debug!("copying binaries");
copy_binaries_to_bundle(&contents_directory, config)?;

if let Some(identity) = config
.macos
.as_ref()
.macos()
.and_then(|macos| macos.signing_identity.as_ref())
{
// sign application
sign(app_bundle_path.clone(), identity, config, true)?;
sign::try_sign(app_bundle_path.clone(), identity, config, true)?;
// notarization is required for distribution
match notarize_auth() {
match sign::notarize_auth() {
Ok(auth) => {
notarize(app_bundle_path.clone(), auth, config)?;
}
Err(e) => {
warn!("skipping app notarization, {}", e.to_string());
log::warn!("skipping app notarization, {}", e.to_string());
}
}
}
Expand All @@ -63,22 +56,21 @@ pub fn package(config: &Config) -> crate::Result<Vec<PathBuf>> {
}

// Copies the app's binaries to the bundle.
fn copy_binaries_to_bundle(bundle_directory: &Path, config: &Config) -> crate::Result<()> {
let bin_dir = bundle_directory.join("MacOS");
create_dir_all(&bin_dir)?;
fn copy_binaries_to_bundle(contents_directory: &Path, config: &Config) -> crate::Result<()> {
let bin_dir = contents_directory.join("MacOS");
std::fs::create_dir_all(&bin_dir)?;

log::debug!("copying binaries");
for bin in config.binaries.iter() {
for bin in &config.binaries {
let bin_path = config.binary_path(bin);
copy(&bin_path, bin_dir.join(&bin.filename))?;
std::fs::copy(&bin_path, bin_dir.join(&bin.filename))?;
}

Ok(())
}

// Creates the Info.plist file.
fn create_info_plist(
bundle_dir: &Path,
contents_directory: &Path,
bundle_icon_file: Option<PathBuf>,
config: &Config,
) -> crate::Result<()> {
Expand Down Expand Up @@ -125,8 +117,7 @@ fn create_info_plist(
);
}
if let Some(version) = config
.macos
.as_ref()
.macos()
.and_then(|macos| macos.minimum_system_version.as_deref())
{
plist.insert("LSMinimumSystemVersion".into(), version.into());
Expand Down Expand Up @@ -177,8 +168,7 @@ fn create_info_plist(
}

if let Some(exception_domain) = config
.macos
.as_ref()
.macos()
.and_then(|macos| macos.exception_domain.clone())
{
let mut security = plist::Dictionary::new();
Expand All @@ -193,8 +183,7 @@ fn create_info_plist(
}

if let Some(user_plist_path) = config
.macos
.as_ref()
.macos()
.and_then(|macos| macos.info_plist_path.as_ref())
{
let user_plist = plist::Value::from_file(user_plist_path)?;
Expand All @@ -205,7 +194,7 @@ fn create_info_plist(
}
}

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

Ok(())
}
Expand All @@ -222,19 +211,19 @@ fn copy_dir(from: &Path, to: &Path) -> crate::Result<()> {
}

let parent = to.parent().expect("No data in parent");
create_dir_all(parent)?;
std::fs::create_dir_all(parent)?;
for entry in walkdir::WalkDir::new(from) {
let entry = entry?;
debug_assert!(entry.path().starts_with(from));
let rel_path = entry.path().strip_prefix(from)?;
let dest_path = to.join(rel_path);
if entry.file_type().is_symlink() {
let target = read_link(entry.path())?;
let target = std::fs::read_link(entry.path())?;
std::os::unix::fs::symlink(&target, &dest_path)?;
} else if entry.file_type().is_dir() {
create_dir(dest_path)?;
std::fs::create_dir(dest_path)?;
} else {
copy(entry.path(), dest_path)?;
std::fs::copy(entry.path(), dest_path)?;
}
}
Ok(())
Expand All @@ -253,60 +242,58 @@ fn copy_framework_from(dest_dir: &Path, framework: &str, src_dir: &Path) -> crat
}

// Copies the macOS application bundle frameworks to the .app
fn copy_frameworks_to_bundle(bundle_directory: &Path, config: &Config) -> crate::Result<()> {
let frameworks = config
.macos
.as_ref()
.map(|macos| macos.frameworks.as_ref().cloned().unwrap_or_default())
.unwrap_or_default();
if frameworks.is_empty() {
return Ok(());
}

let dest_dir = bundle_directory.join("Frameworks");
create_dir_all(bundle_directory)?;

for framework in frameworks {
if framework.ends_with(".framework") {
let src_path = PathBuf::from(framework);
let src_name = src_path
.file_name()
.expect("Couldn't get framework filename");
copy_dir(&src_path, &dest_dir.join(src_name))?;
continue;
} else if framework.ends_with(".dylib") {
let src_path = PathBuf::from(&framework);
if !src_path.exists() {
return Err(crate::Error::FrameworkNotFound(framework));
fn copy_frameworks_to_bundle(contents_directory: &Path, config: &Config) -> crate::Result<()> {
if let Some(frameworks) = config.macos().and_then(|m| m.frameworks.as_ref()) {
let dest_dir = contents_directory.join("Frameworks");
std::fs::create_dir_all(contents_directory)?;

for framework in frameworks {
if framework.ends_with(".framework") {
let src_path = PathBuf::from(framework);
let src_name = src_path
.file_name()
.expect("Couldn't get framework filename");
copy_dir(&src_path, &dest_dir.join(src_name))?;
continue;
} else if framework.ends_with(".dylib") {
let src_path = PathBuf::from(&framework);
if !src_path.exists() {
return Err(crate::Error::FrameworkNotFound(framework));
}
let src_name = src_path.file_name().expect("Couldn't get library filename");
std::fs::create_dir_all(&dest_dir)?;
std::fs::copy(&src_path, dest_dir.join(src_name))?;
continue;
} else if framework.contains('/') {
return Err(crate::Error::InvalidFramework {
framework,
reason: "path should have the .framework extension",
});
}
let src_name = src_path.file_name().expect("Couldn't get library filename");
create_dir_all(&dest_dir)?;
copy(&src_path, dest_dir.join(src_name))?;
continue;
} else if framework.contains('/') {
return Err(crate::Error::InvalidFramework {
framework,
reason: "path should have the .framework extension",
});
}
if let Some(home_dir) = dirs_next::home_dir() {
if copy_framework_from(&dest_dir, &framework, &home_dir.join("Library/Frameworks/"))? {
if let Some(home_dir) = dirs::home_dir() {
if copy_framework_from(
&dest_dir,
&framework,
&home_dir.join("Library/Frameworks/"),
)? {
continue;
}
}
if copy_framework_from(
&dest_dir,
&framework,
&PathBuf::from("/Library/Frameworks/"),
)? || copy_framework_from(
&dest_dir,
&framework,
&PathBuf::from("/Network/Library/Frameworks/"),
)? {
continue;
}
}
if copy_framework_from(
&dest_dir,
&framework,
&PathBuf::from("/Library/Frameworks/"),
)? || copy_framework_from(
&dest_dir,
&framework,
&PathBuf::from("/Network/Library/Frameworks/"),
)? {
continue;
}

return Err(crate::Error::FrameworkNotFound(framework));
return Err(crate::Error::FrameworkNotFound(framework));
}
}

Ok(())
}
6 changes: 6 additions & 0 deletions crates/packager/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub trait ConfigExt {
fn wix(&self) -> Option<&WixConfig>;
/// Returns the debian specific configuration
fn deb(&self) -> Option<&DebianConfig>;
/// Returns the macos specific configuration
fn macos(&self) -> Option<&MacOsConfig>;
/// Returns the target triple for the package to be built (e.g. "aarch64-unknown-linux-gnu").
fn target_triple(&self) -> String;
/// Returns the architecture for the package to be built (e.g. "arm", "x86" or "x86_64").
Expand All @@ -39,6 +41,10 @@ impl ConfigExt for Config {
self.windows.as_ref()
}

fn macos(&self) -> Option<&MacOsConfig> {
self.macos.as_ref()
}

fn nsis(&self) -> Option<&NsisConfig> {
self.nsis.as_ref()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/packager/src/deb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn generate_data(config: &Config, package_dir: &Path) -> crate::Result<PathB
std::fs::copy(&bin_path, bin_dir.join(&bin.filename))?;
}

log::debug!("copying resource files");
log::debug!("copying resources");
let resource_dir = data_dir.join("usr/lib").join(config.main_binary_name()?);
config.copy_resources(&resource_dir)?;

Expand Down
9 changes: 5 additions & 4 deletions crates/packager/src/nsis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use handlebars::{to_json, Handlebars};

use crate::{
config::{Config, ConfigExt, ConfigExtInternal, LogLevel, NSISInstallerMode},
sign,
util::{self, download, download_and_verify, extract_zip, HashAlgorithm},
};

Expand Down Expand Up @@ -253,7 +254,7 @@ fn build_nsis_app_installer(
{
let main_binary = config.main_binary()?;
let app_exe_source = config.binary_path(main_binary);
crate::sign::try_sign(&app_exe_source.with_extension("exe"), config)?;
sign::try_sign(&app_exe_source.with_extension("exe"), config)?;
}

#[cfg(not(target_os = "windows"))]
Expand Down Expand Up @@ -295,13 +296,13 @@ fn build_nsis_app_installer(

#[cfg(target_os = "windows")]
{
use crate::sign::ConfigSignExt;
use sign::ConfigSignExt;
if config.can_sign() {
data.insert(
"uninstaller_sign_cmd",
to_json(format!(
"{:?}",
crate::sign::sign_command("%1", &config.sign_params())?.0
sign::sign_command("%1", &config.sign_params())?.0
)),
);
}
Expand Down Expand Up @@ -474,7 +475,7 @@ fn build_nsis_app_installer(
std::fs::rename(nsis_output_path, &nsis_installer_path)?;

#[cfg(target_os = "windows")]
crate::sign::try_sign(&nsis_installer_path, config)?;
sign::try_sign(&nsis_installer_path, config)?;
#[cfg(not(target_os = "windows"))]
log::warn!("Code signing is currently only supported on Windows hosts, skipping signing the installer...");

Expand Down
Loading

0 comments on commit f3c6a84

Please sign in to comment.