commit 74027d4a0df3cfa5b3fcd4feabec9acc3c41c4aa Author: Adrian Malacoda Date: Wed Oct 25 20:11:32 2017 -0500 Initial commit diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c28b8b3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "plushie-narwhal" +version = "0.0.1" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f45d8c1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "plushie-narwhal" +version = "0.0.1" +authors = ["A. Malacoda "] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..652d039 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,139 @@ +use std::process::Command; +use std::env; + +use std::path::{Path, PathBuf}; +use std::fs::canonicalize; +use std::fs::File; +use std::io::Write; + +pub fn yarn_install () { + Command::new("yarn").output().expect("failed to execute yarn process"); +} + +pub fn browserify (infile: &str, outfile: &str) -> PathBuf { + let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(&out_dir); + let full_outfile = out_dir.join(outfile); + Command::new("node_modules/.bin/browserify") + .args(&[infile, "-o", full_outfile.to_str().expect("failed to build output path")]) + .output() + .expect("failed to build js bundle"); + full_outfile +} + +pub fn lessc (infile: &str, outfile: &str) -> PathBuf { + let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(&out_dir); + let full_outfile = out_dir.join(outfile); + Command::new("node_modules/.bin/lessc") + .args(&[infile, full_outfile.to_str().expect("failed to build output path")]) + .output() + .expect("failed to build css bundle"); + full_outfile +} + +pub struct ResourcesGenerator { + resources: Vec +} + +impl ResourcesGenerator { + pub fn new () -> ResourcesGenerator { + ResourcesGenerator { + resources: vec![] + } + } + + pub fn add (&mut self, resource: Resource) -> &mut ResourcesGenerator { + self.resources.push(resource); + self + } + + pub fn write (&self) { + let out_dir = env::var("OUT_DIR").unwrap(); + let out_dir = Path::new(&out_dir); + let mut outfile = File::create(out_dir.join("resources.rs").to_str().expect("failed to build output path")).expect("failed to create outfile"); + + let mut consts = String::new(); + + let mut routes = String::new(); + let mut routes_fn = String::from("pub fn routes() -> Vec {\n\troutes!["); + + let mut handlebars = String::new(); + for resource in &self.resources { + let canonical_source = canonicalize(&resource.source).expect("failed to get canonical path"); + let data_type = if resource.raw { + "[u8]" + } else { + "str" + }; + + let include = format!("include_{}!(\"{}\")", if resource.raw { "bytes" } else { "str" }, canonical_source.display()); + if let Some(constant) = resource.constant { + consts.push_str(&format!( + "const {}: &'static {} = {};\n", constant, data_type, include + )); + } + + if let Some(ref mount_at) = resource.mount_at { + let mut function_name = String::from("route"); + function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_")); + routes.push_str(&format!( + "#[get(\"{}\")]\nfn {}() -> Content<&'static {}> {{\n\tContent(ContentType::{}, {})\n}}\n\n", + mount_at.endpoint, + function_name, + data_type, + mount_at.content_type, + resource.constant.unwrap_or(&include) + )); + routes_fn.push_str(&function_name); + routes_fn.push(','); + } + + if let Some(ref handlebars_name) = resource.template_name { + handlebars.push_str(&format!( + "\n\thbs.register_template_string(\"{}\", {}).expect(\"Failed to register template: {}\");", + handlebars_name, + resource.constant.unwrap_or(&include), + handlebars_name + )); + } + } + + if !routes.is_empty() { + outfile.write(b"use rocket::Route;\nuse rocket::response::Content;\nuse rocket::http::ContentType;\n\n"); + } + + if !handlebars.is_empty () { + outfile.write(b"use handlebars::Handlebars;\n\n"); + } + + outfile.write(consts.as_bytes()); + outfile.write(b"\n"); + + if !routes.is_empty() { + outfile.write(routes.as_bytes()); + outfile.write(routes_fn.as_bytes()); + outfile.write(b"]\n}"); + } + + if !handlebars.is_empty() { + outfile.write(b"pub fn handlebars() -> Handlebars {\n"); + outfile.write(b"\tlet mut hbs = Handlebars::new();"); + outfile.write(handlebars.as_bytes()); + outfile.write(b"hbs\n}"); + } + } +} + +pub struct Resource { + source: PathBuf, + mount_at: Option, + constant: Option<&'static str>, + template_name: Option<&'static str>, + raw: bool +} + +pub struct ResourceMount { + endpoint: &'static str, + content_type: &'static str +} diff --git a/target/debug/.cargo-lock b/target/debug/.cargo-lock new file mode 100644 index 0000000..e69de29 diff --git a/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/dep-lib-plushie_narwhal-7829241311c7df40 b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/dep-lib-plushie_narwhal-7829241311c7df40 new file mode 100644 index 0000000..3a7b66a Binary files /dev/null and b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/dep-lib-plushie_narwhal-7829241311c7df40 differ diff --git a/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40 b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40 new file mode 100644 index 0000000..7fbff92 --- /dev/null +++ b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40 @@ -0,0 +1 @@ +bebe28a6b3b9f807 \ No newline at end of file diff --git a/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40.json b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40.json new file mode 100644 index 0000000..be4c0a1 --- /dev/null +++ b/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/lib-plushie_narwhal-7829241311c7df40.json @@ -0,0 +1 @@ +{"rustc":11807999472408288889,"features":"[]","target":7989262708905000541,"profile":731176819336294830,"deps":[],"local":[{"MtimeBased":[[1508980177,567194711],"/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/debug/.fingerprint/plushie-narwhal-7829241311c7df40/dep-lib-plushie_narwhal-7829241311c7df40"]}],"rustflags":[]} \ No newline at end of file diff --git a/target/debug/deps/libplushie_narwhal-7829241311c7df40.rlib b/target/debug/deps/libplushie_narwhal-7829241311c7df40.rlib new file mode 100644 index 0000000..3d3efc3 Binary files /dev/null and b/target/debug/deps/libplushie_narwhal-7829241311c7df40.rlib differ diff --git a/target/debug/libplushie_narwhal.d b/target/debug/libplushie_narwhal.d new file mode 100644 index 0000000..e60d54c --- /dev/null +++ b/target/debug/libplushie_narwhal.d @@ -0,0 +1 @@ +/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/debug/libplushie_narwhal.rlib: /home/malacoda/Projects/monarch-pass/plushie-narwhal/src/lib.rs diff --git a/target/debug/libplushie_narwhal.rlib b/target/debug/libplushie_narwhal.rlib new file mode 100644 index 0000000..3d3efc3 Binary files /dev/null and b/target/debug/libplushie_narwhal.rlib differ diff --git a/target/rls/debug/.cargo-lock b/target/rls/debug/.cargo-lock new file mode 100644 index 0000000..e69de29 diff --git a/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/dep-lib-plushie_narwhal-a4300300f3236c85 b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/dep-lib-plushie_narwhal-a4300300f3236c85 new file mode 100644 index 0000000..8491fe0 Binary files /dev/null and b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/dep-lib-plushie_narwhal-a4300300f3236c85 differ diff --git a/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85 b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85 new file mode 100644 index 0000000..3035cf3 --- /dev/null +++ b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85 @@ -0,0 +1 @@ +adce138a5edbbf39 \ No newline at end of file diff --git a/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85.json b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85.json new file mode 100644 index 0000000..bffaf98 --- /dev/null +++ b/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/lib-plushie_narwhal-a4300300f3236c85.json @@ -0,0 +1 @@ +{"rustc":16990310272001297305,"features":"[]","target":7989262708905000541,"profile":14119667493710126711,"deps":[],"local":[{"MtimeBased":[[1508980108,617370357],"/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/rls/debug/.fingerprint/plushie-narwhal-a4300300f3236c85/dep-lib-plushie_narwhal-a4300300f3236c85"]}],"rustflags":["--error-format=json"]} \ No newline at end of file diff --git a/target/rls/debug/deps/libplushie_narwhal-a4300300f3236c85.rmeta b/target/rls/debug/deps/libplushie_narwhal-a4300300f3236c85.rmeta new file mode 100644 index 0000000..dad0fda Binary files /dev/null and b/target/rls/debug/deps/libplushie_narwhal-a4300300f3236c85.rmeta differ diff --git a/target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d b/target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d new file mode 100644 index 0000000..4186eed --- /dev/null +++ b/target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d @@ -0,0 +1,5 @@ +/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.rmeta: Projects/monarch-pass/plushie-narwhal/src/lib.rs + +/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d: Projects/monarch-pass/plushie-narwhal/src/lib.rs + +Projects/monarch-pass/plushie-narwhal/src/lib.rs: diff --git a/target/rls/debug/libplushie_narwhal.d b/target/rls/debug/libplushie_narwhal.d new file mode 100644 index 0000000..e31cdac --- /dev/null +++ b/target/rls/debug/libplushie_narwhal.d @@ -0,0 +1 @@ +/home/malacoda/Projects/monarch-pass/plushie-narwhal/target/rls/debug/libplushie_narwhal.rmeta: /home/malacoda/Projects/monarch-pass/plushie-narwhal/src/lib.rs