Fix built-in template rename order
This commit is contained in:
70
build.rs
Normal file
70
build.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use std::{
|
||||
env, fs, io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=examples");
|
||||
|
||||
let examples_root = PathBuf::from("examples");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is not set"));
|
||||
let out_file = out_dir.join("embedded_examples.rs");
|
||||
|
||||
let mut dirs = Vec::new();
|
||||
let mut files = Vec::new();
|
||||
|
||||
if examples_root.is_dir() {
|
||||
collect_examples(&examples_root, &examples_root, &mut dirs, &mut files)
|
||||
.expect("failed to collect example templates");
|
||||
}
|
||||
|
||||
dirs.sort();
|
||||
files.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
|
||||
let mut generated = String::new();
|
||||
generated.push_str(
|
||||
"struct EmbeddedFile {\n path: &'static str,\n contents: &'static [u8],\n}\n\n",
|
||||
);
|
||||
generated.push_str("static EMBEDDED_EXAMPLE_DIRS: &[&str] = &[\n");
|
||||
for dir in &dirs {
|
||||
generated.push_str(&format!(" {:?},\n", dir));
|
||||
}
|
||||
generated.push_str("];\n\n");
|
||||
generated.push_str("static EMBEDDED_EXAMPLE_FILES: &[EmbeddedFile] = &[\n");
|
||||
for (relative_path, absolute_path) in &files {
|
||||
generated.push_str(&format!(
|
||||
" EmbeddedFile {{ path: {:?}, contents: include_bytes!(r#\"{}\"#) }},\n",
|
||||
relative_path,
|
||||
absolute_path.display()
|
||||
));
|
||||
}
|
||||
generated.push_str("];\n");
|
||||
|
||||
fs::write(out_file, generated).expect("failed to write embedded example list");
|
||||
}
|
||||
|
||||
fn collect_examples(
|
||||
root: &Path,
|
||||
dir: &Path,
|
||||
dirs: &mut Vec<String>,
|
||||
files: &mut Vec<(String, PathBuf)>,
|
||||
) -> io::Result<()> {
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
let relative_path = path
|
||||
.strip_prefix(root)
|
||||
.expect("example path is outside examples root")
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
|
||||
if path.is_dir() {
|
||||
dirs.push(relative_path);
|
||||
collect_examples(root, &path, dirs, files)?;
|
||||
} else if path.is_file() {
|
||||
files.push((relative_path, path.canonicalize()?));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user