Use absolute paths when including resources. (Fixes #53)

This commit is contained in:
Leo Schwarz 2016-07-02 15:15:10 +02:00 committed by Matthew Collins
parent 8d95965d40
commit e96ec620fb
1 changed files with 16 additions and 12 deletions

View File

@ -1,6 +1,6 @@
use std::env;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::io::BufWriter;
use std::io::Write;
@ -15,23 +15,27 @@ fn main() {
let mut file = BufWriter::new(fs::File::create(&dest.join("resources.rs")).unwrap());
write!(file, "pub fn get_file(name: &str) -> Option<&'static [u8]> {{\n").unwrap();
write!(file, " match name {{\n").unwrap();
for entry in &out {
let entry = entry.replace("\\", "/");
let short = &entry;
write!(file, " {:?} => Some(include_bytes!(\"../{}\")),\n", short, entry).unwrap();
for path in &out {
let mut absolute_path = std::env::current_dir().unwrap();
absolute_path.push(path);
let absolute = absolute_path.to_str().unwrap().replace("\\", "/");
let relative = path.to_str().unwrap().replace("\\", "/");
write!(file, " {:?} => Some(include_bytes!(\"{}\")),\n", relative, absolute).unwrap();
}
write!(file, " _ => None\n }}\n}}\n").unwrap();
}
fn build_map(out: &mut Vec<String>, path: &Path) {
fn build_map(out: &mut Vec<PathBuf>, path: &Path) {
let files = fs::read_dir(path).unwrap();
for entry in files {
let entry = entry.unwrap();
if fs::metadata(entry.path()).unwrap().is_dir() {
build_map(out, &entry.path());
} else {
out.push(entry.path().to_str().unwrap().to_owned());
}
let entry = entry.unwrap();
if fs::metadata(entry.path()).unwrap().is_dir() {
build_map(out, &entry.path());
} else {
out.push(entry.path());
}
}
}