Skip to content

Commit

Permalink
feat(bundler): custom desktop file template, closes #5176 (#5180)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
3 people authored May 24, 2023
1 parent ff5e4db commit 35cd751
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changes/deb-custom-desktop-file-config.md
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions .changes/deb-custom-desktop-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-bundler": "minor:feat"
---

Added `desktop_template` option on `DebianSettings`.
7 changes: 7 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
56 changes: 43 additions & 13 deletions tooling/bundler/src/bundle/linux/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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(())
}

Expand Down
10 changes: 10 additions & 0 deletions tooling/bundler/src/bundle/linux/templates/main.desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Desktop Entry]
Categories={{categories}}
{{#if comment}}
Comment={{comment}}
{{/if}}
Exec={{exec}}
Icon={{icon}}
Name={{name}}
Terminal=false
Type=Application
9 changes: 9 additions & 0 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 35cd751

Please sign in to comment.