mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Allow codegen to create new files
This commit is contained in:
parent
26ec8b0c7b
commit
a75c645c68
1 changed files with 25 additions and 17 deletions
|
@ -15,6 +15,7 @@ pub(crate) mod schema;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
|
io::{Read, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -79,8 +80,16 @@ 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()
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.create(true)
|
||||||
|
.open(path)
|
||||||
|
.unwrap();
|
||||||
|
let mut old_contents = String::with_capacity(contents.len());
|
||||||
|
file.read_to_string(&mut old_contents).unwrap();
|
||||||
|
|
||||||
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
|
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
|
||||||
// File is already up to date.
|
// File is already up to date.
|
||||||
continue;
|
continue;
|
||||||
|
@ -88,16 +97,15 @@ pub fn ensure_files_contents<'a>(
|
||||||
|
|
||||||
err_count += 1;
|
err_count += 1;
|
||||||
|
|
||||||
let display_path = file.strip_prefix(&project_root()).unwrap_or(file);
|
let display_path = path.strip_prefix(&project_root()).unwrap_or(path);
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
|
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
|
||||||
display_path.display()
|
display_path.display()
|
||||||
);
|
);
|
||||||
if let Some(parent) = file.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
let _ = fs::create_dir_all(parent);
|
let _ = fs::create_dir_all(parent);
|
||||||
}
|
}
|
||||||
fs::write(file, contents).unwrap();
|
file.write_all(contents.as_bytes()).unwrap();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (s, were) = match err_count {
|
let (s, were) = match err_count {
|
||||||
|
|
Loading…
Reference in a new issue