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.

42 lines
1.1 KiB

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