forked from rust-lang/docs.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
62 lines (50 loc) · 1.73 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
extern crate time;
extern crate sass_rs;
extern crate git2;
use std::env;
use std::path::Path;
use std::fs::{self, File};
use std::io::Write;
use git2::Repository;
fn main() {
write_git_version();
compile_sass();
copy_js();
}
fn write_git_version() {
let git_hash = get_git_hash().unwrap_or("???????".to_owned());
let build_date = time::strftime("%Y-%m-%d", &time::now_utc()).unwrap();
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("git_version");
let mut file = File::create(&dest_path).unwrap();
write!(file, "({} {})", git_hash, build_date).unwrap();
}
fn get_git_hash() -> Option<String> {
let repo = match Repository::open(env::current_dir().unwrap()) {
Ok(repo) => repo,
Err(_) => return None,
};
let head = repo.head().unwrap();
head.target().map(|h| {
let mut h = format!("{}", h);
h.truncate(7);
h
})
}
fn compile_sass() {
use sass_rs::Context;
let mut file_context = Context::new_file(concat!(env!("CARGO_MANIFEST_DIR"),
"/templates/style.scss")).unwrap();
let css = file_context.compile().unwrap();
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("style.css");
let mut file = File::create(&dest_path).unwrap();
file.write_all(css.as_bytes()).unwrap();
}
fn copy_js() {
["menu.js", "index.js"].iter()
.for_each(|path| {
let source_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
.join(format!("templates/{}", path));
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join(path);
fs::copy(&source_path, &dest_path).expect("Copy JavaScript file to target");
});
}