build.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. use std::env;
  13. use std::fs::File;
  14. use std::io::{self, Write};
  15. use std::path::PathBuf;
  16. use chrono::prelude::*;
  17. /// The build script entry point.
  18. fn main() -> io::Result<()> {
  19. #![allow(clippy::write_with_newline)]
  20. let tagline = "eza - A modern, maintained replacement for ls";
  21. let url = "https://github.com/eza-community/eza";
  22. let ver =
  23. if is_debug_build() {
  24. format!("{}\nv{} \\1;31m(pre-release debug build!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), url)
  25. }
  26. else if is_development_version() {
  27. format!("{}\nv{} [{}] built on {} \\1;31m(pre-release!)\\0m\n\\1;4;34m{}\\0m", tagline, version_string(), git_hash(), build_date(), url)
  28. }
  29. else {
  30. format!("{}\nv{}\n\\1;4;34m{}\\0m", tagline, version_string(), url)
  31. };
  32. // We need to create these files in the Cargo output directory.
  33. let out = PathBuf::from(env::var("OUT_DIR").unwrap());
  34. let path = &out.join("version_string.txt");
  35. // Bland version text
  36. let mut f = File::create(path).unwrap_or_else(|_| { panic!("{}", path.to_string_lossy().to_string()) });
  37. writeln!(f, "{}", strip_codes(&ver))?;
  38. Ok(())
  39. }
  40. /// Removes escape codes from a string.
  41. fn strip_codes(input: &str) -> String {
  42. input.replace("\\0m", "")
  43. .replace("\\1;31m", "")
  44. .replace("\\1;4;34m", "")
  45. }
  46. /// Retrieve the project’s current Git hash, as a string.
  47. fn git_hash() -> String {
  48. use std::process::Command;
  49. String::from_utf8_lossy(
  50. &Command::new("git")
  51. .args(["rev-parse", "--short", "HEAD"])
  52. .output().unwrap()
  53. .stdout).trim().to_string()
  54. }
  55. /// Whether we should show pre-release info in the version string.
  56. ///
  57. /// Both weekly releases and actual releases are --release releases,
  58. /// but actual releases will have a proper version number.
  59. fn is_development_version() -> bool {
  60. cargo_version().ends_with("-pre") || env::var("PROFILE").unwrap() == "debug"
  61. }
  62. /// Whether we are building in debug mode.
  63. fn is_debug_build() -> bool {
  64. env::var("PROFILE").unwrap() == "debug"
  65. }
  66. /// Retrieves the [package] version in Cargo.toml as a string.
  67. fn cargo_version() -> String {
  68. env::var("CARGO_PKG_VERSION").unwrap()
  69. }
  70. /// Returns the version and build parameters string.
  71. fn version_string() -> String {
  72. let mut ver = cargo_version();
  73. let feats = nonstandard_features_string();
  74. if ! feats.is_empty() {
  75. ver.push_str(&format!(" [{}]", &feats));
  76. }
  77. ver
  78. }
  79. /// Finds whether a feature is enabled by examining the Cargo variable.
  80. fn feature_enabled(name: &str) -> bool {
  81. env::var(format!("CARGO_FEATURE_{}", name))
  82. .map(|e| ! e.is_empty())
  83. .unwrap_or(false)
  84. }
  85. /// A comma-separated list of non-standard feature choices.
  86. fn nonstandard_features_string() -> String {
  87. let mut s = Vec::new();
  88. if feature_enabled("GIT") {
  89. s.push("+git");
  90. }
  91. else {
  92. s.push("-git");
  93. }
  94. s.join(", ")
  95. }
  96. /// Formats the current date as an ISO 8601 string.
  97. fn build_date() -> String {
  98. let now = Local::now();
  99. now.date_naive().format("%Y-%m-%d").to_string()
  100. }