build.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //! lovingly stolen from https://github.com/rust-lang-nursery/rustup.rs/blob/master/build.rs
  2. use std::env;
  3. use std::error::Error;
  4. use std::fs::File;
  5. use std::io::Write;
  6. use std::path::PathBuf;
  7. use std::process::{self, Command};
  8. struct Ignore;
  9. impl<E> From<E> for Ignore
  10. where
  11. E: Error,
  12. {
  13. fn from(_: E) -> Ignore {
  14. Ignore
  15. }
  16. }
  17. fn main() {
  18. if let Err(_) = Command::new("sass").status() {
  19. eprintln!(
  20. "build error: sass compiler not installed.\n\
  21. please follow the instructions at https://sass-lang.com/install, \
  22. making sure the `sass` executable is in your $PATH."
  23. );
  24. process::exit(1);
  25. }
  26. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  27. File::create(out_dir.join("commit-info.txt"))
  28. .unwrap()
  29. .write_all(commit_info().as_bytes())
  30. .unwrap();
  31. Command::new("sass")
  32. .args(&["style/style.scss", "static/style.css"])
  33. .status()
  34. .unwrap();
  35. }
  36. // Try to get hash and date of the last commit on a best effort basis. If anything goes wrong
  37. // (git not installed or if this is not a git repository) just return an empty string.
  38. fn commit_info() -> String {
  39. match commit_hash() {
  40. Ok(hash) => hash.trim_right().to_owned(),
  41. _ => String::new(),
  42. }
  43. }
  44. fn commit_hash() -> Result<String, Ignore> {
  45. Ok(String::from_utf8(
  46. Command::new("git")
  47. .args(&["rev-parse", "--short=9", "HEAD"])
  48. .output()?
  49. .stdout,
  50. )?)
  51. }