浏览代码

feat: Add files field in macos build config, closes #3290 (#7798)

* Add appContents field in macos tauri config.

* Change MacConfig::appContents to MacConfig::files to make it similar to DebConfig::files.

* Change appContents to files in helloworld/tauri.conf.json

* use common::copy_dir helper

* add change files [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Palash Bansal 1 年之前
父节点
当前提交
27bad32d4d

+ 7 - 0
.changes/app-bundle-files-config.md

@@ -0,0 +1,7 @@
+---
+"tauri-bundler": patch:feat
+"tauri-cli": patch:feat
+"@tauri-apps/cli": patch:feat
+---
+
+Add `files` object on the `tauri > bundle > macOS` configuration option.

+ 5 - 0
.changes/app-bundle-files.md

@@ -0,0 +1,5 @@
+---
+"tauri-bundler": patch:feat
+---
+
+Add `files` map on the `MacOsSettings` struct to add custom files to the `.app` bundle.

+ 11 - 0
core/tauri-config-schema/schema.json

@@ -55,6 +55,7 @@
           "icon": [],
           "identifier": "",
           "macOS": {
+            "files": {},
             "minimumSystemVersion": "10.13"
           },
           "rpm": {
@@ -208,6 +209,7 @@
             "icon": [],
             "identifier": "",
             "macOS": {
+              "files": {},
               "minimumSystemVersion": "10.13"
             },
             "rpm": {
@@ -1079,6 +1081,7 @@
         "macOS": {
           "description": "Configuration for the macOS bundles.",
           "default": {
+            "files": {},
             "minimumSystemVersion": "10.13"
           },
           "allOf": [
@@ -1570,6 +1573,14 @@
             "type": "string"
           }
         },
+        "files": {
+          "description": "The files to include in the application relative to the Contents directory.",
+          "default": {},
+          "type": "object",
+          "additionalProperties": {
+            "type": "string"
+          }
+        },
         "minimumSystemVersion": {
           "description": "A version string indicating the minimum macOS X version that the bundled application supports. Defaults to `10.13`.\n\nSetting it to `null` completely removes the `LSMinimumSystemVersion` field on the bundle's `Info.plist` and the `MACOSX_DEPLOYMENT_TARGET` environment variable.\n\nAn empty string is considered an invalid value so the default value is used.",
           "default": "10.13",

+ 4 - 0
core/tauri-utils/src/config.rs

@@ -427,6 +427,9 @@ pub struct MacConfig {
   ///
   /// If a name is used, ".framework" must be omitted and it will look for standard install locations. You may also use a path to a specific framework.
   pub frameworks: Option<Vec<String>>,
+  /// The files to include in the application relative to the Contents directory.
+  #[serde(default)]
+  pub files: HashMap<PathBuf, PathBuf>,
   /// A version string indicating the minimum macOS X version that the bundled application supports. Defaults to `10.13`.
   ///
   /// Setting it to `null` completely removes the `LSMinimumSystemVersion` field on the bundle's `Info.plist`
@@ -459,6 +462,7 @@ impl Default for MacConfig {
   fn default() -> Self {
     Self {
       frameworks: None,
+      files: HashMap::new(),
       minimum_system_version: minimum_system_version(),
       exception_domain: None,
       license: None,

+ 2 - 1
examples/helloworld/tauri.conf.json

@@ -37,6 +37,7 @@
       },
       "macOS": {
         "frameworks": [],
+        "files": {},
         "exceptionDomain": ""
       }
     },
@@ -53,4 +54,4 @@
       "csp": "default-src 'self'; connect-src ipc: http://ipc.localhost"
     }
   }
-}
+}

+ 1 - 8
tooling/bundler/src/bundle/linux/debian.rs

@@ -216,14 +216,7 @@ fn copy_custom_files(settings: &Settings, data_dir: &Path) -> crate::Result<()>
     if path.is_file() {
       common::copy_file(path, data_dir.join(deb_path))?;
     } else {
-      let out_dir = data_dir.join(deb_path);
-      for entry in walkdir::WalkDir::new(path) {
-        let entry_path = entry?.into_path();
-        if entry_path.is_file() {
-          let without_prefix = entry_path.strip_prefix(path).unwrap();
-          common::copy_file(&entry_path, out_dir.join(without_prefix))?;
-        }
-      }
+      common::copy_dir(path, &data_dir.join(deb_path))?;
     }
   }
   Ok(())

+ 21 - 0
tooling/bundler/src/bundle/macos/app.rs

@@ -106,6 +106,8 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
     is_an_executable: true,
   }));
 
+  copy_custom_files_to_bundle(&bundle_directory, settings)?;
+
   if let Some(identity) = &settings.macos().signing_identity {
     // Sign frameworks and sidecar binaries first, per apple, signing must be done inside out
     // https://developer.apple.com/forums/thread/701514
@@ -165,6 +167,25 @@ fn copy_binaries_to_bundle(
   Ok(paths)
 }
 
+/// Copies user-defined files to the app under Contents.
+fn copy_custom_files_to_bundle(bundle_directory: &Path, settings: &Settings) -> crate::Result<()> {
+  for (contents_path, path) in settings.macos().files.iter() {
+    let contents_path = if contents_path.is_absolute() {
+      contents_path.strip_prefix("/").unwrap()
+    } else {
+      contents_path
+    };
+    if path.is_file() {
+      common::copy_file(path, bundle_directory.join(contents_path))
+        .with_context(|| format!("Failed to copy file {:?} to {:?}", path, contents_path))?;
+    } else {
+      common::copy_dir(path, &bundle_directory.join(contents_path))
+        .with_context(|| format!("Failed to copy directory {:?} to {:?}", path, contents_path))?;
+    }
+  }
+  Ok(())
+}
+
 // Creates the Info.plist file.
 fn create_info_plist(
   bundle_dir: &Path,

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

@@ -258,6 +258,9 @@ pub struct MacOsSettings {
   ///
   /// - embedding the correct rpath in your binary (e.g. by running `install_name_tool -add_rpath "@executable_path/../Frameworks" path/to/binary` after compiling)
   pub frameworks: Option<Vec<String>>,
+  /// List of custom files to add to the application bundle.
+  /// Maps the path in the Contents directory in the app to the path of the file to include (relative to the current working directory).
+  pub files: HashMap<PathBuf, PathBuf>,
   /// A version string indicating the minimum MacOS version that the bundled app supports (e.g. `"10.11"`).
   /// If you are using this config field, you may also want have your `build.rs` script emit `cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11`.
   pub minimum_system_version: Option<String>,

+ 11 - 0
tooling/cli/schema.json

@@ -55,6 +55,7 @@
           "icon": [],
           "identifier": "",
           "macOS": {
+            "files": {},
             "minimumSystemVersion": "10.13"
           },
           "rpm": {
@@ -208,6 +209,7 @@
             "icon": [],
             "identifier": "",
             "macOS": {
+              "files": {},
               "minimumSystemVersion": "10.13"
             },
             "rpm": {
@@ -1079,6 +1081,7 @@
         "macOS": {
           "description": "Configuration for the macOS bundles.",
           "default": {
+            "files": {},
             "minimumSystemVersion": "10.13"
           },
           "allOf": [
@@ -1570,6 +1573,14 @@
             "type": "string"
           }
         },
+        "files": {
+          "description": "The files to include in the application relative to the Contents directory.",
+          "default": {},
+          "type": "object",
+          "additionalProperties": {
+            "type": "string"
+          }
+        },
         "minimumSystemVersion": {
           "description": "A version string indicating the minimum macOS X version that the bundled application supports. Defaults to `10.13`.\n\nSetting it to `null` completely removes the `LSMinimumSystemVersion` field on the bundle's `Info.plist` and the `MACOSX_DEPLOYMENT_TARGET` environment variable.\n\nAn empty string is considered an invalid value so the default value is used.",
           "default": "10.13",

+ 1 - 0
tooling/cli/src/interface/rust.rs

@@ -1224,6 +1224,7 @@ fn tauri_config_to_bundle_settings(
     },
     macos: MacOsSettings {
       frameworks: config.macos.frameworks,
+      files: config.macos.files,
       minimum_system_version: config.macos.minimum_system_version,
       license: config.macos.license,
       exception_domain: config.macos.exception_domain,

+ 1 - 0
tooling/cli/templates/tauri.conf.json

@@ -32,6 +32,7 @@
       },
       "macOS": {
         "frameworks": [],
+        "files": {},
         "exceptionDomain": "",
         "signingIdentity": null,
         "providerShortName": null,