Initial commit

This commit is contained in:
Adrian Malacoda 2017-10-25 20:11:32 -05:00
commit 74027d4a0d
17 changed files with 158 additions and 0 deletions

4
Cargo.lock generated Normal file
View File

@ -0,0 +1,4 @@
[root]
name = "plushie-narwhal"
version = "0.0.1"

4
Cargo.toml Normal file
View File

@ -0,0 +1,4 @@
[package]
name = "plushie-narwhal"
version = "0.0.1"
authors = ["A. Malacoda <malacoda@monarch-pass.net>"]

139
src/lib.rs Normal file
View File

@ -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<Resource>
}
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<Route> {\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<ResourceMount>,
constant: Option<&'static str>,
template_name: Option<&'static str>,
raw: bool
}
pub struct ResourceMount {
endpoint: &'static str,
content_type: &'static str
}

0
target/debug/.cargo-lock Normal file
View File

View File

@ -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":[]}

View File

@ -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

Binary file not shown.

View File

View File

@ -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"]}

View File

@ -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:

View File

@ -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