aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Cargo.toml8
-rw-r--r--examples/foo/Cargo.toml11
-rw-r--r--examples/foo/build.rs5
-rw-r--r--examples/foo/src/data/empty0
-rw-r--r--examples/foo/src/data/foo1
-rw-r--r--examples/foo/src/data/inner/boom1
-rw-r--r--examples/foo/src/main.rs9
-rw-r--r--src/lib.rs52
9 files changed, 89 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a9d37c5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+target
+Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..b853292
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "includedir"
+version = "0.1.0"
+authors = ["Till Höppner <till@hoeppner.ws>"]
+
+[dependencies]
+walkdir = "0.1.5"
+phf_codegen = "0.7.12"
diff --git a/examples/foo/Cargo.toml b/examples/foo/Cargo.toml
new file mode 100644
index 0000000..8a30ca2
--- /dev/null
+++ b/examples/foo/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "foo"
+version = "0.1.0"
+authors = ["Till Höppner <till@hoeppner.ws>"]
+build = "build.rs"
+
+[dependencies]
+phf = "*"
+
+[build-dependencies.includedir]
+path = "../.."
diff --git a/examples/foo/build.rs b/examples/foo/build.rs
new file mode 100644
index 0000000..1196b6a
--- /dev/null
+++ b/examples/foo/build.rs
@@ -0,0 +1,5 @@
+extern crate includedir;
+
+fn main() {
+ includedir::build("src/data").unwrap();
+}
diff --git a/examples/foo/src/data/empty b/examples/foo/src/data/empty
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/foo/src/data/empty
diff --git a/examples/foo/src/data/foo b/examples/foo/src/data/foo
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/examples/foo/src/data/foo
@@ -0,0 +1 @@
+foo
diff --git a/examples/foo/src/data/inner/boom b/examples/foo/src/data/inner/boom
new file mode 100644
index 0000000..9e2ba7e
--- /dev/null
+++ b/examples/foo/src/data/inner/boom
@@ -0,0 +1 @@
+boom
diff --git a/examples/foo/src/main.rs b/examples/foo/src/main.rs
new file mode 100644
index 0000000..8270aec
--- /dev/null
+++ b/examples/foo/src/main.rs
@@ -0,0 +1,9 @@
+extern crate phf;
+
+include!(concat!(env!("OUT_DIR"), "/dir_data.rs"));
+
+fn main() {
+ for (k, v) in FILES.entries() {
+ println!("{}: {} bytes", k, v.len());
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..939b958
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,52 @@
+extern crate walkdir;
+extern crate phf_codegen;
+
+use std::{env, io};
+use std::fs::File;
+use std::io::{BufWriter, Write};
+use std::path::Path;
+
+use walkdir::WalkDir;
+
+pub fn build<P: AsRef<Path>>(dir: P) -> io::Result<()> {
+ let dir = dir.as_ref();
+
+ let base_path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).to_owned();
+
+ let dir_name = dir.file_name().expect("Invalid directory name").to_string_lossy();
+ let out_name = format!("dir_{}.rs", dir_name);
+ let out_path = Path::new(&env::var("OUT_DIR").unwrap()).join(out_name);
+ let mut out_file = BufWriter::new(File::create(&out_path).unwrap());
+
+ write!(&mut out_file,
+ "static FILES: phf::Map<&'static str, &'static [u8]> = ")
+ .unwrap();
+
+ // FIXME: rustfmt mangles this
+ let entries: Vec<(String, String)> = WalkDir::new(dir)
+ .into_iter()
+ .filter(|e| {
+ !e.as_ref()
+ .ok()
+ .map_or(true, |e| e.file_type().is_dir())
+ })
+ .map(|e| {
+ let entry = e.expect("Invalid dir entry.");
+ let name = format!("{}", entry.path().display());
+ let code = format!("include_bytes!(\"{}\") \
+ as &'static [u8]",
+ base_path.join(entry.path())
+ .display());
+ (name, code)
+ })
+ .collect();
+
+ let mut map: phf_codegen::Map<&str> = phf_codegen::Map::new();
+ for &(ref name, ref code) in &entries {
+ map.entry(name, code);
+ }
+ map.build(&mut out_file).unwrap();
+
+ write!(&mut out_file, ";\n").unwrap();
+ Ok(())
+}