-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
151 lines (129 loc) · 4.36 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use inner::Info;
use std::env;
use std::fs::File;
use std::io::{self, BufWriter};
use std::path::Path;
fn main() {
let mut info = Info::new();
generate_version_short(&mut info);
generate_version_long(&mut info);
println!("cargo:rerun-if-env-changed=NIGHTLY_BUILD");
if let Ok(date) = std::env::var("NIGHTLY_BUILD") {
println!(
"cargo:rustc-env=CARGO_PKG_VERSION={}-nightly.{}",
std::env::var("CARGO_PKG_VERSION")
.unwrap()
.split('-')
.next()
.unwrap(),
date,
);
}
}
fn generate_version_short(info: &mut Info) {
let dst =
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("version_short.txt")).unwrap();
let mut dst = BufWriter::new(dst);
io::copy(&mut io::Cursor::new(version_short(info)), &mut dst).unwrap();
}
fn generate_version_long(info: &mut Info) {
let dst =
File::create(Path::new(&env::var("OUT_DIR").unwrap()).join("version_long.txt")).unwrap();
let mut dst = BufWriter::new(dst);
io::copy(&mut io::Cursor::new(version_long(info)), &mut dst).unwrap();
}
fn version_short(info: &mut Info) -> String {
let mut version = env!("CARGO_PKG_VERSION").to_string();
if let Some(hash) = info.commit_hash_short() {
let mut extra = String::new();
extra.push_str(" (");
extra.push_str(hash);
extra.push(' ');
if let Some(date) = info.commit_date() {
extra.push_str(date);
extra.push(')');
version.push_str(&extra)
}
}
version
}
fn version_long(info: &mut Info) -> String {
let mut version = version_short(info);
version.push_str("\nbinary: ");
version.push_str(env!("CARGO_PKG_NAME"));
version.push_str("\nrelease: ");
version.push_str(env!("CARGO_PKG_VERSION"));
if let Some(hash) = info.commit_hash_long() {
version.push_str("\ncommit-hash: ");
version.push_str(hash);
}
if let Some(date) = info.commit_date() {
version.push_str("\ncommit-date: ");
version.push_str(date);
}
version
}
mod inner {
use std::collections::HashMap;
use std::env;
use std::process::Command;
pub struct Info(HashMap<&'static str, Option<String>>);
impl Info {
pub fn new() -> Self {
Info(HashMap::new())
}
pub fn commit_hash_short(&mut self) -> Option<&str> {
self.0
.entry("commit_hash_short")
.or_insert_with(commit_hash_short)
.as_deref()
}
pub fn commit_hash_long(&mut self) -> Option<&str> {
self.0
.entry("commit_hash_long")
.or_insert_with(commit_hash_long)
.as_deref()
}
pub fn commit_date(&mut self) -> Option<&str> {
self.0
.entry("commit_date")
.or_insert_with(commit_date)
.as_deref()
}
}
pub fn commit_hash_short() -> Option<String> {
let hash = command_stdout(Command::new(git()).args(&["show", "-s", "--format=%h"]));
match is_dirty() {
Some(id) if id => hash.map(|hash| format!("{}-dirty", hash)),
_ => hash,
}
}
pub fn commit_hash_long() -> Option<String> {
let hash = command_stdout(Command::new(git()).args(&["show", "-s", "--format=%H"]));
match is_dirty() {
Some(id) if id => hash.map(|hash| format!("{}-dirty", hash)),
_ => hash,
}
}
pub fn commit_date() -> Option<String> {
command_stdout(Command::new(git()).args(&["show", "-s", "--format=%ad", "--date=short"]))
}
pub fn is_dirty() -> Option<bool> {
Command::new(git())
.args(&["diff-index", "--quiet", "HEAD"])
.status()
.ok()
.map(|status| !status.success())
}
fn git() -> String {
env::var("GIT_CMD").unwrap_or_else(|_| "git".to_string())
}
fn command_stdout(cmd: &mut Command) -> Option<String> {
cmd.output()
.ok()
.map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
}
}