-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
77 lines (66 loc) · 2.21 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
extern crate chardet;
extern crate encoding;
extern crate time;
use chardet::{charset2encoding, detect};
use encoding::label::encoding_from_whatwg_label;
use encoding::DecoderTrap;
use time::now_utc;
use std::env;
use std::fs::File;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::Command as Cmd;
/// `include!(concat!(env!("OUT_DIR"), "/zipcs.txt"));`
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_path = out_dir.join("zipcs.txt");
File::create(&out_path)
.and_then(|mut f| f.write_all(fun().as_bytes()))
.unwrap()
}
fn fun() -> String {
let rustc = rustc_version()
.map(|s| format!(" rustc{}", s.split(' ').nth(1).unwrap()))
.unwrap_or_default();
let git = commit_hash()
.and_then(|s| branch_name().map(|b| format!("{}@{}{} ", s, b, rustc)))
.unwrap_or_default();
let version = format!("{} ({}{})", env!("CARGO_PKG_VERSION"), git, date_time());
format!("pub const VERSION: &str = \"{}\";", version)
}
// date --help
fn date_time() -> String {
now_utc()
// .strftime("%Y-%m-%d/%H:%M:%SUTC")
.strftime("%Y-%m-%dUTC")
.map(|dt| dt.to_string())
.unwrap_or_default()
}
// git describe --always --abbrev=10 --dirty=-modified
fn commit_hash() -> io::Result<String> {
Cmd::new("git")
.args(&["describe", "--always", "--abbrev=8", "--dirty=-modified"])
.output()
.map(|o| decode(&o.stdout))
.map(|s| s.trim().to_string())
}
fn branch_name() -> io::Result<String> {
Cmd::new("git")
.args(&["rev-parse", "--abbrev-ref", "HEAD"])
.output()
.map(|o| decode(o.stdout.as_slice()).trim().to_string())
}
fn rustc_version() -> io::Result<String> {
Cmd::new("rustc")
.arg("--version")
.output()
.map(|o| decode_utf8_unchecked(o.stdout).trim().to_string())
}
fn decode_utf8_unchecked(bytes: Vec<u8>) -> String {
unsafe { String::from_utf8_unchecked(bytes) }
}
fn decode(bytes: &[u8]) -> String {
encoding_from_whatwg_label(charset2encoding(&detect(bytes).0))
.and_then(|code| code.decode(bytes, DecoderTrap::Strict).ok())
.unwrap_or_else(|| String::from_utf8_lossy(bytes).into_owned())
}