build.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /// The version string isn’t the simplest: we want to show the version,
  2. /// current Git hash, and compilation date when building *debug* versions, but
  3. /// just the version for *release* versions so the builds are reproducible.
  4. ///
  5. /// This script generates the string from the environment variables that Cargo
  6. /// adds (http://doc.crates.io/environment-variables.html) and runs `git` to
  7. /// get the SHA1 hash. It then writes the string into a file, which exa then
  8. /// includes at build-time.
  9. ///
  10. /// - https://stackoverflow.com/q/43753491/3484614
  11. /// - https://crates.io/crates/vergen
  12. extern crate datetime;
  13. use std::env;
  14. use std::io::Result as IOResult;
  15. fn git_hash() -> String {
  16. use std::process::Command;
  17. String::from_utf8_lossy(
  18. &Command::new("git")
  19. .args(&["rev-parse", "--short", "HEAD"])
  20. .output().unwrap()
  21. .stdout).trim().to_string()
  22. }
  23. fn main() {
  24. write_statics().unwrap();
  25. }
  26. fn is_development_version() -> bool {
  27. // Both weekly releases and actual releases are --release releases,
  28. // but actual releases will have a proper version number
  29. cargo_version().ends_with("-pre") || env::var("PROFILE").unwrap() == "debug"
  30. }
  31. fn cargo_version() -> String {
  32. env::var("CARGO_PKG_VERSION").unwrap()
  33. }
  34. fn build_date() -> String {
  35. use datetime::{LocalDateTime, ISO};
  36. let now = LocalDateTime::now();
  37. format!("{}", now.date().iso())
  38. }
  39. fn write_statics() -> IOResult<()> {
  40. use std::fs::File;
  41. use std::io::Write;
  42. use std::path::PathBuf;
  43. let ver = if is_development_version() {
  44. format!("exa v{} ({} built on {})", cargo_version(), git_hash(), build_date())
  45. }
  46. else {
  47. format!("exa v{}", cargo_version())
  48. };
  49. let out = PathBuf::from(env::var("OUT_DIR").unwrap());
  50. let mut f = File::create(&out.join("version_string.txt"))?;
  51. write!(f, "{:?}", ver)
  52. }