Skip to content

Commit 35cd751

Browse files
snorkysnarkFabianLarslucasfernog
authored
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>
1 parent ff5e4db commit 35cd751

File tree

9 files changed

+93
-13
lines changed

9 files changed

+93
-13
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-utils": patch
3+
"tauri-cli": patch
4+
"@tauri-apps/cli": patch
5+
---
6+
7+
Added the `desktop_template` option on `tauri.conf.json > tauri > bundle > deb`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": "minor:feat"
3+
---
4+
5+
Added `desktop_template` option on `DebianSettings`.

core/tauri-config-schema/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,13 @@
12701270
"additionalProperties": {
12711271
"type": "string"
12721272
}
1273+
},
1274+
"desktopTemplate": {
1275+
"description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
1276+
"type": [
1277+
"string",
1278+
"null"
1279+
]
12731280
}
12741281
},
12751282
"additionalProperties": false

core/tauri-utils/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ pub struct DebConfig {
276276
/// The files to include on the package.
277277
#[serde(default)]
278278
pub files: HashMap<PathBuf, PathBuf>,
279+
/// Path to a custom desktop file Handlebars template.
280+
///
281+
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
282+
pub desktop_template: Option<PathBuf>,
279283
}
280284

281285
fn de_minimum_system_version<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>

tooling/bundler/src/bundle/linux/debian.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@
2626
use super::super::common;
2727
use crate::Settings;
2828
use anyhow::Context;
29+
use handlebars::Handlebars;
2930
use heck::AsKebabCase;
3031
use image::{self, codecs::png::PngDecoder, ImageDecoder};
3132
use libflate::gzip;
3233
use log::info;
34+
use serde::Serialize;
3335
use walkdir::WalkDir;
3436

3537
use std::{
3638
collections::BTreeSet,
3739
ffi::OsStr,
38-
fs::{self, File},
40+
fs::{self, read_to_string, File},
3941
io::{self, Write},
4042
path::{Path, PathBuf},
4143
};
@@ -141,23 +143,51 @@ fn generate_desktop_file(settings: &Settings, data_dir: &Path) -> crate::Result<
141143
let desktop_file_path = data_dir
142144
.join("usr/share/applications")
143145
.join(desktop_file_name);
144-
let file = &mut common::create_file(&desktop_file_path)?;
146+
145147
// For more information about the format of this file, see
146148
// https://developer.gnome.org/integration-guide/stable/desktop-files.html.en
147-
writeln!(file, "[Desktop Entry]")?;
148-
if let Some(category) = settings.app_category() {
149-
writeln!(file, "Categories={}", category.gnome_desktop_categories())?;
149+
let file = &mut common::create_file(&desktop_file_path)?;
150+
151+
let mut handlebars = Handlebars::new();
152+
handlebars.register_escape_fn(handlebars::no_escape);
153+
if let Some(template) = &settings.deb().desktop_template {
154+
handlebars
155+
.register_template_string("main.desktop", read_to_string(template)?)
156+
.with_context(|| "Failed to setup custom handlebar template")?;
150157
} else {
151-
writeln!(file, "Categories=")?;
158+
handlebars
159+
.register_template_string("main.desktop", include_str!("./templates/main.desktop"))
160+
.with_context(|| "Failed to setup custom handlebar template")?;
152161
}
153-
if !settings.short_description().is_empty() {
154-
writeln!(file, "Comment={}", settings.short_description())?;
162+
163+
#[derive(Serialize)]
164+
struct DesktopTemplateParams<'a> {
165+
categories: &'a str,
166+
comment: Option<&'a str>,
167+
exec: &'a str,
168+
icon: &'a str,
169+
name: &'a str,
155170
}
156-
writeln!(file, "Exec={}", bin_name)?;
157-
writeln!(file, "Icon={}", bin_name)?;
158-
writeln!(file, "Name={}", settings.product_name())?;
159-
writeln!(file, "Terminal=false")?;
160-
writeln!(file, "Type=Application")?;
171+
172+
handlebars.render_to_write(
173+
"main.desktop",
174+
&DesktopTemplateParams {
175+
categories: settings
176+
.app_category()
177+
.map(|app_category| app_category.gnome_desktop_categories())
178+
.unwrap_or(""),
179+
comment: if !settings.short_description().is_empty() {
180+
Some(settings.short_description())
181+
} else {
182+
None
183+
},
184+
exec: bin_name,
185+
icon: bin_name,
186+
name: settings.product_name(),
187+
},
188+
file,
189+
)?;
190+
161191
Ok(())
162192
}
163193

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Desktop Entry]
2+
Categories={{categories}}
3+
{{#if comment}}
4+
Comment={{comment}}
5+
{{/if}}
6+
Exec={{exec}}
7+
Icon={{icon}}
8+
Name={{name}}
9+
Terminal=false
10+
Type=Application

tooling/bundler/src/bundle/settings.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ pub struct DebianSettings {
156156
/// List of custom files to add to the deb package.
157157
/// Maps the path on the debian package to the path of the file to include (relative to the current working directory).
158158
pub files: HashMap<PathBuf, PathBuf>,
159+
/// Path to a custom desktop file Handlebars template.
160+
///
161+
/// Available variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.
162+
///
163+
/// Default file contents:
164+
/// ```text
165+
#[doc = include_str!("./linux/templates/main.desktop")]
166+
/// ```
167+
pub desktop_template: Option<PathBuf>,
159168
}
160169

161170
/// The macOS bundle settings.

tooling/cli/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,13 @@
12701270
"additionalProperties": {
12711271
"type": "string"
12721272
}
1273+
},
1274+
"desktopTemplate": {
1275+
"description": "Path to a custom desktop file Handlebars template.\n\nAvailable variables: `categories`, `comment` (optional), `exec`, `icon` and `name`.",
1276+
"type": [
1277+
"string",
1278+
"null"
1279+
]
12731280
}
12741281
},
12751282
"additionalProperties": false

tooling/cli/src/interface/rust.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ fn tauri_config_to_bundle_settings(
10671067
Some(depends)
10681068
},
10691069
files: config.deb.files,
1070+
desktop_template: config.deb.desktop_template,
10701071
},
10711072
macos: MacOsSettings {
10721073
frameworks: config.macos.frameworks,

0 commit comments

Comments
 (0)