-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.rs
More file actions
225 lines (192 loc) · 6.58 KB
/
Copy pathbuild.rs
File metadata and controls
225 lines (192 loc) · 6.58 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;
use image::RgbaImage;
use resvg::{tiny_skia, usvg};
const UI_PATHS: &[&str] = &[
"assets/icon.svg",
"assets/icon-dark.svg",
"ui/src",
"ui/public",
"ui/index.html",
"ui/package.json",
"ui/package-lock.json",
"ui/tailwind.config.js",
"ui/postcss.config.js",
"ui/vite.config.js",
];
const UI_ASSETS: &[(&str, &str)] = &[
("assets/icon.svg", "ui/public/icon.svg"),
("assets/icon-dark.svg", "ui/public/icon-dark.svg"),
];
const TRAY_ICON_SVG_PATHS: &[&str] = &["assets/logo-active.svg", "assets/logo-inactive.svg"];
const MACOS_TRAY_ICONS: &[(&str, &str, bool)] = &[
("assets/logo-active.svg", "tray-icon-active-light.png", false),
("assets/logo-active.svg", "tray-icon-active-dark.png", true),
("assets/logo-inactive.svg", "tray-icon-inactive-light.png", false),
("assets/logo-inactive.svg", "tray-icon-inactive-dark.png", true),
];
fn main() {
for path in UI_PATHS {
println!("cargo:rerun-if-changed={path}");
}
for path in TRAY_ICON_SVG_PATHS {
println!("cargo:rerun-if-changed={path}");
}
println!("cargo:rerun-if-env-changed=NEOMIST_SKIP_UI_BUILD");
build_macos_tray_icons();
if env::var_os("NEOMIST_SKIP_UI_BUILD").is_some() {
println!("cargo:warning=Skipping UI build (NEOMIST_SKIP_UI_BUILD set)");
return;
}
let ui_dir = Path::new("ui");
let dist_index = ui_dir.join("dist").join("index.html");
let latest_input = latest_input_mtime();
let dist_mtime = file_mtime(&dist_index);
let needs_build = match (latest_input, dist_mtime) {
(Some(input_time), Some(dist_time)) => input_time > dist_time,
(Some(_), None) => true,
_ => !dist_index.exists(),
};
let needs_build = needs_build || UI_ASSETS.iter().any(|(_, dest)| !Path::new(dest).exists());
if !needs_build {
return;
}
for (source, dest) in UI_ASSETS {
sync_asset(Path::new(source), Path::new(dest));
}
if needs_npm_install(ui_dir) {
run_npm(ui_dir, &["install"]);
}
run_npm(ui_dir, &["run", "build"]);
if !dist_index.exists() {
panic!("UI build failed: dist/index.html missing");
}
}
fn latest_input_mtime() -> Option<SystemTime> {
let mut latest = None;
for path in UI_PATHS {
let path = Path::new(path);
if path.is_dir() {
latest = max_time(latest, dir_mtime(path));
} else {
latest = max_time(latest, file_mtime(path));
}
}
latest
}
fn max_time(current: Option<SystemTime>, next: Option<SystemTime>) -> Option<SystemTime> {
match (current, next) {
(Some(a), Some(b)) => Some(if a > b { a } else { b }),
(None, Some(b)) => Some(b),
(Some(a), None) => Some(a),
(None, None) => None,
}
}
fn dir_mtime(dir: &Path) -> Option<SystemTime> {
let mut latest = file_mtime(dir);
let entries = fs::read_dir(dir).ok()?;
for entry in entries.flatten() {
let path = entry.path();
if path
.file_name()
.is_some_and(|name| name == "node_modules" || name == "dist")
{
continue;
}
if path.is_dir() {
latest = max_time(latest, dir_mtime(&path));
} else {
latest = max_time(latest, file_mtime(&path));
}
}
latest
}
fn file_mtime(path: &Path) -> Option<SystemTime> {
fs::metadata(path).and_then(|meta| meta.modified()).ok()
}
fn needs_npm_install(ui_dir: &Path) -> bool {
let node_modules = ui_dir.join("node_modules");
if !node_modules.exists() {
return true;
}
let lock_path = ui_dir.join("package-lock.json");
let lock_mtime = file_mtime(&lock_path);
let node_mtime = file_mtime(&node_modules);
match (lock_mtime, node_mtime) {
(Some(lock), Some(node)) => lock > node,
(Some(_), None) => true,
_ => false,
}
}
fn run_npm(ui_dir: &Path, args: &[&str]) {
let npm = if cfg!(target_os = "windows") {
"npm.cmd"
} else {
"npm"
};
let status = Command::new(npm)
.args(args)
.current_dir(ui_dir)
.status()
.expect("Failed to run npm");
if !status.success() {
panic!("npm command failed: {args:?}");
}
}
fn sync_asset(source: &Path, dest: &Path) {
let source_bytes =
fs::read(source).unwrap_or_else(|err| panic!("Failed to read {source:?}: {err}"));
let needs_copy = match fs::read(dest) {
Ok(dest_bytes) => dest_bytes != source_bytes,
Err(_) => true,
};
if needs_copy {
if let Some(parent) = dest.parent() {
if let Err(err) = fs::create_dir_all(parent) {
panic!("Failed to create {parent:?}: {err}");
}
}
if let Err(err) = fs::write(dest, source_bytes) {
panic!("Failed to write {dest:?}: {err}");
}
}
}
fn build_macos_tray_icons() {
if env::var("CARGO_CFG_TARGET_OS").ok().as_deref() != Some("macos") {
return;
}
let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR missing"));
for (source, output_name, use_dark_strokes) in MACOS_TRAY_ICONS {
render_macos_tray_icon(Path::new(source), &out_dir.join(output_name), *use_dark_strokes);
}
}
fn render_macos_tray_icon(source: &Path, dest: &Path, use_dark_strokes: bool) {
let svg = fs::read_to_string(source)
.unwrap_or_else(|err| panic!("Failed to read {source:?}: {err}"));
let svg = if use_dark_strokes {
svg.replace("stroke:#000000", "stroke:#ffffff")
} else {
svg
};
let options = usvg::Options::default();
let tree = usvg::Tree::from_str(&svg, &options)
.unwrap_or_else(|err| panic!("Failed to parse tray icon SVG {source:?}: {err}"));
let size = tree.size().to_int_size();
let (width, height) = size.dimensions();
let mut pixmap = tiny_skia::Pixmap::new(width, height)
.unwrap_or_else(|| panic!("Failed to allocate tray icon pixmap for {source:?}"));
let mut pixmap_mut = pixmap.as_mut();
resvg::render(&tree, tiny_skia::Transform::identity(), &mut pixmap_mut);
let image = RgbaImage::from_raw(width, height, pixmap.take_demultiplied())
.unwrap_or_else(|| panic!("Failed to build tray icon image buffer for {source:?}"));
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent)
.unwrap_or_else(|err| panic!("Failed to create {parent:?}: {err}"));
}
image
.save(dest)
.unwrap_or_else(|err| panic!("Failed to write {dest:?}: {err}"));
}