1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![cfg_attr(doc_cfg, feature(doc_cfg))]
pub use anyhow::Result;
use std::path::{Path, PathBuf};
#[cfg(feature = "codegen")]
mod codegen;
#[cfg(feature = "codegen")]
pub use codegen::context::CodegenContext;
#[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 {
pub fn new() -> Self {
Self::default()
}
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
}
}
#[derive(Default)]
pub struct Attributes {
#[allow(dead_code)]
windows_attributes: WindowsAttributes,
}
impl Attributes {
pub fn new() -> Self {
Self::default()
}
pub fn windows_attributes(mut self, windows_attributes: WindowsAttributes) -> Self {
self.windows_attributes = windows_attributes;
self
}
}
pub fn build() {
if let Err(error) = try_build(Attributes::default()) {
panic!("error found during tauri-build: {}", error);
}
}
#[allow(unused_variables)]
pub fn try_build(attributes: Attributes) -> Result<()> {
#[cfg(windows)]
{
use anyhow::{anyhow, Context};
use std::fs::read_to_string;
use tauri_utils::config::Config;
use winres::WindowsResource;
let config: Config = serde_json::from_str(
&read_to_string("tauri.conf.json").expect("failed to read tauri.conf.json"),
)
.expect("failed to parse tauri.conf.json");
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();
if let Some(version) = &config.package.version {
res.set("FileVersion", version);
res.set("ProductVersion", version);
}
if let Some(product_name) = &config.package.product_name {
res.set("ProductName", product_name);
res.set("FileDescription", product_name);
}
res.set_icon_with_id(&icon_path_string, "32512");
res.compile().with_context(|| {
format!(
"failed to compile `{}` into a Windows Resource file during tauri-build",
icon_path_string
)
})?;
} else {
return Err(anyhow!(format!(
"`{}` not found; required for generating a Windows Resource file during tauri-build",
icon_path_string
)));
}
}
Ok(())
}