-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
58 lines (49 loc) · 1.9 KB
/
build.rs
File metadata and controls
58 lines (49 loc) · 1.9 KB
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
use std::env;
use std::fs;
use std::process::Command;
fn set_git_version(){
let version = env::var("CARGO_PKG_VERSION").unwrap();
let child = Command::new("git").args(["describe", "--always"]).output();
match child {
Ok(child) if child.status.success() => {
let buf = String::from_utf8(child.stdout).expect("failed to read stdout");
println!("cargo:rustc-env=VERSION={version}-{buf}");
}
_ => {
eprintln!("`git describe` failed, using CARGO_PKG_VERSION version");
println!("cargo:rustc-env=VERSION={version}");
}
}
}
fn set_update_checker() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
let git_config = format!("{}/.git/config", manifest_dir);
let (mut owner, mut repo) = ("unknown".to_string(), "unknown".to_string());
if let Ok(content) = fs::read_to_string(&git_config) {
if let Some(line) = content.lines().find(|l| l.trim().starts_with("url =")) {
let url = line.trim()["url =".len()..].trim();
// 1. git@github.com:owner/repo.git
// 2. https://github.com/owner/repo.git
// 3. git://github.com/owner/repo.git
let repo_part = if let Some(idx) = url.find("github.com") {
&url[idx + "github.com/".len()..]
}else {
""
};
let repo_part = repo_part.trim_end_matches(".git");
let mut parts = repo_part.splitn(2, '/');
if let (Some(o), Some(r)) = (parts.next(), parts.next()) {
if !o.is_empty() && !r.is_empty() {
owner = o.to_string();
repo = r.to_string();
}
}
}
}
println!("cargo:rustc-env=GIT_OWNER={owner}");
println!("cargo:rustc-env=GIT_REPO={repo}");
}
fn main() {
set_git_version();
set_update_checker();
}