Browse Source

feat(bundler): Add `files` option to the AppImage Configuration. (#8720)

* Add `files` option to Appimage

* Add .changes file
Naman Garg 1 năm trước cách đây
mục cha
commit
d6c7568c27

+ 7 - 0
.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.

+ 14 - 3
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

+ 3 - 0
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<PathBuf, PathBuf>,
 }
 
 /// Configuration for Debian (.deb) bundles.

+ 3 - 2
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")]

+ 28 - 1
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<PathBuf, PathBuf>,
+  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.

+ 8 - 2
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<Vec<PathBuf>> {
   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() {

+ 2 - 18
tooling/bundler/src/bundle/linux/debian.rs

@@ -68,7 +68,8 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
 
   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<P: AsRef<Path>>(path: P, data: &str) -> crate::Result<()> {

+ 14 - 0
tooling/bundler/src/bundle/settings.rs

@@ -186,6 +186,13 @@ pub struct DebianSettings {
   pub desktop_template: Option<PathBuf>,
 }
 
+/// The Linux AppImage bundle settings.
+#[derive(Clone, Debug, Default)]
+pub struct AppImageSettings {
+  /// The files to include in the Appimage Binary.
+  pub files: HashMap<PathBuf, PathBuf>,
+}
+
 /// The RPM bundle settings.
 #[derive(Clone, Debug, Default)]
 pub struct RpmSettings {
@@ -482,6 +489,8 @@ pub struct BundleSettings {
   pub deep_link_protocols: Option<Vec<DeepLinkProtocol>>,
   /// 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

+ 14 - 3
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

+ 5 - 2
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() {