forked from mavlink/mavlink-camera-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
107 lines (95 loc) · 4.1 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
use std::fs;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use regex::Regex;
use ts_rs::TS;
#[path = "src/stream/webrtc/signalling_protocol.rs"]
mod signalling_protocol;
use crate::signalling_protocol::*;
fn file_download(url: &str, output: &str) {
let mut resp =
reqwest::blocking::get(url).unwrap_or_else(|_| panic!("Failed to download file: {url}"));
let file_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(output);
let mut output_file = std::fs::File::create(&file_path)
.unwrap_or_else(|_| panic!("Failed to create file: {file_path:?}"));
std::io::copy(&mut resp, &mut output_file).expect("Failed to copy content.");
}
fn main() {
// Configure vergen
let mut config = vergen::Config::default();
*config.build_mut().semver_mut() = true;
*config.build_mut().timestamp_mut() = true;
*config.build_mut().kind_mut() = vergen::TimestampKind::DateOnly;
*config.git_mut().sha_kind_mut() = vergen::ShaKind::Short;
vergen::vergen(config).expect("Unable to generate the cargo keys!");
file_download(
"https://unpkg.com/vue@3.0.5/dist/vue.global.js",
"src/html/vue.js",
);
// set SKIP_BINDINGS=1 to skip typescript bindings generation
if std::env::var("SKIP_BINDINGS").is_err() {
generate_typescript_bindings();
}
// set SKIP_YARN=1 to skip YARN build
if std::env::var("SKIP_YARN").is_err() {
build_with_yarn();
}
}
fn generate_typescript_bindings() {
println!("cargo:rerun-if-changed=src/stream/webrtc/signalling_protocol.rs");
// Generate all typescript bindings and join them into a single String
let bindings = [
Message::export_to_string().unwrap(),
Answer::export_to_string().unwrap(),
Question::export_to_string().unwrap(),
Negotiation::export_to_string().unwrap(),
BindOffer::export_to_string().unwrap(),
BindAnswer::export_to_string().unwrap(),
PeerIdAnswer::export_to_string().unwrap(),
Stream::export_to_string().unwrap(),
IceNegotiation::export_to_string().unwrap(),
MediaNegotiation::export_to_string().unwrap(),
EndSessionQuestion::export_to_string().unwrap(),
]
.join("\n\n");
// Remove all typescript "import type" because all types are going to live in the same typescritp file
let re = Regex::new(r"(?m)^import type .*\n").unwrap();
let bindings = re.replace_all(bindings.as_str(), "").to_string();
// Replace all notices by a custom one
let re = Regex::new(r"(?m)^// This file was generated by .*\n\n").unwrap();
let mut bindings = re.replace_all(bindings.as_str(), "").to_string();
let custom_notice_str = "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs) during `cargo build` step. Do not edit this file manually.\n\n";
bindings.insert_str(0, custom_notice_str);
// Export to file
let output_dir = Path::new("./src/stream/webrtc/frontend/bindings/");
if !output_dir.exists() {
std::fs::create_dir_all(output_dir).unwrap();
}
let bindings_file_path = output_dir.join(Path::new("signalling_protocol.d.ts"));
let mut bindings_file = fs::File::create(bindings_file_path).unwrap();
bindings_file.write_all(bindings.as_bytes()).unwrap();
}
fn build_with_yarn() {
// Note that as we are not waching all files, sometimes we'd need to force this build
println!("cargo:rerun-if-changed=./src/stream/webrtc/frontend/index.html");
println!("cargo:rerun-if-changed=./src/stream/webrtc/frontend/package.json");
println!("cargo:rerun-if-changed=./src/stream/webrtc/frontend/src");
// Build with YARN
let frontend_dir = Path::new("./src/stream/webrtc/frontend");
frontend_dir.try_exists().unwrap();
Command::new("yarn")
.args(["--version"])
.status()
.expect("Failed to build frontend, `yarn` appears to be not installed.");
Command::new("yarn")
.args(["install"])
.current_dir(frontend_dir)
.status()
.unwrap();
Command::new("yarn")
.args(["build"])
.current_dir(frontend_dir)
.status()
.unwrap();
}