-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbuild.rs
More file actions
122 lines (99 loc) · 3.31 KB
/
build.rs
File metadata and controls
122 lines (99 loc) · 3.31 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
use std::{path::Path, process::Command};
use vergen_gix::{BuildBuilder, CargoBuilder, DependencyKind, GixBuilder};
#[cfg(windows)]
fn print_link_search_path() {
use std::env;
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
if cfg!(target_arch = "x86_64") {
println!("cargo:rustc-link-search=native={}/lib/x64", manifest_dir);
} else {
println!("cargo:rustc-link-search=native={}/lib", manifest_dir);
}
}
#[cfg(not(windows))]
fn print_link_search_path() {}
fn main() -> Result<(), Box<dyn std::error::Error>> {
print_link_search_path();
generate_build_details()?;
// set SKIP_WEB=1 to skip
if std::env::var("SKIP_WEB").is_err() {
build_web();
}
Ok(())
}
fn build_web() {
// Note that as we are not watching all files, sometimes we'd need to force this build
println!("cargo:rerun-if-changed=./frontend/index.html");
println!("cargo:rerun-if-changed=./frontend/package.json");
println!("cargo:rerun-if-changed=./frontend/vite.config.ts");
println!("cargo:rerun-if-changed=./frontend/tsconfig.json");
println!("cargo:rerun-if-changed=./frontend/tsconfig.config.json");
println!("cargo:rerun-if-changed=./frontend/src");
println!("cargo:rerun-if-changed=./frontend/bindings/signalling_protocol.d.ts");
let frontend_dir = Path::new("./frontend");
frontend_dir.try_exists().unwrap();
let bindings_file = Path::new("./frontend/bindings/signalling_protocol.d.ts");
if !bindings_file.exists() {
panic!(
"\n\n\
Frontend TypeScript bindings not found at: {}\n\n\
Please generate them first by running:\n\n\
\tcargo run --package=bindings\n\n\
Or use `build.sh` which handles this automatically.\n",
bindings_file.display()
);
}
let program = if Command::new("bun")
.args(["--version"])
.status()
.ok()
.map(|status| status.success())
.unwrap_or(false)
{
"bun"
} else {
"yarn"
};
let version = Command::new(program)
.args(["--version"])
.status()
.unwrap_or_else(|_| {
panic!("Failed to build frontend, `{program}` appears to be not installed.",)
});
if !version.success() {
panic!("{program} version failed!");
}
let install = Command::new(program)
.args(["install", "--frozen-lockfile"])
.current_dir(frontend_dir)
.status()
.unwrap();
if !install.success() {
panic!("{program} install failed!");
}
let args = if program == "bun" {
vec!["run", "build"]
} else {
vec!["build"]
};
let build = Command::new(program)
.args(&args)
.current_dir(frontend_dir)
.status()
.unwrap();
if !build.success() {
panic!("{program} build failed!");
}
}
fn generate_build_details() -> Result<(), Box<dyn std::error::Error>> {
let mut emitter = vergen_gix::Emitter::default();
emitter.add_instructions(&BuildBuilder::all_build()?)?;
emitter.add_instructions(
CargoBuilder::all_cargo()?.set_dep_kind_filter(Some(DependencyKind::Normal)),
)?;
if std::path::Path::new(".git").is_dir() {
emitter.add_instructions(&GixBuilder::all_git()?)?;
}
emitter.emit()?;
Ok(())
}