forked from cross-rs/cross
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
76 lines (65 loc) · 1.79 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
use std::env;
use std::error::Error;
use std::fs::{read_dir, File};
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
struct Some {}
impl<E> From<E> for Some
where E: Error
{
fn from(_: E) -> Some {
Some {}
}
}
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out_dir.join("commit-info.txt"))
.unwrap()
.write_all(commit_info().as_bytes())
.unwrap();
File::create(out_dir.join("docker-images.rs"))
.unwrap()
.write_all(docker_images().as_bytes())
.unwrap();
}
fn commit_info() -> String {
match (commit_hash(), commit_date()) {
(Ok(hash), Ok(date)) => format!(" ({} {})", hash.trim(), date.trim()),
_ => String::new(),
}
}
fn commit_hash() -> Result<String, Some> {
let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"])
.output()?;
if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(Some {})
}
}
fn commit_date() -> Result<String, Some> {
let output = Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()?;
if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(Some {})
}
}
fn docker_images() -> String {
let mut images = String::from("[");
let mut dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
dir.push("docker");
for entry in read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
images.push_str("\"");
images.push_str(path.file_name().unwrap().to_str().unwrap());
images.push_str("\", ");
}
}
images.push_str("]");
images
}