-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
122 lines (101 loc) · 3.25 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::{
env,
path::Path,
process::{exit, Command},
};
use vergen_gix::{BuildBuilder, CargoBuilder, DependencyKind, GixBuilder};
macro_rules! info {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}
fn is_wasm_target_installed() -> bool {
let output = Command::new("rustup")
.args(["target", "list", "--installed"])
.output()
.expect("Failed to execute rustup");
let installed_targets = String::from_utf8_lossy(&output.stdout);
installed_targets.contains("wasm32-unknown-unknown")
}
fn install_wasm_target() {
info!("Adding wasm32-unknown-unknown target...");
let output = Command::new("rustup")
.args(["target", "add", "wasm32-unknown-unknown"])
.output()
.expect("Failed to execute rustup");
if !output.status.success() {
eprintln!(
"Failed to install webasm: {}",
String::from_utf8_lossy(&output.stderr)
);
exit(1);
}
}
fn get_trunk_version() -> Option<String> {
Command::new("trunk")
.arg("--version")
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.and_then(|version_string| version_string.split_whitespace().last().map(String::from))
}
fn install_trunk() -> Result<(), Box<dyn std::error::Error>> {
info!("Installing trunk...");
let output = Command::new("cargo")
.arg("install")
.arg("trunk")
.arg("--force")
.output()?;
if !output.status.success() {
eprintln!(
"Failed to install trunk: {}",
String::from_utf8_lossy(&output.stderr)
);
exit(1);
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let out_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
for path in ["src", "assets", "index.html"].iter() {
let p = Path::new(&out_dir).join(format!("src/webpage/{}", path));
println!("cargo:rerun-if-changed={}", p.display());
}
vergen_gix::Emitter::default()
.add_instructions(&BuildBuilder::all_build()?)?
.add_instructions(&GixBuilder::all_git()?)?
.add_instructions(
CargoBuilder::all_cargo()?.set_dep_kind_filter(Some(DependencyKind::Normal)),
)?
.emit()?;
if std::env::var("SKIP_FRONTEND").is_ok() {
return Ok(());
}
if !is_wasm_target_installed() {
install_wasm_target();
}
if get_trunk_version().is_none() {
info!("trunk not found");
install_trunk().unwrap_or_else(|e| {
eprintln!("Error: {}", e);
exit(1);
});
}
info!("Building frontend...");
let mut trunk_command = Command::new("trunk");
trunk_command.args(["build", "./src/webpage/index.html"]);
// Add --release argument if not in debug mode
if cfg!(not(debug_assertions)) {
trunk_command.args(["--release", "--locked"]);
}
let trunk_output = trunk_command.output().expect("Failed to execute trunk");
if !trunk_output.status.success() {
eprintln!(
"Trunk build failed: {}",
String::from_utf8_lossy(&trunk_output.stderr)
);
exit(1);
}
info!("{}", String::from_utf8_lossy(&trunk_output.stdout));
Ok(())
}