Skip to content

Commit fcb702e

Browse files
authored
fix(cli): allow build --bundles nsis arg in linux+macOS (#14954)
1 parent f17240b commit fcb702e

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

.changes/fix-build-bundles-arg.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-bundler": patch:bug
3+
"tauri-cli": patch:bug
4+
"@tauri-apps/cli": patch:bug
5+
---
6+
7+
Fix `build --bundles` to allow `nsis` arg in linux+macOS

crates/tauri-bundler/src/bundle.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// SPDX-License-Identifier: MIT
55

66
mod category;
7-
#[cfg(any(target_os = "linux", target_os = "windows"))]
87
mod kmp;
98
#[cfg(target_os = "linux")]
109
mod linux;
@@ -17,26 +16,16 @@ mod windows;
1716

1817
use tauri_utils::{display_path, platform::Target as TargetPlatform};
1918

20-
#[cfg(any(target_os = "linux", target_os = "windows"))]
2119
const BUNDLE_VAR_TOKEN: &[u8] = b"__TAURI_BUNDLE_TYPE_VAR_UNK";
2220
/// Patch a binary with bundle type information
23-
#[cfg(any(target_os = "linux", target_os = "windows"))]
2421
fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<()> {
25-
log::info!(
26-
"Patching {} with bundle type information: {}",
27-
display_path(binary),
28-
package_type.short_name()
29-
);
30-
31-
let mut file_data = std::fs::read(binary).expect("Could not read binary file.");
32-
33-
let bundle_var_index =
34-
kmp::index_of(BUNDLE_VAR_TOKEN, &file_data).ok_or(crate::Error::MissingBundleTypeVar)?;
3522
#[cfg(target_os = "linux")]
3623
let bundle_type = match package_type {
3724
crate::PackageType::Deb => b"__TAURI_BUNDLE_TYPE_VAR_DEB",
3825
crate::PackageType::Rpm => b"__TAURI_BUNDLE_TYPE_VAR_RPM",
3926
crate::PackageType::AppImage => b"__TAURI_BUNDLE_TYPE_VAR_APP",
27+
// NSIS installers can be built in linux using cargo-xwin
28+
crate::PackageType::Nsis => b"__TAURI_BUNDLE_TYPE_VAR_NSS",
4029
_ => {
4130
return Err(crate::Error::InvalidPackageType(
4231
package_type.short_name().to_owned(),
@@ -55,7 +44,31 @@ fn patch_binary(binary: &PathBuf, package_type: &PackageType) -> crate::Result<(
5544
))
5645
}
5746
};
47+
#[cfg(target_os = "macos")]
48+
let bundle_type = match package_type {
49+
// NSIS installers can be built in macOS using cargo-xwin
50+
crate::PackageType::Nsis => b"__TAURI_BUNDLE_TYPE_VAR_NSS",
51+
crate::PackageType::MacOsBundle | crate::PackageType::Dmg => {
52+
// skip patching for macOS-native bundles
53+
return Ok(());
54+
}
55+
_ => {
56+
return Err(crate::Error::InvalidPackageType(
57+
package_type.short_name().to_owned(),
58+
"macOS".to_owned(),
59+
))
60+
}
61+
};
5862

63+
log::info!(
64+
"Patching {} with bundle type information: {}",
65+
display_path(binary),
66+
package_type.short_name()
67+
);
68+
69+
let mut file_data = std::fs::read(binary).expect("Could not read binary file.");
70+
let bundle_var_index =
71+
kmp::index_of(BUNDLE_VAR_TOKEN, &file_data).ok_or(crate::Error::MissingBundleTypeVar)?;
5972
file_data[bundle_var_index..bundle_var_index + BUNDLE_VAR_TOKEN.len()]
6073
.copy_from_slice(bundle_type);
6174

@@ -133,7 +146,6 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<Bundle>> {
133146
continue;
134147
}
135148

136-
#[cfg(any(target_os = "linux", target_os = "windows"))]
137149
if let Err(e) = patch_binary(&main_binary_path, package_type) {
138150
log::warn!("Failed to add bundler type to the binary: {e}. Updater plugin may not be able to update this package. This shouldn't normally happen, please report it to https://github.com/tauri-apps/tauri/issues");
139151
}
@@ -163,7 +175,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<Bundle>> {
163175

164176
#[cfg(target_os = "windows")]
165177
PackageType::WindowsMsi => windows::msi::bundle_project(settings, false)?,
166-
// note: don't restrict to windows as NSIS installers can be built in linux using cargo-xwin
178+
// don't restrict to windows as NSIS installers can be built in linux+macOS using cargo-xwin
167179
PackageType::Nsis => windows::nsis::bundle_project(settings, false)?,
168180

169181
#[cfg(target_os = "linux")]

crates/tauri-bundler/src/bundle/kmp/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// Knuth–Morris–Pratt algorithm
77
// based on https://github.com/howeih/rust_kmp
8-
#[cfg(any(target_os = "linux", target_os = "windows"))]
98
pub fn index_of(pattern: &[u8], target: &[u8]) -> Option<usize> {
109
let failure_function = find_failure_function(pattern);
1110

@@ -38,7 +37,6 @@ pub fn index_of(pattern: &[u8], target: &[u8]) -> Option<usize> {
3837
None
3938
}
4039

41-
#[cfg(any(target_os = "linux", target_os = "windows"))]
4240
fn find_failure_function(pattern: &[u8]) -> Vec<usize> {
4341
let mut i = 1;
4442
let mut j = 0;

crates/tauri-bundler/src/bundle/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const ALL_PACKAGE_TYPES: &[PackageType] = &[
126126
PackageType::IosBundle,
127127
#[cfg(target_os = "windows")]
128128
PackageType::WindowsMsi,
129-
#[cfg(target_os = "windows")]
129+
// NSIS installers can be built on all platforms but it's hidden in the --help output on macOS/Linux.
130130
PackageType::Nsis,
131131
#[cfg(target_os = "macos")]
132132
PackageType::MacOsBundle,

crates/tauri-cli/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl ValueEnum for BundleFormat {
4343
}
4444

4545
fn to_possible_value(&self) -> Option<PossibleValue> {
46-
let hide = self.0 == PackageType::Updater;
46+
let hide = (!cfg!(windows) && self.0 == PackageType::Nsis) || self.0 == PackageType::Updater;
4747
Some(PossibleValue::new(self.0.short_name()).hide(hide))
4848
}
4949
}

0 commit comments

Comments
 (0)