-
Notifications
You must be signed in to change notification settings - Fork 31
/
build.rs
63 lines (54 loc) · 1.93 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
// Copyright 2016 The Rustw Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate walkdir;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use walkdir::WalkDir;
/// This build script creates a function for looking up static data to be served
/// by the web server. The contents of the static directory are included in the
/// binary via `include_bytes` and exposed to the rest of the program by
/// `lookup_static_file`.
fn main() {
let from = env::var("CARGO_MANIFEST_DIR").unwrap();
let from = Path::new(&from);
let from = from.join("static");
let mut out_path = Path::new(&env::var("OUT_DIR").unwrap()).to_owned();
out_path.push("lookup_static.rs");
// Don't rerun on every build, but do rebuild if the build script changes
// or something changes in the static directory.
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=static");
let mut out_file = File::create(&out_path).unwrap();
out_file.write(PREFIX.as_bytes()).unwrap();
for entry in WalkDir::new(&from)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| !e.path_is_symlink() && e.file_type().is_file())
{
let relative = entry.path().strip_prefix(&from).unwrap();
write!(
out_file,
"\nr#\"{}\"# => Ok(include_bytes!(r#\"{}\"#)),",
relative.display(),
entry.path().display()
)
.unwrap();
}
out_file.write(SUFFIX.as_bytes()).unwrap();
}
const PREFIX: &str = "
pub fn lookup_static_file(path: &str) -> Result<&'static [u8], ()> {
match path {
";
const SUFFIX: &str = "
_ => Err(()),
}
}
";