devbox_build/
lib.rs

1//! Small utility library for writing Rust build sripts (build.rs).
2//!
3//! It contains a collection of types that should make writing of build sripts a bit easier.
4//! It focuses on file manipulation and generation while checking file stamps to avoid unnecessary
5//! build steps. It can replace makefiles for simple things like copying some files and invoking
6//! some external compiler or tool like NPM when things have changes.
7//!
8//! Most methods do not return a Result but simply panic with consistent error messages stoping
9//! Cargo build which is desired behaviour for build sripts in most cases. For situations when
10//! you do want to recover from errors or implement a better error reporting most method have a twin
11//! method suffixed with '_result' that return `Result` values instead.
12//!
13//! # To install via umbrella devbox crate
14//!
15//! ```toml
16//! [dev-dependencies]
17//! devbox = { version = "0.1" }
18//! ```
19//!
20//! # Example
21//!
22//! Example build sripts builds a web application located in project `root/webapp` inside the target
23//! directory using NPM by installing all JS dependencies and running build script through NPM.
24//! Built web app is then embedded into Rust binary by packing it as a Rust source code. All
25//! the steps are done only on clean builds or when relevant resources change since last build.
26//!
27//! ```rust,no_run
28//! # use devbox_build::{Build, Cmd, Resource};
29//!
30//! pub fn main() {
31//!
32//!     let build = Build::new();
33//!
34//!     //-- Setup web app build dir inside of Rust target directory ----------------------
35//!
36//!     // Rust does not allow changes outside target directory, so setup a webapp build
37//!     // directory using links to source files where nodejs and company can do it's thing
38//!
39//!     let webrs = build.out_dir().file("webapp.rs");
40//!     let websrc = build.manifest_dir().dir("webapp");
41//!     let webwrk = build.out_dir().dir("webapp_build");
42//!     let webwrk_pkg = webwrk.file("package.json");
43//!     let webwrk_pkl = webwrk.file("package-lock.json");
44//!     let webwrk_ndm = webwrk.dir("node_modules");
45//!     let webwrk_dst = webwrk.dir("dist");
46//!
47//!     for unit in websrc.content("*") {
48//!         unit.link_from_inside(&webwrk);
49//!     }
50//!
51//!     //-- Build webapp using NPM -------------------------------------------------------
52//!
53//!     let npm = Cmd::new("npm").arg("--prefix").arg(webwrk.path());
54//!
55//!     webwrk_ndm.mk_from("Install WebApp node packages", &webwrk_pkg + &webwrk_pkl, ||{
56//!         npm.clone().arg("install").run();
57//!         webwrk_ndm.touch();
58//!     });
59//!
60//!     webwrk_dst.mk_from("Build WebApp using webpack", &webwrk.content("**"), || {
61//!         npm.clone().arg("run").arg("build").run();
62//!         webwrk_dst.touch();
63//!     });
64//!
65//!     //-- Package webapp into server binary as Rust source code ------------------------
66//!
67//!     webrs.mk_from("Embed WebApp build into binary", &webwrk_dst, || {
68//!         let mappings = webwrk_dst.files("**").into_iter().map(|file|
69//!             format!(r#""{}" => Some(include_bytes!("{}")),"#,
70//!                 file.path().strip_prefix(&webwrk_dst.path()).unwrap().to_str().unwrap(),
71//!                 file.path().to_str().unwrap())
72//!         ).fold("".to_owned(), |result, ref s| result + s + "\n" );
73//!
74//!         webrs.rewrite(format!(r"
75//!             pub fn file(path: &str) -> Option<&'static [u8]> {{
76//!                 match path {{
77//!                     {}
78//!                     &_ => None,
79//!                 }}
80//!             }}
81//!         ", mappings));
82//!     });
83//! }
84//! ```
85
86mod build;
87mod cmd;
88mod fs;
89mod res;
90
91pub use build::Build;
92pub use cmd::Cmd;
93pub use fs::{File, Dir, Unit};
94pub use res::{Resource, Set};