-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.rs
More file actions
138 lines (129 loc) · 4.26 KB
/
build.rs
File metadata and controls
138 lines (129 loc) · 4.26 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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
use embed_manifest::{
embed_manifest,
manifest::{
ActiveCodePage, DpiAwareness, HeapType, Setting,
SupportedOS::{Windows7, Windows10},
},
new_manifest,
};
use winres::WindowsResource;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=fedra.iss.in");
println!("cargo:rerun-if-changed=sounds");
build_docs();
configure_installer();
let target = env::var("TARGET").unwrap_or_default();
if target.contains("windows") {
let manifest = new_manifest("Fedra")
.supported_os(Windows7..=Windows10)
.active_code_page(ActiveCodePage::Utf8)
.heap_type(HeapType::SegmentHeap)
.dpi_awareness(DpiAwareness::PerMonitorV2)
.long_path_aware(Setting::Enabled);
if let Err(e) = embed_manifest(manifest) {
println!("cargo:warning=Failed to embed manifest: {e}");
println!("cargo:warning=The application will still work but may lack optimal Windows theming");
}
embed_version_info();
embed_commit_hash();
}
}
fn embed_commit_hash() {
let output = Command::new("git").args(["rev-parse", "HEAD"]).output();
let hash = match output {
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),
_ => "unknown".to_string(),
};
println!("cargo:rustc-env=FEDRA_COMMIT_HASH={hash}");
// Force a rebuild if the commit changes
let git_dir = Path::new(".git");
if git_dir.exists() {
let head_path = git_dir.join("HEAD");
println!("cargo:rerun-if-changed={}", head_path.display());
if let Ok(head_content) = fs::read_to_string(&head_path)
&& let Some(ref_path) = head_content.trim().strip_prefix("ref: ")
{
let ref_full_path = git_dir.join(ref_path);
println!("cargo:rerun-if-changed={}", ref_full_path.display());
}
}
}
fn embed_version_info() {
let version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
let mut res = WindowsResource::new();
res.set("ProductName", "Fedra")
.set("FileDescription", "Fedra")
.set("LegalCopyright", "Copyright © 2026 Quin Gillespie")
.set("CompanyName", "Quin Gillespie")
.set("OriginalFilename", "fedra.exe")
.set("ProductVersion", &version)
.set("FileVersion", &version);
if let Err(e) = res.compile() {
println!("cargo:warning=Failed to embed version info: {e}");
}
}
fn target_profile_dir() -> Option<PathBuf> {
let profile = env::var("PROFILE").ok()?;
if let Ok(target_dir) = env::var("CARGO_TARGET_DIR") {
let mut dir = PathBuf::from(target_dir);
dir.push(profile);
return Some(dir);
}
let out_dir = PathBuf::from(env::var("OUT_DIR").ok()?);
out_dir.ancestors().nth(3).map(Path::to_path_buf)
}
fn build_docs() {
let Some(target_dir) = target_profile_dir() else {
println!("cargo:warning=Could not determine target directory for docs.");
return;
};
let doc_dir = PathBuf::from("doc");
let readme = doc_dir.join("readme.md");
let config = doc_dir.join("pandoc.yaml");
println!("cargo:rerun-if-changed={}", readme.display());
println!("cargo:rerun-if-changed={}", config.display());
let pandoc_check = Command::new("pandoc").arg("--version").output();
if pandoc_check.is_err() {
println!("cargo:warning=Pandoc not found. Documentation will not be generated.");
return;
}
let output = target_dir.join("readme.html");
let status = Command::new("pandoc")
.arg(format!("--defaults={}", config.display()))
.arg(&readme)
.arg("-o")
.arg(&output)
.status();
match status {
Ok(s) if s.success() => {}
_ => println!("cargo:warning=Failed to generate documentation."),
}
}
fn configure_installer() {
let Some(target_dir) = target_profile_dir() else { return };
let input_path = PathBuf::from("fedra.iss.in");
if !input_path.exists() {
return;
}
let content = match fs::read_to_string(&input_path) {
Ok(c) => c,
Err(e) => {
println!("cargo:warning=Failed to read installer script: {e}");
return;
}
};
let version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
let new_content = content.replace("@PROJECT_VERSION@", &version);
let output_path = target_dir.join("fedra.iss");
if let Err(e) = fs::write(&output_path, new_content) {
println!("cargo:warning=Failed to write installer script: {e}");
}
}