From e96ec620fb928ed122f1965e606ad9800d5b85b8 Mon Sep 17 00:00:00 2001 From: Leo Schwarz Date: Sat, 2 Jul 2016 15:15:10 +0200 Subject: [PATCH] Use absolute paths when including resources. (Fixes #53) --- resources/build.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/resources/build.rs b/resources/build.rs index a36cb63..600741d 100644 --- a/resources/build.rs +++ b/resources/build.rs @@ -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, path: &Path) { +fn build_map(out: &mut Vec, 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()); + } } }