Skip to content

Commit 3872c8f

Browse files
committed
fix: rustify build script
1 parent d39e545 commit 3872c8f

File tree

2 files changed

+54
-76
lines changed

2 files changed

+54
-76
lines changed

stacks-common/build.rs

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,48 @@ use std::{env, fs};
44

55
use toml::Value;
66

7-
fn current_git_hash() -> Option<String> {
8-
if option_env!("GIT_COMMIT") == None {
9-
let commit = Command::new("git")
10-
.arg("log")
11-
.arg("-1")
12-
.arg("--pretty=format:%h") // Abbreviated commit hash
13-
.current_dir(env!("CARGO_MANIFEST_DIR"))
14-
.output();
15-
16-
if let Ok(commit) = commit {
17-
if let Ok(commit) = String::from_utf8(commit.stdout) {
18-
return Some(commit.trim().to_string());
19-
}
20-
}
21-
} else {
22-
return option_env!("GIT_COMMIT").map(String::from);
23-
}
7+
/// Given a [Command], run it and return the output as a string,
8+
/// returning `None` if the command fails.
9+
fn run_git_command(command: &mut Command) -> Option<String> {
10+
command
11+
.output()
12+
.map(|output| String::from_utf8(output.stdout).ok())
13+
.unwrap_or(None)
14+
.map(|s| s.trim().to_string())
15+
}
2416

25-
None
17+
fn current_git_hash() -> Option<String> {
18+
option_env!("GIT_COMMIT").map(String::from).or_else(|| {
19+
run_git_command(
20+
Command::new("git")
21+
.arg("log")
22+
.arg("-1")
23+
.arg("--pretty=format:%h")
24+
.current_dir(env!("CARGO_MANIFEST_DIR")),
25+
)
26+
})
2627
}
2728

2829
fn current_git_branch() -> Option<String> {
29-
if option_env!("GIT_BRANCH") == None {
30-
let commit = Command::new("git")
31-
.arg("rev-parse")
32-
.arg("--abbrev-ref")
33-
.arg("HEAD")
34-
.output();
35-
if let Ok(commit) = commit {
36-
if let Ok(commit) = String::from_utf8(commit.stdout) {
37-
return Some(commit.trim().to_string());
38-
}
39-
}
40-
} else {
41-
return option_env!("GIT_BRANCH").map(String::from);
42-
}
43-
44-
None
30+
option_env!("GIT_BRANCH").map(String::from).or_else(|| {
31+
run_git_command(
32+
Command::new("git")
33+
.arg("rev-parse")
34+
.arg("--abbrev-ref")
35+
.arg("HEAD"),
36+
)
37+
})
4538
}
4639

4740
fn is_working_tree_clean() -> bool {
48-
let status = Command::new("git")
41+
Command::new("git")
4942
.arg("diff")
5043
.arg("--quiet")
5144
.arg("--exit-code")
5245
.current_dir(env!("CARGO_MANIFEST_DIR"))
53-
.status();
54-
55-
if let Ok(status) = status {
56-
status.code() == Some(0)
57-
} else {
58-
true
59-
}
46+
.status()
47+
.map(|status| status.code() == Some(0))
48+
.unwrap_or(true)
6049
}
6150

6251
fn main() {
@@ -90,40 +79,29 @@ fn main() {
9079
}
9180
}
9281

93-
if let Some(git) = current_git_hash() {
94-
// println!("git commit: {}", git);
95-
rust_code.push_str(&format!(
96-
"pub const GIT_COMMIT: Option<&'static str> = Some(\"{}\");\n",
97-
git
98-
));
99-
println!("cargo:rustc-env=GIT_COMMIT={}", git);
100-
} else {
101-
rust_code.push_str(&format!(
102-
"pub const GIT_COMMIT: Option<&'static str> = None;\n"
103-
));
104-
}
105-
if let Some(git) = current_git_branch() {
106-
rust_code.push_str(&format!(
107-
"pub const GIT_BRANCH: Option<&'static str> = Some(\"{}\");\n",
108-
git
109-
));
110-
println!("cargo:rustc-env=GIT_BRANCH={}", git);
111-
} else {
112-
rust_code.push_str(&format!(
113-
"pub const GIT_BRANCH: Option<&'static str> = None;\n"
114-
));
82+
let git_commit = current_git_hash();
83+
rust_code.push_str(&format!(
84+
"pub const GIT_COMMIT: Option<&'static str> = {git_commit:?};\n",
85+
));
86+
if let Some(git_commit) = git_commit {
87+
println!("cargo:rustc-env=GIT_COMMIT={}", git_commit);
11588
}
116-
if !is_working_tree_clean() {
117-
rust_code.push_str(&format!(
118-
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"\");\n"
119-
));
120-
println!("cargo:rustc-env=GIT_TREE_CLEAN=+");
121-
} else {
122-
rust_code.push_str(&format!(
123-
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"+\");\n"
124-
));
89+
90+
let git_branch = current_git_branch();
91+
rust_code.push_str(&format!(
92+
"pub const GIT_BRANCH: Option<&'static str> = {git_branch:?};\n",
93+
));
94+
if let Some(git_branch) = git_branch {
95+
println!("cargo:rustc-env=GIT_BRANCH={}", git_branch);
12596
}
12697

98+
let is_clean = if is_working_tree_clean() { "" } else { "+" };
99+
rust_code.push_str(&format!(
100+
"pub const GIT_TREE_CLEAN: Option<&'static str> = Some(\"{}\");\n",
101+
is_clean
102+
));
103+
println!("cargo:rustc-env=GIT_TREE_CLEAN={}", is_clean);
104+
127105
let out_dir = env::var_os("OUT_DIR").unwrap();
128106
let dest_path = Path::new(&out_dir).join("versions.rs");
129107
fs::write(&dest_path, rust_code).expect("Failed to write generated code");

stackslib/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ const BUILD_TYPE: &'static str = "release";
8282

8383
pub fn version_string(pkg_name: &str, pkg_version: Option<&str>) -> String {
8484
let pkg_version = pkg_version.unwrap_or(STACKS_NODE_VERSION);
85-
let git_branch = GIT_BRANCH_ENV.unwrap_or(GIT_BRANCH.unwrap_or(""));
86-
let git_commit = GIT_COMMIT_ENV.unwrap_or(GIT_COMMIT.unwrap_or(""));
87-
let git_tree_clean = GIT_TREE_CLEAN_ENV.unwrap_or(GIT_TREE_CLEAN.unwrap_or(""));
85+
let git_branch = GIT_BRANCH_ENV.unwrap_or_else(|| GIT_BRANCH.unwrap_or(""));
86+
let git_commit = GIT_COMMIT_ENV.unwrap_or_else(|| GIT_COMMIT.unwrap_or(""));
87+
let git_tree_clean = GIT_TREE_CLEAN_ENV.unwrap_or_else(|| GIT_TREE_CLEAN.unwrap_or(""));
8888

8989
format!(
9090
"{} {} ({}:{}{}, {} build, {} [{}])",

0 commit comments

Comments
 (0)