forked from rust-lang/docs.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
51 lines (40 loc) · 1.34 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
extern crate time;
extern crate sass_rs;
extern crate git2;
use std::env;
use std::path::Path;
use std::fs::File;
use std::io::Write;
use git2::Repository;
fn main() {
write_git_version();
compile_sass();
}
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::sass_context::SassFileContext;
let mut file_context = SassFileContext::new(concat!(env!("CARGO_MANIFEST_DIR"),
"/templates/style.scss"));
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();
}