Skip to content

Commit

Permalink
feat: add mainBinaryName option (#10977)
Browse files Browse the repository at this point in the history
* feat: add `mainBinaryName` option

* remove unused imports [skip ci]

* enhance error message [skip ci]

* migrate `mainBinaryName`

* change file

* revert bin src_path

* add link

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
  • Loading branch information
amrbashir and lucasfernog authored Sep 15, 2024
1 parent 3ad2427 commit 35bd9dd
Show file tree
Hide file tree
Showing 32 changed files with 188 additions and 135 deletions.
5 changes: 5 additions & 0 deletions .changes/changelog-path-deb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-bundler': 'major:breaking'
---

Changed changelog file location in `deb` to `usr/share/doc/<product_name>/changelog.gz` instead of `usr/share/doc/<main_binary_name>/changelog.gz`. For tauri v1 users, the path is unchanged as `product_name` and `main_binary_name` used the same value.
8 changes: 8 additions & 0 deletions .changes/main_binary_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"tauri": "patch:feat"
"tauri-utils": "patch:feat"
"tauri-cli": "patch:feat"
"tauri-bundler": "patch:feat"
---

Add `mainBinaryName` config option to set the file name for the main binary.
5 changes: 5 additions & 0 deletions .changes/resources-path-deb-rpm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-bundler': 'major:breaking'
---

Changed resources directory location in `deb` and `rpm` to `/usr/lib/<product_name>` instead of `/usr/lib/<main_binary_name>`. For tauri v1 users, the path is unchanged as `product_name` and `main_binary_name` used the same value.
2 changes: 1 addition & 1 deletion crates/tauri-bundler/src/bundle/linux/appimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
// setup data to insert into shell script
let mut sh_map = BTreeMap::new();
sh_map.insert("arch", settings.target().split('-').next().unwrap());
sh_map.insert("crate_name", settings.main_binary_name());
sh_map.insert("product_name", settings.product_name());
sh_map.insert("appimage_filename", &appimage_filename);

let tauri_tools_path = settings
Expand Down
6 changes: 3 additions & 3 deletions crates/tauri-bundler/src/bundle/linux/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ pub fn generate_data(
fn generate_changelog_file(settings: &Settings, data_dir: &Path) -> crate::Result<()> {
if let Some(changelog_src_path) = &settings.deb().changelog {
let mut src_file = File::open(changelog_src_path)?;
let bin_name = settings.main_binary_name();
let dest_path = data_dir.join(format!("usr/share/doc/{}/changelog.gz", bin_name));
let product_name = settings.product_name();
let dest_path = data_dir.join(format!("usr/share/doc/{product_name}/changelog.gz"));

let changelog_file = common::create_file(&dest_path)?;
let mut gzip_encoder = GzEncoder::new(changelog_file, Compression::new(9));
Expand Down Expand Up @@ -306,7 +306,7 @@ fn generate_md5sums(control_dir: &Path, data_dir: &Path) -> crate::Result<()> {
/// Copy the bundle's resource files into an appropriate directory under the
/// `data_dir`.
fn copy_resource_files(settings: &Settings, data_dir: &Path) -> crate::Result<()> {
let resource_dir = data_dir.join("usr/lib").join(settings.main_binary_name());
let resource_dir = data_dir.join("usr/lib").join(settings.product_name());
settings.copy_resources(&resource_dir)
}

Expand Down
8 changes: 5 additions & 3 deletions crates/tauri-bundler/src/bundle/linux/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ pub fn list_icon_files(
data_dir: &Path,
) -> crate::Result<BTreeMap<Icon, PathBuf>> {
let base_dir = data_dir.join("usr/share/icons/hicolor");
let main_binary_name = settings.main_binary_name()?;
let get_dest_path = |width: u32, height: u32, is_high_density: bool| {
base_dir.join(format!(
"{}x{}{}/apps/{}.png",
width,
height,
if is_high_density { "@2" } else { "" },
settings.main_binary_name()
main_binary_name
))
};
let mut icons = BTreeMap::new();
Expand Down Expand Up @@ -97,8 +98,9 @@ pub fn generate_desktop_file(
custom_template_path: &Option<PathBuf>,
data_dir: &Path,
) -> crate::Result<(PathBuf, PathBuf)> {
let bin_name = settings.main_binary_name();
let desktop_file_name = format!("{bin_name}.desktop");
let bin_name = settings.main_binary_name()?;
let product_name = settings.product_name();
let desktop_file_name = format!("{product_name}.desktop");
let path = PathBuf::from("usr/share/applications").join(desktop_file_name);
let dest_path = PathBuf::from("/").join(&path);
let file_path = data_dir.join(&path);
Expand Down
4 changes: 2 additions & 2 deletions crates/tauri-bundler/src/bundle/linux/rpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {

// Add resources
if settings.resource_files().count() > 0 {
let resource_dir = Path::new("/usr/lib").join(settings.main_binary_name());
let resource_dir = Path::new("/usr/lib").join(settings.product_name());
// Create an empty file, needed to add a directory to the RPM package
// (cf https://github.com/rpm-rs/rpm/issues/177)
let empty_file_path = &package_dir.join("empty");
File::create(empty_file_path)?;
// Then add the resource directory `/usr/lib/<binary_name>` to the package.
// Then add the resource directory `/usr/lib/<product_name>` to the package.
builder = builder.with_file(
empty_file_path,
FileOptions::new(resource_dir.to_string_lossy()).mode(FileMode::Dir { permissions: 0o755 }),
Expand Down
14 changes: 7 additions & 7 deletions crates/tauri-bundler/src/bundle/linux/templates/appimage
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ else
linuxdeploy_arch="$ARCH"
fi

mkdir -p "{{crate_name}}.AppDir"
cp -r ../appimage_deb/data/usr "{{crate_name}}.AppDir"
mkdir -p "{{product_name}}.AppDir"
cp -r ../appimage_deb/data/usr "{{product_name}}.AppDir"

cd "{{crate_name}}.AppDir"
cd "{{product_name}}.AppDir"
mkdir -p "usr/bin"
mkdir -p "usr/lib"

Expand Down Expand Up @@ -56,14 +56,14 @@ find -L /usr/lib* -name libwebkit2gtkinjectedbundle.so -exec mkdir -p "$(dirname
( cd "{{tauri_tools_path}}" && ( wget -q -4 -N https://github.com/AppImage/AppImageKit/releases/download/continuous/AppRun-${ARCH} || wget -q -4 -N https://github.com/AppImage/AppImageKit/releases/download/12/AppRun-${ARCH} ) )
chmod +x "{{tauri_tools_path}}/AppRun-${ARCH}"

# We need AppRun to be installed as {{crate_name}}.AppDir/AppRun.
# We need AppRun to be installed as {{product_name}}.AppDir/AppRun.
# Otherwise the linuxdeploy scripts will default to symlinking our main bin instead and will crash on trying to launch.
cp "{{tauri_tools_path}}/AppRun-${ARCH}" AppRun

cp "{{icon_path}}" .DirIcon
ln -sf "{{icon_path}}" "{{crate_name}}.png"
ln -sf "{{icon_path}}" "{{product_name}}.png"

ln -sf "usr/share/applications/{{crate_name}}.desktop" "{{crate_name}}.desktop"
ln -sf "usr/share/applications/{{product_name}}.desktop" "{{product_name}}.desktop"

cd ..

Expand All @@ -83,4 +83,4 @@ chmod +x "{{tauri_tools_path}}/linuxdeploy-${linuxdeploy_arch}.AppImage"

dd if=/dev/zero bs=1 count=3 seek=8 conv=notrunc of="{{tauri_tools_path}}/linuxdeploy-${linuxdeploy_arch}.AppImage"

OUTPUT="{{appimage_filename}}" "{{tauri_tools_path}}/linuxdeploy-${linuxdeploy_arch}.AppImage" --appimage-extract-and-run --appdir "{{crate_name}}.AppDir" --plugin gtk ${gst_plugin} --output appimage
OUTPUT="{{appimage_filename}}" "{{tauri_tools_path}}/linuxdeploy-${linuxdeploy_arch}.AppImage" --appimage-extract-and-run --appdir "{{product_name}}.AppDir" --plugin gtk ${gst_plugin} --output appimage
2 changes: 1 addition & 1 deletion crates/tauri-bundler/src/bundle/macos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn create_info_plist(
plist.insert("CFBundleDisplayName".into(), settings.product_name().into());
plist.insert(
"CFBundleExecutable".into(),
settings.main_binary_name().into(),
settings.main_binary_name()?.into(),
);
if let Some(path) = bundle_icon_file {
plist.insert(
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-bundler/src/bundle/macos/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn generate_info_plist(
writeln!(
file,
" <key>CFBundleExecutable</key>\n <string>{}</string>",
settings.main_binary_name()
settings.main_binary_name()?
)?;
writeln!(
file,
Expand Down
43 changes: 27 additions & 16 deletions crates/tauri-bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use super::category::AppCategory;
use crate::bundle::{common, platform::target_triple};
use anyhow::Context;
pub use tauri_utils::config::WebviewInstallMode;
use tauri_utils::{
config::{BundleType, DeepLinkProtocol, FileAssociation, NSISInstallerMode, NsisCompression},
Expand Down Expand Up @@ -617,17 +618,17 @@ pub struct BundleSettings {
#[derive(Clone, Debug)]
pub struct BundleBinary {
name: String,
src_path: Option<String>,
main: bool,
src_path: Option<String>,
}

impl BundleBinary {
/// Creates a new bundle binary.
pub fn new(name: String, main: bool) -> Self {
Self {
name,
src_path: None,
main,
src_path: None,
}
}

Expand All @@ -640,13 +641,6 @@ impl BundleBinary {
}
}

/// Sets the src path of the binary.
#[must_use]
pub fn set_src_path(mut self, src_path: Option<String>) -> Self {
self.src_path = src_path;
self
}

/// Mark the binary as the main executable.
pub fn set_main(&mut self, main: bool) {
self.main = main;
Expand All @@ -657,16 +651,23 @@ impl BundleBinary {
self.name = name;
}

/// Returns the binary name.
pub fn name(&self) -> &str {
&self.name
/// Sets the src path of the binary.
#[must_use]
pub fn set_src_path(mut self, src_path: Option<String>) -> Self {
self.src_path = src_path;
self
}

/// Returns the binary `main` flag.
pub fn main(&self) -> bool {
self.main
}

/// Returns the binary name.
pub fn name(&self) -> &str {
&self.name
}

/// Returns the binary source path.
pub fn src_path(&self) -> Option<&String> {
self.src_path.as_ref()
Expand Down Expand Up @@ -852,14 +853,24 @@ impl Settings {
}

/// Returns the file name of the binary being bundled.
pub fn main_binary_name(&self) -> &str {
pub fn main_binary(&self) -> crate::Result<&BundleBinary> {
self
.binaries
.iter()
.find(|bin| bin.main)
.context("failed to find main binary, make sure you have a `package > default-run` in the Cargo.toml file")
.map_err(Into::into)
}

/// Returns the file name of the binary being bundled.
pub fn main_binary_name(&self) -> crate::Result<&str> {
self
.binaries
.iter()
.find(|bin| bin.main)
.expect("failed to find main binary")
.name
.as_str()
.context("failed to find main binary, make sure you have a `package > default-run` in the Cargo.toml file")
.map(|b| b.name.as_str())
.map_err(Into::into)
}

/// Returns the path to the specified binary.
Expand Down
25 changes: 8 additions & 17 deletions crates/tauri-bundler/src/bundle/windows/msi/wix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,7 @@ fn run_candle(
}
};

let main_binary = settings
.binaries()
.iter()
.find(|bin| bin.main())
.ok_or_else(|| anyhow::anyhow!("Failed to get main binary"))?;
let main_binary = settings.main_binary()?;

let mut args = vec![
"-arch".to_string(),
Expand Down Expand Up @@ -404,13 +400,6 @@ pub fn build_wix_app_installer(
// target only supports x64.
log::info!("Target: {}", arch);

let main_binary = settings
.binaries()
.iter()
.find(|bin| bin.main())
.ok_or_else(|| anyhow::anyhow!("Failed to get main binary"))?;
let app_exe_source = settings.binary_path(main_binary);

let output_path = settings.project_out_directory().join("wix").join(arch);

if output_path.exists() {
Expand Down Expand Up @@ -546,10 +535,6 @@ pub fn build_wix_app_installer(
let shortcut_guid = generate_package_guid(settings).to_string();
data.insert("shortcut_guid", to_json(shortcut_guid.as_str()));

// Note: `main_binary_name` is not used in our template but we keep it as it is potentially useful for custom temples
let main_binary_name = settings.main_binary_name().to_string();
data.insert("main_binary_name", to_json(&main_binary_name));

let binaries = generate_binaries_data(settings)?;

let binaries_json = to_json(binaries);
Expand All @@ -572,7 +557,13 @@ pub fn build_wix_app_installer(
let merge_modules = get_merge_modules(settings)?;
data.insert("merge_modules", to_json(merge_modules));

data.insert("app_exe_source", to_json(app_exe_source));
// Note: `main_binary_name` is not used in our template but we keep it as it is potentially useful for custom temples
let main_binary_name = settings.main_binary_name()?;
data.insert("main_binary_name", to_json(main_binary_name));

let main_binary = settings.main_binary()?;
let main_binary_path = settings.binary_path(main_binary);
data.insert("main_binary_path", to_json(main_binary_path));

// copy icon from `settings.windows().icon_path` folder to resource folder near msi
let icon_path = copy_icon(settings, "icon.ico", &settings.windows().icon_path)?;
Expand Down
16 changes: 2 additions & 14 deletions crates/tauri-bundler/src/bundle/windows/nsis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,9 @@ fn build_nsis_app_installer(
}
data.insert("language_files", to_json(language_files_paths));

let main_binary = settings
.binaries()
.iter()
.find(|bin| bin.main())
.ok_or_else(|| anyhow::anyhow!("Failed to get main binary"))?;
let main_binary = settings.main_binary()?;
let main_binary_path = settings.binary_path(main_binary).with_extension("exe");
data.insert(
"main_binary_name",
to_json(
main_binary_path
.file_stem()
.and_then(|file_name| file_name.to_str())
.unwrap_or_else(|| main_binary.name()),
),
);
data.insert("main_binary_name", to_json(main_binary.name()));
data.insert("main_binary_path", to_json(&main_binary_path));

let out_file = "nsis-output.exe";
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-bundler/src/bundle/windows/templates/main.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
{{/each~}}
</Component>
<Component Id="Path" Guid="{{path_component_guid}}" Win64="$(var.Win64)">
<File Id="Path" Source="{{app_exe_source}}" KeyPath="yes" Checksum="yes"/>
<File Id="Path" Source="{{main_binary_path}}" KeyPath="yes" Checksum="yes"/>
{{#each file_associations as |association| ~}}
{{#each association.ext as |ext| ~}}
<ProgId Id="{{../../product_name}}.{{ext}}" Advertise="yes" Description="{{association.description}}">
Expand Down
7 changes: 7 additions & 0 deletions crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
],
"pattern": "^[^/\\:*?\"<>|]+$"
},
"mainBinaryName": {
"description": "App main binary filename. Defaults to the name of your cargo crate.",
"type": [
"string",
"null"
]
},
"version": {
"description": "App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used.\n\n By default version 1.0 is used on Android.",
"type": [
Expand Down
9 changes: 4 additions & 5 deletions crates/tauri-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
app_paths::tauri_dir,
config::{get as get_config, ConfigHandle, FrontendDist},
},
interface::{AppInterface, AppSettings, Interface},
interface::{AppInterface, Interface},
ConfigValue, Result,
};
use anyhow::Context;
Expand Down Expand Up @@ -83,10 +83,9 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
let app_settings = interface.app_settings();
let interface_options = options.clone().into();

let bin_path = app_settings.app_binary_path(&interface_options)?;
let out_dir = bin_path.parent().unwrap();
let out_dir = app_settings.out_dir(&interface_options)?;

interface.build(interface_options)?;
let bin_path = interface.build(interface_options)?;

log::info!(action ="Built"; "application at: {}", tauri_utils::display_path(&bin_path));

Expand All @@ -100,7 +99,7 @@ pub fn command(mut options: Options, verbosity: u8) -> Result<()> {
&interface,
&app_settings,
config_,
out_dir,
&out_dir,
)?;
}

Expand Down
5 changes: 2 additions & 3 deletions crates/tauri-cli/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ pub fn command(options: Options, verbosity: u8) -> crate::Result<()> {
let app_settings = interface.app_settings();
let interface_options = options.clone().into();

let bin_path = app_settings.app_binary_path(&interface_options)?;
let out_dir = bin_path.parent().unwrap();
let out_dir = app_settings.out_dir(&interface_options)?;

bundle(
&options,
Expand All @@ -131,7 +130,7 @@ pub fn command(options: Options, verbosity: u8) -> crate::Result<()> {
&interface,
&app_settings,
config_,
out_dir,
&out_dir,
)
}

Expand Down
Loading

0 comments on commit 35bd9dd

Please sign in to comment.