-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
executable file
·92 lines (82 loc) · 3.32 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
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};
static ASSET_FILES: &'static [&'static str] = &[
"node_modules/mozilla-fira-pack/Fira/woff/FiraMono-Regular.woff",
"node_modules/mozilla-fira-pack/Fira/woff/FiraSans-Regular.woff",
"node_modules/mozilla-fira-pack/Fira/woff/FiraSans-Light.woff",
"node_modules/mozilla-fira-pack/Fira/woff/FiraSans-UltraLight.woff",
"node_modules/dygraphs/dygraph-combined.js",
"node_modules/preact/dist/preact.min.js",
"app.js",
"styles.css",
"index.html",
];
fn main() {
let out_dir_str = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_str);
let proj_dir = env::current_dir().unwrap();
let client_dir = proj_dir.join("client");
let status = match Command::new("npm")
.arg("install")
.current_dir(&client_dir)
.status() {
Ok(o) => o,
Err(e1) => match Command::new("npm.cmd")
.arg("install")
.current_dir(&client_dir)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status() {
Ok(o) => o,
Err(e2) => {
println!("Tried both npm and npm.cmd, neither worked");
println!("Error encountered for 'npm install': {:?}", e1);
println!("Error encountered for 'npm.cmd install': {:?}", e2);
panic!("Failed to execute NPM.");
}
}
};
if !status.success() {
panic!("'npm install' did not succeed.")
}
let assets_out_dir = out_dir.join("assets");
fs::create_dir_all(&assets_out_dir).unwrap();
let mut wahb = String::new();
wahb.push_str("fn _webassets_handler_body<'a>(path: &'a str) -> Option<(WebAssetContainer, &'static str)> {\n");
wahb.push_str("match path {\n");
for asset_source in ASSET_FILES {
let source_path = client_dir.join(asset_source);
let asset_filename = source_path.file_name().unwrap();
let target_path = assets_out_dir.join(asset_filename);
let ext = target_path.extension().unwrap().to_str().unwrap();
let content_type = match ext {
"html" | "htm" => "text/html",
"js" => "application/javascript",
"css" => "text/css",
"woff" => "application/font-woff",
_ => "text/plain"
};
let match_arm = format!(
"r#\"{}\"# => Some(({}r#\"{}\"#)), \"{}\")),\n",
asset_filename.to_str().unwrap(),
match ext {
"woff" => "WebAssetContainer::Binary(include_bytes!(",
_ => "WebAssetContainer::Text(include_str!("
},
target_path.to_str().unwrap(),
content_type
);
wahb.push_str(&match_arm);
fs::copy(&source_path, &target_path).unwrap();
}
wahb.push_str("_ => None\n");
wahb.push_str("}\n");
wahb.push_str("}\n");
let wa_handler_body_path = out_dir.join("webassets_handler_body.rs");
let mut wa_handler_body_file = File::create(&wa_handler_body_path).unwrap();
wa_handler_body_file.write_all(wahb.as_bytes()).unwrap();
}