forked from ajeetdsouza/zoxide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile-time warning when git is missing (ajeetdsouza#187)
- Loading branch information
1 parent
5cb091d
commit 7d9ca0d
Showing
1 changed file
with
44 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,50 @@ | ||
use std::env; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
let git_describe = Command::new("git") | ||
.args(&["describe", "--tags", "--broken"]) | ||
.output() | ||
.ok() | ||
.and_then(|proc| String::from_utf8(proc.stdout).ok()); | ||
macro_rules! warn { | ||
($fmt:tt) => ({ | ||
::std::println!(::std::concat!("cargo:warning=", $fmt)); | ||
}); | ||
($fmt:tt, $($arg:tt)*) => ({ | ||
::std::println!(::std::concat!("cargo:warning=", $fmt), $($arg)*); | ||
}); | ||
} | ||
|
||
let version_info = match git_describe { | ||
Some(description) if !description.is_empty() => description, | ||
_ => format!("v{}", env!("CARGO_PKG_VERSION")), | ||
fn git_version() -> Option<String> { | ||
let mut git = Command::new("git"); | ||
git.args(&["describe", "--tags", "--broken"]); | ||
|
||
let output = match git.output() { | ||
Err(e) => { | ||
warn!("when retrieving version: git failed to start: {}", e); | ||
return None; | ||
} | ||
Ok(output) if !output.status.success() => { | ||
warn!( | ||
"when retrieving version: git exited with code: {:?}", | ||
output.status.code() | ||
); | ||
return None; | ||
} | ||
Ok(output) => output, | ||
}; | ||
|
||
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version_info); | ||
match String::from_utf8(output.stdout) { | ||
Ok(version) => Some(version), | ||
Err(e) => { | ||
warn!("when retrieving version: git returned invalid utf-8: {}", e); | ||
None | ||
} | ||
} | ||
} | ||
|
||
fn crate_version() -> String { | ||
warn!("falling back to crate version"); | ||
// unwrap is safe here, since Cargo will always supply this variable. | ||
format!("v{}", env::var("CARGO_PKG_VERSION").unwrap()) | ||
} | ||
|
||
fn main() { | ||
let version = git_version().unwrap_or_else(crate_version); | ||
println!("cargo:rustc-env=ZOXIDE_VERSION={}", version); | ||
} |