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

use std::env;
use std::error::Error;
use std::fs::{read as read_file, File};
use std::io::ErrorKind::NotFound;
use std::io::Write;
use std::path::Path;
pub fn create_static_output(
source_path: &Path,
mut target: impl Write,
) -> Result<(), Box<dyn Error>> {
let const_name = source_path
.file_stem()
.ok_or("Not a filename")?
.to_str()
.ok_or("Filename is not valid unicode")?
.to_uppercase();
let file = read_file(source_path)?;
let content = std::str::from_utf8(&file)?;
let fence = content.chars().filter(|&c| c == '#').collect::<String>() + "#";
writeln!(
target,
"pub const {const_name}: &str = r{fence}\"{content}\"{fence};"
)?;
Ok(())
}
pub fn create_static_output_files(
source_dir: &Path,
dest_path: &Path,
) -> Result<(), Box<dyn Error>> {
let mut f = File::create(dest_path)?;
match source_dir.read_dir() {
Ok(dir_content) => {
for maybe_dir_entry in dir_content {
create_static_output(&maybe_dir_entry?.path(), &mut f)?;
}
}
Err(err) => {
if err.kind() != NotFound {
return Err(format!("Unexpected error: {err}").into());
}
}
}
Ok(())
}
pub fn main() -> Result<(), Box<dyn Error>> {
create_static_output_files(
Path::new("static_files"),
&(Path::new(&env::var("OUT_DIR")?).join("static_files.rs")),
)
}