Initial commit
This commit is contained in:
commit
74027d4a0d
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[root]
|
||||
name = "plushie-narwhal"
|
||||
version = "0.0.1"
|
||||
|
4
Cargo.toml
Normal file
4
Cargo.toml
Normal 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
139
src/lib.rs
Normal 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
0
target/debug/.cargo-lock
Normal file
Binary file not shown.
@ -0,0 +1 @@
|
||||
bebe28a6b3b9f807
|
@ -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":[]}
|
BIN
target/debug/deps/libplushie_narwhal-7829241311c7df40.rlib
Normal file
BIN
target/debug/deps/libplushie_narwhal-7829241311c7df40.rlib
Normal file
Binary file not shown.
1
target/debug/libplushie_narwhal.d
Normal file
1
target/debug/libplushie_narwhal.d
Normal 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
|
BIN
target/debug/libplushie_narwhal.rlib
Normal file
BIN
target/debug/libplushie_narwhal.rlib
Normal file
Binary file not shown.
0
target/rls/debug/.cargo-lock
Normal file
0
target/rls/debug/.cargo-lock
Normal file
Binary file not shown.
@ -0,0 +1 @@
|
||||
adce138a5edbbf39
|
@ -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"]}
|
BIN
target/rls/debug/deps/libplushie_narwhal-a4300300f3236c85.rmeta
Normal file
BIN
target/rls/debug/deps/libplushie_narwhal-a4300300f3236c85.rmeta
Normal file
Binary file not shown.
5
target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d
Normal file
5
target/rls/debug/deps/plushie_narwhal-a4300300f3236c85.d
Normal 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:
|
1
target/rls/debug/libplushie_narwhal.d
Normal file
1
target/rls/debug/libplushie_narwhal.d
Normal 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
|
Loading…
x
Reference in New Issue
Block a user