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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use anyhow::{Context, Result};
use std::{
env::var,
fs::{create_dir_all, File},
io::{BufWriter, Write},
path::PathBuf,
};
use tauri_codegen::{context_codegen, ContextData};
use tauri_utils::config::{AppUrl, WindowUrl};
#[cfg_attr(doc_cfg, doc(cfg(feature = "codegen")))]
#[derive(Debug)]
pub struct CodegenContext {
dev: bool,
config_path: PathBuf,
out_file: PathBuf,
}
impl Default for CodegenContext {
fn default() -> Self {
Self {
dev: false,
config_path: PathBuf::from("tauri.conf.json"),
out_file: PathBuf::from("tauri-build-context.rs"),
}
}
}
impl CodegenContext {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn config_path(mut self, config_path: impl Into<PathBuf>) -> Self {
self.config_path = config_path.into();
self
}
#[must_use]
pub fn out_file(mut self, filename: PathBuf) -> Self {
self.out_file = filename;
self
}
#[must_use]
pub fn dev(mut self) -> Self {
self.dev = true;
self
}
pub fn build(self) -> PathBuf {
match self.try_build() {
Ok(out) => out,
Err(error) => panic!("Error found during Codegen::build: {error}"),
}
}
pub fn try_build(self) -> Result<PathBuf> {
let (config, config_parent) = tauri_codegen::get_config(&self.config_path)?;
let app_url = if self.dev {
&config.build.dev_path
} else {
&config.build.dist_dir
};
match app_url {
AppUrl::Url(WindowUrl::App(p)) => {
println!("cargo:rerun-if-changed={}", config_parent.join(p).display());
}
AppUrl::Files(files) => {
for path in files {
println!(
"cargo:rerun-if-changed={}",
config_parent.join(path).display()
);
}
}
_ => (),
}
for icon in &config.tauri.bundle.icon {
println!(
"cargo:rerun-if-changed={}",
config_parent.join(icon).display()
);
}
if let Some(tray_icon) = config.tauri.system_tray.as_ref().map(|t| &t.icon_path) {
println!(
"cargo:rerun-if-changed={}",
config_parent.join(tray_icon).display()
);
}
#[cfg(target_os = "macos")]
println!(
"cargo:rerun-if-changed={}",
config_parent.join("Info.plist").display()
);
let code = context_codegen(ContextData {
dev: self.dev,
config,
config_parent,
root: quote::quote!(::tauri),
})?;
let out = var("OUT_DIR")
.map(PathBuf::from)
.map(|path| path.join(&self.out_file))
.with_context(|| "unable to find OUT_DIR during tauri-build")?;
let parent = out.parent().with_context(|| {
"`Codegen` could not find the parent to `out_file` while creating the file"
})?;
create_dir_all(parent)?;
let mut file = File::create(&out).map(BufWriter::new).with_context(|| {
format!(
"Unable to create output file during tauri-build {}",
out.display()
)
})?;
writeln!(file, "{code}").with_context(|| {
format!(
"Unable to write tokenstream to out file during tauri-build {}",
out.display()
)
})?;
Ok(out)
}
}