mod git_ignore;
use crate::codegen::track_autogen::track_autogen;
use git_ignore::{git_ignore, git_rm};
use path_abs::PathAbs;
use std::fs;
use std::path::Path;
pub struct OutputConfig<'a> {
pub code: &'a str,
pub file_path: &'a str,
pub git_ignore: bool,
pub cargo_track: bool,
}
pub fn output_code(cfg: &OutputConfig) {
let file_pathabs = PathAbs::new(Path::new(cfg.file_path))
.unwrap_or_else(|_| panic!("Could not get absolute path for {}", cfg.file_path));
let file_absolute = file_pathabs.as_path().to_str().unwrap();
let file_parent = file_pathabs
.as_path()
.parent()
.unwrap_or_else(|| panic!("Could not get parent directory for {}", file_absolute));
fs::create_dir_all(file_parent).unwrap_or_else(|_| {
panic!(
"Could not create intermediate directories for {}",
file_absolute
)
});
git_rm(&file_absolute);
fs::write(file_absolute, cfg.code)
.unwrap_or_else(|_| panic!("Couldn't output generated code to {}", file_absolute));
track_autogen(file_pathabs.as_path().to_str().unwrap().to_owned());
if cfg.git_ignore {
git_ignore(&file_pathabs).unwrap_or_else(|_| panic!("Could not ignore {}", file_absolute));
}
if cfg.cargo_track {
println!("cargo:rerun-if-changed={}", cfg.file_path);
} else {
println!("Generated {}", cfg.file_path);
}
}