-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
100 lines (82 loc) · 3.23 KB
/
build.rs
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
use glib_build_tools::compile_resources;
use std::fs;
use std::path::Path;
use std::process::Command;
fn should_update(source: &Path, target: &Path) -> bool {
if !target.exists() {
return true;
}
let source_modified = fs::metadata(source)
.and_then(|m| m.modified())
.expect("Failed to get source modification time");
let target_modified = fs::metadata(target)
.and_then(|m| m.modified())
.expect("Failed to get target modification time");
source_modified > target_modified
}
fn generate_app_icons(source: &Path, target_dir: &Path) {
// Icon sizes commonly used by Linux desktop environments
let sizes = [16, 24, 32, 48, 64, 128, 256, 512];
for size in sizes {
let size_dir = target_dir.join(format!("{}x{}", size, size)).join("apps");
fs::create_dir_all(&size_dir).unwrap();
let target_icon = size_dir.join("org.timcharper.MindHunt.png");
if should_update(source, &target_icon) {
// Use convert to resize the icon
Command::new("convert")
.arg(source)
.arg("-resize")
.arg(format!("{}x{}", size, size))
.arg(&target_icon)
.status()
.expect("Failed to generate icon");
}
}
}
fn main() {
let version_path = Path::new("version.txt");
let version = fs::read_to_string(version_path)
.unwrap_or_else(|_| "unknown".to_string()) // Fallback if file is missing
.trim()
.to_string(); // Trim to remove newline
println!("cargo:rustc-env=APP_VERSION={}", version);
// Compile the resources
compile_resources(
&["resources"],
"resources/resources.xml",
"compiled.gresource",
);
// Set up target directory for resources based on profile
let out_dir = std::env::var("OUT_DIR").unwrap();
let profile = std::env::var("PROFILE").unwrap();
let target_dir = Path::new("target").join(&profile);
let resources_dir = target_dir.join("resources");
fs::create_dir_all(&resources_dir).unwrap();
// Copy the compiled resource
fs::copy(
Path::new(&out_dir).join("compiled.gresource"),
target_dir.join("compiled.gresource"),
)
.unwrap();
// Set up icon theme directory
let icon_theme_dir = resources_dir.join("icons/hicolor");
fs::create_dir_all(&icon_theme_dir).unwrap();
// Generate icons from the source icon
generate_app_icons(Path::new("resources/mindhunt-icon.png"), &icon_theme_dir);
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
let res_output = resources_dir.join("app.res");
println!("cargo:rerun-if-changed=app.rc");
let status = std::process::Command::new("x86_64-w64-mingw32-windres")
.args(&["app.rc", "-O", "coff", "-o"])
.arg(&res_output)
.status()
.expect("Failed to run windres");
if !status.success() {
panic!("windres failed with exit code: {:?}", status.code());
}
// Tell Cargo to link the generated app.res
println!("cargo:rustc-link-arg={}", res_output.display());
}
// Tell cargo to rerun if resources change
println!("cargo:rerun-if-changed=resources");
}