A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
  1. use std::env;
  2. use std::fs::{read_dir, File};
  3. use std::io::ErrorKind::NotFound;
  4. use std::io::Read;
  5. use std::io::Write;
  6. use std::path::{Path, PathBuf};
  7. fn get_const_name<P: Clone + Into<PathBuf>>(p: &P) -> String {
  8. let mut file_name_without_extension = p.clone().into();
  9. file_name_without_extension.set_extension("");
  10. String::from(
  11. file_name_without_extension
  12. .file_name()
  13. .unwrap()
  14. .to_string_lossy(),
  15. )
  16. .to_uppercase()
  17. }
  18. pub fn create_static_output_files(source_dir: &str) {
  19. let out_dir = env::var("OUT_DIR").unwrap();
  20. let dest_path = Path::new(&out_dir).join("static_files.rs");
  21. let mut f = File::create(&dest_path).unwrap();
  22. match read_dir(source_dir) {
  23. Ok(dir_content) => {
  24. for maybe_dir_entry in dir_content {
  25. let file_path = maybe_dir_entry.unwrap().path();
  26. let mut buffer = String::new();
  27. File::open(file_path.clone())
  28. .unwrap()
  29. .read_to_string(&mut buffer)
  30. .unwrap();
  31. let fence = buffer.chars().filter(|c| *c == '#').collect::<String>() + "#";
  32. f.write_all(
  33. format!(
  34. "pub const {}: &str = r{1}\"{2}\"{1};\n",
  35. get_const_name(&file_path),
  36. fence,
  37. buffer
  38. )
  39. .as_bytes(),
  40. )
  41. .unwrap();
  42. }
  43. }
  44. Err(err) => {
  45. if err.kind() == NotFound {
  46. } else {
  47. Err(err).unwrap()
  48. }
  49. }
  50. }
  51. }
  52. #[allow(unused)]
  53. fn main() {
  54. create_static_output_files("static_files");
  55. }