|
26 | 26 | use super::super::common; |
27 | 27 | use crate::Settings; |
28 | 28 | use anyhow::Context; |
| 29 | +use handlebars::Handlebars; |
29 | 30 | use heck::AsKebabCase; |
30 | 31 | use image::{self, codecs::png::PngDecoder, ImageDecoder}; |
31 | 32 | use libflate::gzip; |
32 | 33 | use log::info; |
| 34 | +use serde::Serialize; |
33 | 35 | use walkdir::WalkDir; |
34 | 36 |
|
35 | 37 | use std::{ |
36 | 38 | collections::BTreeSet, |
37 | 39 | ffi::OsStr, |
38 | | - fs::{self, File}, |
| 40 | + fs::{self, read_to_string, File}, |
39 | 41 | io::{self, Write}, |
40 | 42 | path::{Path, PathBuf}, |
41 | 43 | }; |
@@ -141,23 +143,51 @@ fn generate_desktop_file(settings: &Settings, data_dir: &Path) -> crate::Result< |
141 | 143 | let desktop_file_path = data_dir |
142 | 144 | .join("usr/share/applications") |
143 | 145 | .join(desktop_file_name); |
144 | | - let file = &mut common::create_file(&desktop_file_path)?; |
| 146 | + |
145 | 147 | // For more information about the format of this file, see |
146 | 148 | // 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")?; |
150 | 157 | } 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")?; |
152 | 161 | } |
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, |
155 | 170 | } |
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 | + |
161 | 191 | Ok(()) |
162 | 192 | } |
163 | 193 |
|
|
0 commit comments