-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
133 lines (108 loc) · 3.39 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
use random_string::charsets::ALPHANUMERIC;
use random_string::generate;
use std::env;
use std::process::Command;
fn main() {
set_version();
set_repo_url();
}
fn set_repo_url() {
let cargo_repo_url = env!("CARGO_PKG_REPOSITORY");
let mut repo_url = cargo_repo_url.to_string();
if is_git_repo() {
repo_url = get_remote_url();
if repo_url.starts_with("https://github.com/") {
// The URL might already end with a '/', but GitHub seems to handle it fine if there's two.
// Tested in both Firefox and Chromium.
repo_url.push_str(&format!(
"/tree/{}",
if is_git_tagged() {
get_git_tag()
} else {
get_git_hash()
}
));
}
} else {
println!("Not a Git repo! Skipping repo URL metadata");
}
println!("cargo:rustc-env=IN_REPO_URL={}", repo_url);
}
fn set_version() {
let cargo_version = env!("CARGO_PKG_VERSION");
let mut version = cargo_version.to_string();
if is_git_repo() {
let is_repo_dirty = is_repo_dirty();
if !is_git_tagged() || is_repo_dirty {
version.push_str("+rev.");
version.push_str(&get_git_hash());
// Add some randomness if dirty to avoid the browser caching resources while iterating.
if is_repo_dirty {
version.push_str("-dirty-");
version.push_str(&generate(4, ALPHANUMERIC));
}
}
} else {
println!("Not a Git repo! Skipping version metadata");
}
println!("cargo:rustc-env=IN_VERSION={}", version);
}
fn is_git_repo() -> bool {
if let Ok(var) = env::var("IN_IS_GIT") {
return var == "true";
}
Command::new("git").args(["status"]).status().is_ok()
}
fn get_git_hash() -> String {
if let Ok(var) = env::var("IN_GIT_HASH") {
return var;
}
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.expect("Failed to execute git command");
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
fn is_repo_dirty() -> bool {
if let Ok(var) = env::var("IN_GIT_DIRTY") {
return var == "true";
}
let status_output = Command::new("git")
.args(["status", "--porcelain"])
.output()
.expect("Failed to execute git status command");
!status_output.stdout.is_empty()
}
fn is_git_tagged() -> bool {
if let Ok(var) = env::var("IN_GIT_TAGGED") {
return var == "true";
}
get_git_tag_info().0
}
fn get_git_tag() -> String {
if let Ok(var) = env::var("IN_GIT_TAG") {
return var;
}
get_git_tag_info().1
}
fn get_git_tag_info() -> (bool, String) {
let output = Command::new("git")
.args(["describe", "--tags", "--exact-match"])
.output()
.expect("Failed to execute git describe command");
let is_tagged = output.status.success();
let tag = String::from_utf8_lossy(&output.stdout).trim().to_string();
(is_tagged, tag)
}
fn get_remote_url() -> String {
if let Ok(var) = env::var("IN_GIT_REMOTE_URL") {
return var;
}
let remote_url = Command::new("git")
.args(["remote", "get-url", "origin"])
.output()
.expect("Failed to execute git command");
String::from_utf8_lossy(&remote_url.stdout)
.trim()
.to_string()
}