/
build.rs
63 lines (48 loc) · 1.54 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
// https://github.com/rust-lang/cargo/issues/6833#issue-430806770
extern crate failure;
extern crate which;
use failure::Fallible;
use std::env;
use std::fs::copy;
use std::path::Path;
use std::process::Command;
use which::which;
fn run<F>(name: &str, mut configure: F) -> Fallible<()>
where
F: FnMut(&mut Command) -> &mut Command,
{
let mut command = Command::new(name);
let configured = configure(&mut command);
if !configured.spawn()?.wait()?.success() {
panic!("failed to execute {:?}", configured);
}
Ok(())
}
fn build_client() -> Fallible<()> {
let client = Path::new("./website-src");
let current_dir = env::current_dir()?;
env::set_current_dir(&client)?;
let npm_path = which("npm")?;
let npm_path = npm_path.to_str().unwrap();
// install node packages before building
if !Path::new("node_modules").exists() {
run(npm_path, |command| command.arg("i"))?;
}
run(npm_path, |command| command.arg("run").arg("build"))?;
env::set_current_dir(current_dir)?;
Ok(())
}
fn copy_to_templates_directory() -> Fallible<()> {
if !Path::new("templates").exists() {
std::fs::create_dir(Path::new("templates"))?;
}
copy(Path::new("./website-src/dist/event.html"), Path::new("./templates/event.html"))?;
copy(Path::new("./website-src/dist/schedule.html"), Path::new("./templates/schedule.html"))?;
Ok(())
}
fn main() -> Fallible<()> {
println!("cargo:rerun-if-changed=website-src/src/*");
build_client()?;
copy_to_templates_directory()?;
Ok(())
}