Allow codegen to create new files

This commit is contained in:
Maybe Waffle 2022-09-25 20:16:00 +04:00
parent 26ec8b0c7b
commit a75c645c68

View file

@ -15,6 +15,7 @@ pub(crate) mod schema;
use std::{ use std::{
fs, fs,
io::{Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -79,25 +80,32 @@ pub fn ensure_files_contents<'a>(
) { ) {
let mut err_count = 0; let mut err_count = 0;
for (file, contents) in files_and_contents { for (path, contents) in files_and_contents {
if let Ok(old_contents) = fs::read_to_string(file) { let mut file = fs::File::options()
if normalize_newlines(&old_contents) == normalize_newlines(contents) { .read(true)
// File is already up to date. .write(true)
continue; .create(true)
} .open(path)
.unwrap();
let mut old_contents = String::with_capacity(contents.len());
file.read_to_string(&mut old_contents).unwrap();
err_count += 1; if normalize_newlines(&old_contents) == normalize_newlines(contents) {
// File is already up to date.
let display_path = file.strip_prefix(&project_root()).unwrap_or(file); continue;
eprintln!(
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
display_path.display()
);
if let Some(parent) = file.parent() {
let _ = fs::create_dir_all(parent);
}
fs::write(file, contents).unwrap();
} }
err_count += 1;
let display_path = path.strip_prefix(&project_root()).unwrap_or(path);
eprintln!(
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
display_path.display()
);
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
file.write_all(contents.as_bytes()).unwrap();
} }
let (s, were) = match err_count { let (s, were) = match err_count {