Skip to content

Commit 3327dd6

Browse files
fix(bundler/macos): clean up .app bundle if only .dmg is enabled #7081 (#7116)
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent c596aae commit 3327dd6

File tree

5 files changed

+76
-24
lines changed

5 files changed

+76
-24
lines changed

.changes/cleanup-app-bundle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": patch
3+
---
4+
5+
Remove macOS app bundles from the output if they are not requested by the user.

tooling/bundler/src/bundle.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub use self::{
2424
Settings, SettingsBuilder, UpdaterSettings,
2525
},
2626
};
27+
#[cfg(target_os = "macos")]
28+
use anyhow::Context;
2729
use log::{info, warn};
2830
pub use settings::{NsisSettings, WindowsSettings, WixLanguage, WixLanguageConfig, WixSettings};
2931

@@ -41,7 +43,7 @@ pub struct Bundle {
4143
/// Bundles the project.
4244
/// Returns the list of paths where the bundles can be found.
4345
pub fn bundle_project(settings: Settings) -> crate::Result<Vec<Bundle>> {
44-
let mut bundles = Vec::new();
46+
let mut bundles: Vec<Bundle> = Vec::new();
4547
let package_types = settings.package_types()?;
4648

4749
let target_os = settings
@@ -56,14 +58,28 @@ pub fn bundle_project(settings: Settings) -> crate::Result<Vec<Bundle>> {
5658
}
5759

5860
for package_type in &package_types {
61+
// bundle was already built! e.g. DMG already built .app
62+
if bundles.iter().any(|b| b.package_type == *package_type) {
63+
continue;
64+
}
65+
5966
let bundle_paths = match package_type {
6067
#[cfg(target_os = "macos")]
6168
PackageType::MacOsBundle => macos::app::bundle_project(&settings)?,
6269
#[cfg(target_os = "macos")]
6370
PackageType::IosBundle => macos::ios::bundle_project(&settings)?,
6471
// dmg is dependant of MacOsBundle, we send our bundles to prevent rebuilding
6572
#[cfg(target_os = "macos")]
66-
PackageType::Dmg => macos::dmg::bundle_project(&settings, &bundles)?,
73+
PackageType::Dmg => {
74+
let bundled = macos::dmg::bundle_project(&settings, &bundles)?;
75+
if !bundled.app.is_empty() {
76+
bundles.push(Bundle {
77+
package_type: PackageType::MacOsBundle,
78+
bundle_paths: bundled.app,
79+
});
80+
}
81+
bundled.dmg
82+
}
6783

6884
#[cfg(target_os = "windows")]
6985
PackageType::WindowsMsi => windows::msi::bundle_project(&settings, false)?,
@@ -90,6 +106,33 @@ pub fn bundle_project(settings: Settings) -> crate::Result<Vec<Bundle>> {
90106
});
91107
}
92108

109+
#[cfg(target_os = "macos")]
110+
{
111+
// Clean up .app if only building dmg or updater
112+
if !package_types.contains(&PackageType::MacOsBundle) {
113+
if let Some(app_bundle_paths) = bundles
114+
.iter()
115+
.position(|b| b.package_type == PackageType::MacOsBundle)
116+
.map(|i| bundles.remove(i))
117+
.map(|b| b.bundle_paths)
118+
{
119+
for app_bundle_path in &app_bundle_paths {
120+
info!(action = "Cleaning"; "{}", app_bundle_path.display());
121+
match app_bundle_path.is_dir() {
122+
true => std::fs::remove_dir_all(app_bundle_path),
123+
false => std::fs::remove_file(app_bundle_path),
124+
}
125+
.with_context(|| {
126+
format!(
127+
"Failed to clean the app bundle at {}",
128+
app_bundle_path.display()
129+
)
130+
})?
131+
}
132+
}
133+
}
134+
}
135+
93136
let pluralised = if bundles.len() == 1 {
94137
"bundle"
95138
} else {

tooling/bundler/src/bundle/macos/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ fn copy_frameworks_to_bundle(bundle_directory: &Path, settings: &Settings) -> cr
219219
return Ok(());
220220
}
221221
let dest_dir = bundle_directory.join("Frameworks");
222-
fs::create_dir_all(&bundle_directory)
222+
fs::create_dir_all(bundle_directory)
223223
.with_context(|| format!("Failed to create Frameworks directory at {:?}", dest_dir))?;
224224
for framework in frameworks.iter() {
225225
if framework.ends_with(".framework") {
226226
let src_path = PathBuf::from(framework);
227227
let src_name = src_path
228228
.file_name()
229229
.expect("Couldn't get framework filename");
230-
common::copy_dir(&src_path, &dest_dir.join(&src_name))?;
230+
common::copy_dir(&src_path, &dest_dir.join(src_name))?;
231231
continue;
232232
} else if framework.ends_with(".dylib") {
233233
let src_path = PathBuf::from(framework);
@@ -238,7 +238,7 @@ fn copy_frameworks_to_bundle(bundle_directory: &Path, settings: &Settings) -> cr
238238
)));
239239
}
240240
let src_name = src_path.file_name().expect("Couldn't get library filename");
241-
common::copy_file(&src_path, &dest_dir.join(&src_name))?;
241+
common::copy_file(&src_path, &dest_dir.join(src_name))?;
242242
continue;
243243
} else if framework.contains('/') {
244244
return Err(crate::Error::GenericError(format!(

tooling/bundler/src/bundle/macos/dmg.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use super::{app, icon::create_icns_file};
77
use crate::{
88
bundle::{common::CommandExt, Bundle},
9-
PackageType::MacOsBundle,
10-
Settings,
9+
PackageType, Settings,
1110
};
1211

1312
use anyhow::Context;
@@ -20,18 +19,23 @@ use std::{
2019
process::{Command, Stdio},
2120
};
2221

22+
pub struct Bundled {
23+
pub dmg: Vec<PathBuf>,
24+
pub app: Vec<PathBuf>,
25+
}
26+
2327
/// Bundles the project.
2428
/// Returns a vector of PathBuf that shows where the DMG was created.
25-
pub fn bundle_project(settings: &Settings, bundles: &[Bundle]) -> crate::Result<Vec<PathBuf>> {
29+
pub fn bundle_project(settings: &Settings, bundles: &[Bundle]) -> crate::Result<Bundled> {
2630
// generate the .app bundle if needed
27-
if bundles
31+
let app_bundle_paths = if !bundles
2832
.iter()
29-
.filter(|bundle| bundle.package_type == MacOsBundle)
30-
.count()
31-
== 0
33+
.any(|bundle| bundle.package_type == PackageType::MacOsBundle)
3234
{
33-
app::bundle_project(settings)?;
34-
}
35+
app::bundle_project(settings)?
36+
} else {
37+
Vec::new()
38+
};
3539

3640
// get the target path
3741
let output_path = settings.project_out_directory().join("bundle/dmg");
@@ -151,5 +155,9 @@ pub fn bundle_project(settings: &Settings, bundles: &[Bundle]) -> crate::Result<
151155
if let Some(identity) = &settings.macos().signing_identity {
152156
super::sign::sign(dmg_path.clone(), identity, settings, false)?;
153157
}
154-
Ok(vec![dmg_path])
158+
159+
Ok(Bundled {
160+
dmg: vec![dmg_path],
161+
app: app_bundle_paths,
162+
})
155163
}

tooling/bundler/src/bundle/macos/sign.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn notarize(
288288
info!("notarization started; waiting for Apple response...");
289289

290290
let uuid = uuid[1].to_string();
291-
get_notarization_status(uuid, auth_args, settings)?;
291+
get_notarization_status(uuid, auth_args)?;
292292
staple_app(app_bundle_path.clone())?;
293293
} else {
294294
return Err(
@@ -322,11 +322,7 @@ fn staple_app(mut app_bundle_path: PathBuf) -> crate::Result<()> {
322322
Ok(())
323323
}
324324

325-
fn get_notarization_status(
326-
uuid: String,
327-
auth_args: Vec<String>,
328-
settings: &Settings,
329-
) -> crate::Result<()> {
325+
fn get_notarization_status(uuid: String, auth_args: Vec<String>) -> crate::Result<()> {
330326
std::thread::sleep(std::time::Duration::from_secs(10));
331327
let result = Command::new("xcrun")
332328
.args(vec!["altool", "--notarization-info", &uuid])
@@ -345,7 +341,7 @@ fn get_notarization_status(
345341
{
346342
let status = status[1].to_string();
347343
if status == "in progress" {
348-
get_notarization_status(uuid, auth_args, settings)
344+
get_notarization_status(uuid, auth_args)
349345
} else if status == "invalid" {
350346
Err(
351347
anyhow::anyhow!(format!(
@@ -366,10 +362,10 @@ fn get_notarization_status(
366362
Ok(())
367363
}
368364
} else {
369-
get_notarization_status(uuid, auth_args, settings)
365+
get_notarization_status(uuid, auth_args)
370366
}
371367
} else {
372-
get_notarization_status(uuid, auth_args, settings)
368+
get_notarization_status(uuid, auth_args)
373369
}
374370
}
375371

0 commit comments

Comments
 (0)