From d6c7568c27445653edf570f3969163bc358ba2ba Mon Sep 17 00:00:00 2001 From: Naman Garg <155433377+naman-crabnebula@users.noreply.github.com> Date: Thu, 1 Feb 2024 06:23:26 +0530 Subject: [PATCH] feat(bundler): Add `files` option to the AppImage Configuration. (#8720) * Add `files` option to Appimage * Add .changes file --- .changes/add-files-appimage.md | 7 +++++ core/tauri-config-schema/schema.json | 17 ++++++++++-- core/tauri-utils/src/config.rs | 3 ++ tooling/bundler/src/bundle.rs | 5 ++-- tooling/bundler/src/bundle/common.rs | 29 +++++++++++++++++++- tooling/bundler/src/bundle/linux/appimage.rs | 10 +++++-- tooling/bundler/src/bundle/linux/debian.rs | 20 ++------------ tooling/bundler/src/bundle/settings.rs | 14 ++++++++++ tooling/cli/schema.json | 17 ++++++++++-- tooling/cli/src/interface/rust.rs | 7 +++-- 10 files changed, 98 insertions(+), 31 deletions(-) create mode 100644 .changes/add-files-appimage.md diff --git a/.changes/add-files-appimage.md b/.changes/add-files-appimage.md new file mode 100644 index 000000000..6172126e6 --- /dev/null +++ b/.changes/add-files-appimage.md @@ -0,0 +1,7 @@ +--- +"tauri-bundler": 'patch:enhance' +"tauri-cli": 'patch:enhance' +"@tauri-apps/cli": 'patch:enhance' +--- + +Add `files` option to the AppImage Configuration. diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 075804ac4..7dda9adf1 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -32,7 +32,8 @@ "minSdkVersion": 24 }, "appimage": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "deb": { "files": {} @@ -183,7 +184,8 @@ "minSdkVersion": 24 }, "appimage": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "deb": { "files": {} @@ -1032,7 +1034,8 @@ "appimage": { "description": "Configuration for the AppImage bundle.", "default": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "allOf": [ { @@ -1373,6 +1376,14 @@ "description": "Include additional gstreamer dependencies needed for audio and video playback. This increases the bundle size by ~15-35MB depending on your build system.", "default": false, "type": "boolean" + }, + "files": { + "description": "The files to include in the Appimage Binary.", + "default": {}, + "type": "object", + "additionalProperties": { + "type": "string" + } } }, "additionalProperties": false diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 75f7855f6..7fb2b3c46 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -292,6 +292,9 @@ pub struct AppImageConfig { /// This increases the bundle size by ~15-35MB depending on your build system. #[serde(default, alias = "bundle-media-framework")] pub bundle_media_framework: bool, + /// The files to include in the Appimage Binary. + #[serde(default)] + pub files: HashMap, } /// Configuration for Debian (.deb) bundles. diff --git a/tooling/bundler/src/bundle.rs b/tooling/bundler/src/bundle.rs index 89d9948c7..6aefe6d34 100644 --- a/tooling/bundler/src/bundle.rs +++ b/tooling/bundler/src/bundle.rs @@ -20,8 +20,9 @@ use tauri_utils::display_path; pub use self::{ category::AppCategory, settings::{ - BundleBinary, BundleSettings, DebianSettings, DmgSettings, MacOsSettings, PackageSettings, - PackageType, Position, RpmSettings, Settings, SettingsBuilder, Size, UpdaterSettings, + AppImageSettings, BundleBinary, BundleSettings, DebianSettings, DmgSettings, MacOsSettings, + PackageSettings, PackageType, Position, RpmSettings, Settings, SettingsBuilder, Size, + UpdaterSettings, }, }; #[cfg(target_os = "macos")] diff --git a/tooling/bundler/src/bundle/common.rs b/tooling/bundler/src/bundle/common.rs index df430466d..f0a5e0335 100644 --- a/tooling/bundler/src/bundle/common.rs +++ b/tooling/bundler/src/bundle/common.rs @@ -6,10 +6,11 @@ use log::debug; use std::{ + collections::HashMap, ffi::OsStr, fs::{self, File}, io::{self, BufRead, BufReader, BufWriter}, - path::Path, + path::{Path, PathBuf}, process::{Command, ExitStatus, Output, Stdio}, sync::{Arc, Mutex}, }; @@ -128,6 +129,32 @@ pub fn copy_dir(from: &Path, to: &Path) -> crate::Result<()> { Ok(()) } +/// Copies user-defined files specified in the configuration file to the package. +/// +/// The configuration object maps the path in the package to the path of the file on the filesystem, +/// relative to the tauri.conf.json file. +/// +/// Expects a HashMap of PathBuf entries, representing destination and source paths, +/// and also a path of a directory. The files will be stored with respect to this directory. +pub fn copy_custom_files( + files_map: &HashMap, + data_dir: &Path, +) -> crate::Result<()> { + for (pkg_path, path) in files_map.iter() { + let pkg_path = if pkg_path.is_absolute() { + pkg_path.strip_prefix("/").unwrap() + } else { + pkg_path + }; + if path.is_file() { + copy_file(path, data_dir.join(pkg_path))?; + } else { + copy_dir(path, &data_dir.join(pkg_path))?; + } + } + Ok(()) +} + pub trait CommandExt { // The `pipe` function sets the stdout and stderr to properly // show the command output in the Node.js wrapper. diff --git a/tooling/bundler/src/bundle/linux/appimage.rs b/tooling/bundler/src/bundle/linux/appimage.rs index 57bec975c..7c084d601 100644 --- a/tooling/bundler/src/bundle/linux/appimage.rs +++ b/tooling/bundler/src/bundle/linux/appimage.rs @@ -4,7 +4,10 @@ // SPDX-License-Identifier: MIT use super::{ - super::{common::CommandExt, path_utils}, + super::{ + common::{self, CommandExt}, + path_utils, + }, debian, }; use crate::Settings; @@ -30,7 +33,10 @@ pub fn bundle_project(settings: &Settings) -> crate::Result> { let package_dir = settings.project_out_directory().join("bundle/appimage_deb"); // generate deb_folder structure - let (_, icons) = debian::generate_data(settings, &package_dir)?; + let (data_dir, icons) = debian::generate_data(settings, &package_dir) + .with_context(|| "Failed to build data folders and files")?; + common::copy_custom_files(&settings.deb().files, &data_dir) + .with_context(|| "Failed to copy custom files")?; let output_path = settings.project_out_directory().join("bundle/appimage"); if output_path.exists() { diff --git a/tooling/bundler/src/bundle/linux/debian.rs b/tooling/bundler/src/bundle/linux/debian.rs index 59361d694..87979f328 100644 --- a/tooling/bundler/src/bundle/linux/debian.rs +++ b/tooling/bundler/src/bundle/linux/debian.rs @@ -68,7 +68,8 @@ pub fn bundle_project(settings: &Settings) -> crate::Result> { let (data_dir, _) = generate_data(settings, &package_dir) .with_context(|| "Failed to build data folders and files")?; - copy_custom_files(settings, &data_dir).with_context(|| "Failed to copy custom files")?; + common::copy_custom_files(&settings.deb().files, &data_dir) + .with_context(|| "Failed to copy custom files")?; // Generate control files. let control_dir = package_dir.join("control"); @@ -205,23 +206,6 @@ fn copy_resource_files(settings: &Settings, data_dir: &Path) -> crate::Result<() settings.copy_resources(&resource_dir) } -/// Copies user-defined files to the deb package. -fn copy_custom_files(settings: &Settings, data_dir: &Path) -> crate::Result<()> { - for (deb_path, path) in settings.deb().files.iter() { - let deb_path = if deb_path.is_absolute() { - deb_path.strip_prefix("/").unwrap() - } else { - deb_path - }; - if path.is_file() { - common::copy_file(path, data_dir.join(deb_path))?; - } else { - common::copy_dir(path, &data_dir.join(deb_path))?; - } - } - Ok(()) -} - /// Create an empty file at the given path, creating any parent directories as /// needed, then write `data` into the file. fn create_file_with_data>(path: P, data: &str) -> crate::Result<()> { diff --git a/tooling/bundler/src/bundle/settings.rs b/tooling/bundler/src/bundle/settings.rs index 9baee51df..aadb25d21 100644 --- a/tooling/bundler/src/bundle/settings.rs +++ b/tooling/bundler/src/bundle/settings.rs @@ -186,6 +186,13 @@ pub struct DebianSettings { pub desktop_template: Option, } +/// The Linux AppImage bundle settings. +#[derive(Clone, Debug, Default)] +pub struct AppImageSettings { + /// The files to include in the Appimage Binary. + pub files: HashMap, +} + /// The RPM bundle settings. #[derive(Clone, Debug, Default)] pub struct RpmSettings { @@ -482,6 +489,8 @@ pub struct BundleSettings { pub deep_link_protocols: Option>, /// Debian-specific settings. pub deb: DebianSettings, + /// AppImage-specific settings. + pub appimage: AppImageSettings, /// Rpm-specific settings. pub rpm: RpmSettings, /// DMG-specific settings. @@ -931,6 +940,11 @@ impl Settings { &self.bundle_settings.deb } + /// Returns the appimage settings. + pub fn appimage(&self) -> &AppImageSettings { + &self.bundle_settings.appimage + } + /// Returns the RPM settings. pub fn rpm(&self) -> &RpmSettings { &self.bundle_settings.rpm diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 075804ac4..7dda9adf1 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -32,7 +32,8 @@ "minSdkVersion": 24 }, "appimage": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "deb": { "files": {} @@ -183,7 +184,8 @@ "minSdkVersion": 24 }, "appimage": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "deb": { "files": {} @@ -1032,7 +1034,8 @@ "appimage": { "description": "Configuration for the AppImage bundle.", "default": { - "bundleMediaFramework": false + "bundleMediaFramework": false, + "files": {} }, "allOf": [ { @@ -1373,6 +1376,14 @@ "description": "Include additional gstreamer dependencies needed for audio and video playback. This increases the bundle size by ~15-35MB depending on your build system.", "default": false, "type": "boolean" + }, + "files": { + "description": "The files to include in the Appimage Binary.", + "default": {}, + "type": "object", + "additionalProperties": { + "type": "string" + } } }, "additionalProperties": false diff --git a/tooling/cli/src/interface/rust.rs b/tooling/cli/src/interface/rust.rs index 941d67f52..e0a1a28e7 100644 --- a/tooling/cli/src/interface/rust.rs +++ b/tooling/cli/src/interface/rust.rs @@ -22,8 +22,8 @@ use notify::RecursiveMode; use notify_debouncer_mini::new_debouncer; use serde::Deserialize; use tauri_bundler::{ - AppCategory, BundleBinary, BundleSettings, DebianSettings, DmgSettings, MacOsSettings, - PackageSettings, Position, RpmSettings, Size, UpdaterSettings, WindowsSettings, + AppCategory, AppImageSettings, BundleBinary, BundleSettings, DebianSettings, DmgSettings, + MacOsSettings, PackageSettings, Position, RpmSettings, Size, UpdaterSettings, WindowsSettings, }; use tauri_utils::config::{parse::is_configuration_file, DeepLinkProtocol}; @@ -1223,6 +1223,9 @@ fn tauri_config_to_bundle_settings( files: config.deb.files, desktop_template: config.deb.desktop_template, }, + appimage: AppImageSettings { + files: config.appimage.files, + }, rpm: RpmSettings { license: config.rpm.license, depends: if depends_rpm.is_empty() {