-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
133 lines (117 loc) · 4.37 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 regex::Regex;
use tonic_build;
use walkdir::WalkDir;
fn main() {
// add clang executalbe dir to PATH env
// set LIBCLANG_PATH env to dir which contains libclang
trigger_build_every_time();
replace_version_in_rs(
"true" == std::env::var("UPDATE_ALL_FILES").unwrap_or(String::from("false").to_lowercase()),
);
println!("cargo:rerun-if-changed=src/proto/gss.proto");
tonic_build::configure()
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile_well_known_types(true)
.compile(&["src/proto/gss.proto"], &["proto"])
.unwrap();
}
fn trigger_build_every_time() {
// write build_number.txt to trigger build.rs
let build_number_file = "build_number.txt";
let count = if !std::path::Path::new(build_number_file).exists() {
1
} else {
let content = std::fs::read_to_string(build_number_file).unwrap();
content.parse::<u32>().unwrap_or(0) + 1
};
std::fs::write(build_number_file, count.to_string()).expect("Unable to write file");
println!("cargo:rerun-if-changed={}", build_number_file);
}
fn replace_version_in_rs(update_all_files: bool) {
let latest_version = format!(
"{}.{}",
get_latest_git_commit_hash(true),
get_latest_git_commit_time()
);
// Replace version string in .rs files
let version_regex =
Regex::new(r#"pub static APP_VERSION: &str = "([0-9a-f]{7})\.(\d{8})\.(\d{6})";"#).unwrap();
let version_replacement = format!(r#"pub static APP_VERSION: &str = "{}";"#, latest_version);
let files = if update_all_files {
find_rs_files()
} else {
let file = String::from("./src/version.rs");
if !std::path::Path::new(&file).exists() {
let mut text = String::from(r#"pub static APP_NAME: &str = "gbt_stream_server";"#);
text += "\n";
text += &version_replacement.to_string();
text += "\n";
std::fs::write(&file, text).expect("std::fs::write error");
}
vec![file]
};
for file in files {
println!("file: {}", &file);
let original_content = std::fs::read_to_string(&file).expect("Failed to read file");
let replaced_content = version_regex.replace_all(&original_content, &version_replacement);
if original_content != replaced_content {
println!(
"std::fs::write, file: {}, version: {}",
&file, &latest_version
);
std::fs::write(&file, replaced_content.as_ref()).expect("std::fs::write error");
}
}
}
fn get_latest_git_commit_hash(short: bool) -> String {
// Run Git command to get the latest commit hash
let output = std::process::Command::new("git")
.args(&[
"log",
"-1",
if short {
"--pretty=format:%h"
} else {
"--pretty=format:%H"
},
])
.output()
.expect("std::process::Command::new(git log) error");
return String::from_utf8_lossy(&output.stdout).trim().to_string();
}
fn get_latest_git_commit_time() -> String {
// Run Git command to get the latest commit hash
let output = std::process::Command::new("git")
.args(&["log", "-1", "--format=%ad", "--date=format:%Y%m%d.%H%M%S"])
.output()
.expect("std::process::Command::new(git log) error");
return String::from_utf8_lossy(&output.stdout).trim().to_string();
}
fn find_rs_files() -> Vec<String> {
let mut files = Vec::new();
let walker = WalkDir::new("./src")
.into_iter()
.filter_entry(|e| !is_hidden(e)) // skip hidden files
.filter_map(|e| e.ok());
for entry in walker {
let path = entry.path();
if let Some(ext) = path.extension() {
if ext == "rs" {
if let Some(file_name) = path.to_str() {
// skip build.rs
if !file_name.ends_with("build.rs") {
files.push(file_name.to_owned());
}
}
}
}
}
files
}
fn is_hidden(entry: &walkdir::DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| s.starts_with('.'))
.unwrap_or(false)
}