-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.rs
75 lines (67 loc) · 2.52 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
use anyhow::*;
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::{env, fs};
const DESCRIPTOR_PATH: &str = "conf/ya-runtime-wasi.json";
#[cfg(windows)]
fn setup() {
let mut res = winres::WindowsResource::new();
res.set_icon("conf/webassembly.ico");
res.compile().unwrap();
}
#[cfg(not(windows))]
fn setup() {}
fn update_descriptor() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=conf/{}", DESCRIPTOR_PATH);
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS");
let exe_extension = if target_os == "windows" { ".exe" } else { "" };
let mut descriptors: Value =
serde_json::from_reader(fs::OpenOptions::new().read(true).open(DESCRIPTOR_PATH)?)?;
for descriptor in descriptors.as_array_mut().expect("invalid descriptor") {
if let Some(obj) = descriptor.as_object_mut() {
obj.insert(
"version".into(),
env::var("CARGO_PKG_VERSION")
.expect("env CARGO_PKG_VERSION missing")
.into(),
);
//obj.insert("name".into(), env::var("CARGO_PKG_NAME")?.into());
let runtime_path = obj
.get("runtime-path")
.and_then(|path| Some(format!("{}{}", path.as_str()?, exe_extension)));
let supervisor_path = obj
.get("supervisor-path")
.and_then(|path| Some(format!("{}{}", path.as_str()?, exe_extension)));
if let Some(runtime_path) = runtime_path {
obj.insert("runtime-path".into(), runtime_path.into());
}
if let Some(supervisor_path) = supervisor_path {
obj.insert("supervisor-path".into(), supervisor_path.into());
}
} else {
panic!(
"invalid descriptor template: {}",
serde_json::to_string(&descriptor)?
);
}
}
let output_directory: PathBuf = env::var("CARGO_BUILD_TARGET_DIR")
.ok()
.map(Into::into)
.unwrap_or_else(|| Path::new("target").join(env::var("PROFILE").unwrap()));
let output_file = output_directory.join("ya-runtime-wasi.json");
serde_json::to_writer_pretty(
fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(&output_file)
.with_context(|| format!("writing descriptor to {}", output_file.display()))?,
&descriptors,
)?;
Ok(())
}
fn main() {
update_descriptor().unwrap();
setup();
}