mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-11-22 12:55:13 +00:00
Compile-time warning when git is missing (#187)
This commit is contained in:
parent
5cb091d30a
commit
7d9ca0de43
54
build.rs
54
build.rs
@ -1,16 +1,50 @@
|
|||||||
|
use std::env;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
fn main() {
|
macro_rules! warn {
|
||||||
let git_describe = Command::new("git")
|
($fmt:tt) => ({
|
||||||
.args(&["describe", "--tags", "--broken"])
|
::std::println!(::std::concat!("cargo:warning=", $fmt));
|
||||||
.output()
|
});
|
||||||
.ok()
|
($fmt:tt, $($arg:tt)*) => ({
|
||||||
.and_then(|proc| String::from_utf8(proc.stdout).ok());
|
::std::println!(::std::concat!("cargo:warning=", $fmt), $($arg)*);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let version_info = match git_describe {
|
fn git_version() -> Option<String> {
|
||||||
Some(description) if !description.is_empty() => description,
|
let mut git = Command::new("git");
|
||||||
_ => format!("v{}", env!("CARGO_PKG_VERSION")),
|
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user