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