I was looking for a crate for embedding an entire directory tree into an executable to make deployment easier, but couldn’t find anything which worked the way I wanted it to… So I made my own.
At the moment you need to use a build script, but when proc_macros
become stable I’ll be creating an include_dir!()
macro to go alongside include_str!()
and include_bytes!()
.
To embed a directory and its contents into your binary, you’ll need to add the
following to your build.rs
script.
extern crate include_dir;
use std::env;
use std::path::Path;
use include_dir::include_dir;
fn main() {
let outdir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&outdir).join("assets.rs");
include_dir("assets")
.as_variable("SRC")
.to_file(dest_path)
.unwrap();
}
The generated code comes with doc-comments to make discovering how to use it easier, otherwise check out the integration tests directory.
Let me know what you think