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.

54 lines
1.3 KiB

6 years ago
6 years ago
6 years ago
2 years ago
6 years ago
2 years ago
2 years ago
6 years ago
6 years ago
  1. use std::env;
  2. use std::error::Error;
  3. use std::fs::{read as read_file, File};
  4. use std::io::ErrorKind::NotFound;
  5. use std::io::Write;
  6. use std::path::Path;
  7. pub fn create_static_output(
  8. source_path: &Path,
  9. mut target: impl Write,
  10. ) -> Result<(), Box<dyn Error>> {
  11. let const_name = source_path
  12. .file_stem()
  13. .ok_or("Not a filename")?
  14. .to_str()
  15. .ok_or("Filename is not valid unicode")?
  16. .to_uppercase();
  17. let file = read_file(source_path)?;
  18. let content = std::str::from_utf8(&file)?;
  19. let fence = content.chars().filter(|&c| c == '#').collect::<String>() + "#";
  20. writeln!(
  21. target,
  22. "pub const {const_name}: &str = r{fence}\"{content}\"{fence};"
  23. )?;
  24. Ok(())
  25. }
  26. pub fn create_static_output_files(
  27. source_dir: &Path,
  28. dest_path: &Path,
  29. ) -> Result<(), Box<dyn Error>> {
  30. let mut f = File::create(dest_path)?;
  31. match source_dir.read_dir() {
  32. Ok(dir_content) => {
  33. for maybe_dir_entry in dir_content {
  34. create_static_output(&maybe_dir_entry?.path(), &mut f)?;
  35. }
  36. }
  37. Err(err) => {
  38. if err.kind() != NotFound {
  39. return Err(format!("Unexpected error: {err}").into());
  40. }
  41. }
  42. }
  43. Ok(())
  44. }
  45. pub fn main() -> Result<(), Box<dyn Error>> {
  46. create_static_output_files(
  47. Path::new("static_files"),
  48. &(Path::new(&env::var("OUT_DIR")?).join("static_files.rs")),
  49. )
  50. }