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.

24 lines
1.0 KiB

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(file_name_without_extension.file_name().unwrap().to_string_lossy()).to_uppercase()
  10. }
  11. pub fn create_static_output_files(source_dir: &str) {
  12. let out_dir = env::var("OUT_DIR").unwrap();
  13. let dest_path = Path::new(&out_dir).join("static_files.rs");
  14. let mut f = File::create(&dest_path).unwrap();
  15. for maybe_dir_entry in read_dir(source_dir).unwrap() {
  16. let file_path = maybe_dir_entry.unwrap().path();
  17. let mut buffer = String::new();
  18. File::open(file_path.clone()).unwrap().read_to_string(&mut buffer).unwrap();
  19. let fence = buffer.chars().filter(|c| *c == '#').collect::<String>() + "#";
  20. f.write_all(format!("pub const {}: &'static str = r{1}\"{2}\"{1};\n", get_const_name(&file_path), fence, buffer).as_bytes()).unwrap();
  21. }
  22. }