Adrian Heine
3 years ago
1 changed files with 36 additions and 39 deletions
-
75src/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 {
|
|
||||
let mut file_name_without_extension = p.clone().into();
|
|
||||
file_name_without_extension.set_extension("");
|
|
||||
String::from(
|
|
||||
file_name_without_extension
|
|
||||
.file_name()
|
|
||||
.unwrap()
|
|
||||
.to_string_lossy(),
|
|
||||
)
|
|
||||
.to_uppercase()
|
|
||||
|
pub fn create_static_output(
|
||||
|
source_path: &Path,
|
||||
|
mut target: impl Write,
|
||||
|
) -> Result<(), Box<dyn Error>> {
|
||||
|
let const_name = source_path
|
||||
|
.file_stem()
|
||||
|
.ok_or("Not a filename")?
|
||||
|
.to_str()
|
||||
|
.ok_or("Filename is not valid unicode")?
|
||||
|
.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) {
|
|
||||
let out_dir = env::var("OUT_DIR").unwrap();
|
|
||||
let dest_path = Path::new(&out_dir).join("static_files.rs");
|
|
||||
let mut f = File::create(&dest_path).unwrap();
|
|
||||
match read_dir(source_dir) {
|
|
||||
|
pub fn create_static_output_files(
|
||||
|
source_dir: &Path,
|
||||
|
dest_path: &Path,
|
||||
|
) -> Result<(), Box<dyn Error>> {
|
||||
|
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();
|
|
||||
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();
|
|
||||
|
create_static_output(&maybe_dir_entry?.path(), &mut f)?;
|
||||
}
|
}
|
||||
}
|
}
|
||||
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)]
|
|
||||
fn main() {
|
|
||||
create_static_output_files("static_files");
|
|
||||
|
pub fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
create_static_output_files(
|
||||
|
Path::new("static_files"),
|
||||
|
&(Path::new(&env::var("OUT_DIR")?).join("static_files.rs")),
|
||||
|
)
|
||||
}
|
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue