Selaa lähdekoodia

feat(bundler): custom desktop file template, closes #5176 (#5180)

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Francis The Basilisk 2 vuotta sitten
vanhempi
sitoutus
35cd751adc

+ 7 - 0
.changes/deb-custom-desktop-file-config.md

@@ -0,0 +1,7 @@
+---
+"tauri-utils": patch
+"tauri-cli": patch
+"@tauri-apps/cli": patch
+---
+
+Added the `desktop_template` option on `tauri.conf.json > tauri > bundle > deb`.

+ 5 - 0
.changes/deb-custom-desktop-file.md

@@ -0,0 +1,5 @@
+---
+"tauri-bundler": "minor:feat"
+---
+
+Added `desktop_template` option on `DebianSettings`.

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

@@ -1270,6 +1270,13 @@
           "additionalProperties": {
             "type": "string"
           }
+        },
+        "desktopTemplate": {
+          "description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
+          "type": [
+            "string",
+            "null"
+          ]
         }
       },
       "additionalProperties": false

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

@@ -276,6 +276,10 @@ pub struct DebConfig {
   /// The files to include on the package.
   #[serde(default)]
   pub files: HashMap<PathBuf, PathBuf>,
+  /// Path to a custom desktop file Handlebars template.
+  ///
+  /// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
+  pub desktop_template: Option<PathBuf>,
 }
 
 fn de_minimum_system_version<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>

+ 43 - 13
tooling/bundler/src/bundle/linux/debian.rs

@@ -26,16 +26,18 @@
 use super::super::common;
 use crate::Settings;
 use anyhow::Context;
+use handlebars::Handlebars;
 use heck::AsKebabCase;
 use image::{self, codecs::png::PngDecoder, ImageDecoder};
 use libflate::gzip;
 use log::info;
+use serde::Serialize;
 use walkdir::WalkDir;
 
 use std::{
   collections::BTreeSet,
   ffi::OsStr,
-  fs::{self, File},
+  fs::{self, read_to_string, File},
   io::{self, Write},
   path::{Path, PathBuf},
 };
@@ -141,23 +143,51 @@ fn generate_desktop_file(settings: &Settings, data_dir: &Path) -> crate::Result<
   let desktop_file_path = data_dir
     .join("usr/share/applications")
     .join(desktop_file_name);
-  let file = &mut common::create_file(&desktop_file_path)?;
+
   // For more information about the format of this file, see
   // https://developer.gnome.org/integration-guide/stable/desktop-files.html.en
-  writeln!(file, "[Desktop Entry]")?;
-  if let Some(category) = settings.app_category() {
-    writeln!(file, "Categories={}", category.gnome_desktop_categories())?;
+  let file = &mut common::create_file(&desktop_file_path)?;
+
+  let mut handlebars = Handlebars::new();
+  handlebars.register_escape_fn(handlebars::no_escape);
+  if let Some(template) = &settings.deb().desktop_template {
+    handlebars
+      .register_template_string("main.desktop", read_to_string(template)?)
+      .with_context(|| "Failed to setup custom handlebar template")?;
   } else {
-    writeln!(file, "Categories=")?;
+    handlebars
+      .register_template_string("main.desktop", include_str!("./templates/main.desktop"))
+      .with_context(|| "Failed to setup custom handlebar template")?;
   }
-  if !settings.short_description().is_empty() {
-    writeln!(file, "Comment={}", settings.short_description())?;
+
+  #[derive(Serialize)]
+  struct DesktopTemplateParams<'a> {
+    categories: &'a str,
+    comment: Option<&'a str>,
+    exec: &'a str,
+    icon: &'a str,
+    name: &'a str,
   }
-  writeln!(file, "Exec={}", bin_name)?;
-  writeln!(file, "Icon={}", bin_name)?;
-  writeln!(file, "Name={}", settings.product_name())?;
-  writeln!(file, "Terminal=false")?;
-  writeln!(file, "Type=Application")?;
+
+  handlebars.render_to_write(
+    "main.desktop",
+    &DesktopTemplateParams {
+      categories: settings
+        .app_category()
+        .map(|app_category| app_category.gnome_desktop_categories())
+        .unwrap_or(""),
+      comment: if !settings.short_description().is_empty() {
+        Some(settings.short_description())
+      } else {
+        None
+      },
+      exec: bin_name,
+      icon: bin_name,
+      name: settings.product_name(),
+    },
+    file,
+  )?;
+
   Ok(())
 }
 

+ 10 - 0
tooling/bundler/src/bundle/linux/templates/main.desktop

@@ -0,0 +1,10 @@
+[Desktop Entry]
+Categories={{categories}}
+{{#if comment}}
+Comment={{comment}}
+{{/if}}
+Exec={{exec}}
+Icon={{icon}}
+Name={{name}}
+Terminal=false
+Type=Application

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

@@ -156,6 +156,15 @@ pub struct DebianSettings {
   /// List of custom files to add to the deb package.
   /// Maps the path on the debian package to the path of the file to include (relative to the current working directory).
   pub files: HashMap<PathBuf, PathBuf>,
+  /// Path to a custom desktop file Handlebars template.
+  ///
+  /// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
+  ///
+  /// Default file contents:
+  /// ```text
+  #[doc = include_str!("./linux/templates/main.desktop")]
+  /// ```
+  pub desktop_template: Option<PathBuf>,
 }
 
 /// The macOS bundle settings.

+ 7 - 0
tooling/cli/schema.json

@@ -1270,6 +1270,13 @@
           "additionalProperties": {
             "type": "string"
           }
+        },
+        "desktopTemplate": {
+          "description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
+          "type": [
+            "string",
+            "null"
+          ]
         }
       },
       "additionalProperties": false

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

@@ -1067,6 +1067,7 @@ fn tauri_config_to_bundle_settings(
         Some(depends)
       },
       files: config.deb.files,
+      desktop_template: config.deb.desktop_template,
     },
     macos: MacOsSettings {
       frameworks: config.macos.frameworks,