Rewrite build.rs
This commit is contained in:
parent
e4af85726f
commit
ae5d6837af
1 changed files with 36 additions and 39 deletions
75
src/build.rs
75
src/build.rs
|
|
@ -1,57 +1,54 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{read_dir, File};
|
use std::error::Error;
|
||||||
|
use std::fs::{read as read_file, File};
|
||||||
use std::io::ErrorKind::NotFound;
|
use std::io::ErrorKind::NotFound;
|
||||||
use std::io::Read;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::Path;
|
||||||
|
|
||||||
fn get_const_name<P: Clone + Into<PathBuf>>(p: &P) -> String {
|
pub fn create_static_output(
|
||||||
let mut file_name_without_extension = p.clone().into();
|
source_path: &Path,
|
||||||
file_name_without_extension.set_extension("");
|
mut target: impl Write,
|
||||||
String::from(
|
) -> Result<(), Box<dyn Error>> {
|
||||||
file_name_without_extension
|
let const_name = source_path
|
||||||
.file_name()
|
.file_stem()
|
||||||
.unwrap()
|
.ok_or("Not a filename")?
|
||||||
.to_string_lossy(),
|
.to_str()
|
||||||
)
|
.ok_or("Filename is not valid unicode")?
|
||||||
.to_uppercase()
|
.to_uppercase();
|
||||||
|
let content = String::from_utf8(read_file(&source_path)?)?;
|
||||||
|
let fence = content.chars().filter(|&c| c == '#').collect::<String>() + "#";
|
||||||
|
|
||||||
|
writeln!(
|
||||||
|
target,
|
||||||
|
"pub const {}: &str = r{1}\"{2}\"{1};",
|
||||||
|
const_name, fence, content
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_static_output_files(source_dir: &str) {
|
pub fn create_static_output_files(
|
||||||
let out_dir = env::var("OUT_DIR").unwrap();
|
source_dir: &Path,
|
||||||
let dest_path = Path::new(&out_dir).join("static_files.rs");
|
dest_path: &Path,
|
||||||
let mut f = File::create(&dest_path).unwrap();
|
) -> Result<(), Box<dyn Error>> {
|
||||||
match read_dir(source_dir) {
|
let mut f = File::create(dest_path)?;
|
||||||
|
match source_dir.read_dir() {
|
||||||
Ok(dir_content) => {
|
Ok(dir_content) => {
|
||||||
for maybe_dir_entry in dir_content {
|
for maybe_dir_entry in dir_content {
|
||||||
let file_path = maybe_dir_entry.unwrap().path();
|
create_static_output(&maybe_dir_entry?.path(), &mut f)?;
|
||||||
let mut buffer = String::new();
|
|
||||||
File::open(file_path.clone())
|
|
||||||
.unwrap()
|
|
||||||
.read_to_string(&mut buffer)
|
|
||||||
.unwrap();
|
|
||||||
let fence = buffer.chars().filter(|c| *c == '#').collect::<String>() + "#";
|
|
||||||
f.write_all(
|
|
||||||
format!(
|
|
||||||
"pub const {}: &str = r{1}\"{2}\"{1};\n",
|
|
||||||
get_const_name(&file_path),
|
|
||||||
fence,
|
|
||||||
buffer
|
|
||||||
)
|
|
||||||
.as_bytes(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if err.kind() != NotFound {
|
if err.kind() != NotFound {
|
||||||
panic!("Unexpected error: {}", err)
|
return Err(format!("Unexpected error: {}", err).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
pub fn main() -> Result<(), Box<dyn Error>> {
|
||||||
fn main() {
|
create_static_output_files(
|
||||||
create_static_output_files("static_files");
|
Path::new("static_files"),
|
||||||
|
&(Path::new(&env::var("OUT_DIR")?).join("static_files.rs")),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue