refactor(build): allow setting window icon path on try_build (#1686)

This commit is contained in:
Lucas Fernandes Nogueira 2021-05-03 10:42:29 -03:00 committed by GitHub
parent 1d6f418129
commit c91105ff96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 9 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-build": patch
---
The `try_build` method now has a `Attributes` argument to allow specifying the window icon path used on Windows.

View File

@ -11,6 +11,7 @@ use std::{
};
use tauri_codegen::{context_codegen, ContextData};
// TODO docs
/// A builder for generating a Tauri application context during compile time.
///
/// Meant to be used with [`tauri::include_codegen_context!`] inside your application code.

View File

@ -6,12 +6,62 @@
pub use anyhow::Result;
use std::path::{Path, PathBuf};
#[cfg(feature = "codegen")]
mod codegen;
#[cfg(feature = "codegen")]
pub use codegen::context::CodegenContext;
/// Attributes used on Windows.
#[allow(dead_code)]
pub struct WindowsAttributes {
window_icon_path: PathBuf,
}
impl Default for WindowsAttributes {
fn default() -> Self {
Self {
window_icon_path: PathBuf::from("icons/icon.ico"),
}
}
}
impl WindowsAttributes {
/// Creates the default attribute set.
pub fn new() -> Self {
Self::default()
}
/// Sets the icon to use on the window. Currently only used on Windows.
/// It must be in `ico` format. Defaults to `icons/icon.ico`.
pub fn window_icon_path<P: AsRef<Path>>(mut self, window_icon_path: P) -> Self {
self.window_icon_path = window_icon_path.as_ref().into();
self
}
}
/// The attributes used on the build.
#[derive(Default)]
pub struct Attributes {
#[allow(dead_code)]
windows_attributes: WindowsAttributes,
}
impl Attributes {
/// Creates the default attribute set.
pub fn new() -> Self {
Self::default()
}
/// Sets the icon to use on the window. Currently only used on Windows.
pub fn windows_attributes(mut self, windows_attributes: WindowsAttributes) -> Self {
self.windows_attributes = windows_attributes;
self
}
}
/// Run all build time helpers for your Tauri Application.
///
/// The current helpers include the following:
@ -32,27 +82,39 @@ pub use codegen::context::CodegenContext;
/// If any of the build time helpers fail, they will [`std::panic!`] with the related error message.
/// This is typically desirable when running inside a build script; see [`try_build`] for no panics.
pub fn build() {
if let Err(error) = try_build() {
if let Err(error) = try_build(Attributes::default()) {
panic!("error found during tauri-build: {}", error);
}
}
/// Non-panicking [`build()`].
pub fn try_build() -> Result<()> {
#[allow(unused_variables)]
pub fn try_build(attributes: Attributes) -> Result<()> {
#[cfg(windows)]
{
use anyhow::{anyhow, Context};
use std::path::Path;
use winres::WindowsResource;
if Path::new("icons/icon.ico").exists() {
let icon_path_string = attributes
.windows_attributes
.window_icon_path
.to_string_lossy()
.into_owned();
if attributes.windows_attributes.window_icon_path.exists() {
let mut res = WindowsResource::new();
res.set_icon_with_id("icons/icon.ico", "32512");
res.set_icon_with_id(&icon_path_string, "32512");
res.compile().with_context(|| {
"failed to compile icons/icon.ico into a Windows Resource file during tauri-build"
format!(
"failed to compile `{}` into a Windows Resource file during tauri-build",
icon_path_string
)
})?;
} else {
return Err(anyhow!("no icons/icon.ico file found; required for generating a Windows Resource file during tauri-build"));
return Err(anyhow!(format!(
"`{}` not found; required for generating a Windows Resource file during tauri-build",
icon_path_string
)));
}
}

View File

@ -44,7 +44,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
// handle default window icons for Windows targets
let default_window_icon = if cfg!(windows) {
let icon_path = config_parent.join("icons/icon.ico").display().to_string();
let icon_path = config
.tauri
.bundle
.icon
.iter()
.find(|i| i.ends_with(".ico"))
.cloned()
.unwrap_or_else(|| "icons/icon.ico".to_string());
let icon_path = config_parent.join(icon_path).display().to_string();
quote!(Some(include_bytes!(#icon_path).to_vec()))
} else {
quote!(None)

View File

@ -306,12 +306,16 @@ impl CliConfig {
pub struct BundleConfig {
/// The bundle identifier.
pub identifier: String,
/// The bundle icons.
#[serde(default)]
pub icon: Vec<String>,
}
impl Default for BundleConfig {
fn default() -> Self {
Self {
identifier: String::from(""),
icon: Vec::default(),
}
}
}
@ -725,8 +729,9 @@ mod build {
impl ToTokens for BundleConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
let identifier = str_lit(&self.identifier);
let icon = vec_lit(&self.icon, str_lit);
literal_struct!(tokens, BundleConfig, identifier);
literal_struct!(tokens, BundleConfig, identifier, icon);
}
}
@ -843,6 +848,7 @@ mod test {
}],
bundle: BundleConfig {
identifier: String::from(""),
icon: Vec::new(),
},
cli: None,
updater: UpdaterConfig {