use anyhow::Result;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
pub fn create_file(path: &Path) -> Result<File> {
if let Some(p) = path.parent() {
fs::create_dir_all(p)?;
}
File::create(path).map_err(Into::into)
}
pub fn write_file<P: AsRef<Path>>(build_dir: &Path, filename: P, content: &[u8]) -> Result<()> {
let path = build_dir.join(filename);
create_file(&path)?.write_all(content).map_err(Into::into)
}
pub fn copy_dirs<P: AsRef<Path>>(source: P, target: P) -> std::io::Result<()> {
fs::create_dir_all(&target)?;
for entry in fs::read_dir(source)? {
let entry = entry?;
let entry_target = target.as_ref().join(entry.file_name());
if entry.file_type()?.is_dir() {
copy_dirs(entry.path(), entry_target)?;
} else {
fs::copy(entry.path(), entry_target)?;
}
}
Ok(())
}