build.rs 3.6 KB

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