refactor into multiple functions
This commit is contained in:
parent
11bdb6c878
commit
7c0aaf7efc
73
src/lib.rs
73
src/lib.rs
@ -9,7 +9,9 @@ use std::fs::{File, create_dir_all};
|
|||||||
use std::io::{Write, copy};
|
use std::io::{Write, copy};
|
||||||
|
|
||||||
pub fn yarn_install () {
|
pub fn yarn_install () {
|
||||||
Command::new("yarn").output().expect("failed to execute yarn process");
|
if !Command::new("yarn").status().expect("failed to execute yarn process").success() {
|
||||||
|
panic!("Failed to execute yarn process");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn browserify (infile: &str, outfile: &str) -> PathBuf {
|
pub fn browserify (infile: &str, outfile: &str) -> PathBuf {
|
||||||
@ -86,7 +88,32 @@ impl ResourcesGenerator {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref mount_at) = resource.mount_at {
|
if resource.mount_at.is_some() {
|
||||||
|
let (function_name, route_function) = self.create_rocket_route(resource, &include);
|
||||||
|
routes.push_str(&route_function);
|
||||||
|
routes_fn.push_str(&function_name);
|
||||||
|
routes_fn.push(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
if resource.template_name.is_some() {
|
||||||
|
handlebars.push_str(&self.create_handlebars_template(resource, &include));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !routes.is_empty() {
|
||||||
|
self.write_rocket_routes(&mut outfile, &routes, &routes_fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !handlebars.is_empty () {
|
||||||
|
self.write_handlebars_templates(&mut outfile, &handlebars);
|
||||||
|
}
|
||||||
|
|
||||||
|
outfile.write(consts.as_bytes());
|
||||||
|
outfile.write(b"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_rocket_route(&self, resource: &Resource, include: &str) -> (String, String) {
|
||||||
|
let mount_at = resource.mount_at.as_ref().unwrap();
|
||||||
let mut function_name = String::from("route");
|
let mut function_name = String::from("route");
|
||||||
let mut function_return_type = "&'static str";
|
let mut function_return_type = "&'static str";
|
||||||
let mut function_return_value = String::from(resource.constant.unwrap_or(&include));
|
let mut function_return_value = String::from(resource.constant.unwrap_or(&include));
|
||||||
@ -97,29 +124,17 @@ impl ResourcesGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_").replace("-", "_"));
|
function_name.push_str(&mount_at.endpoint.replace("/", "_").replace(".", "_").replace("-", "_"));
|
||||||
routes.push_str(&format!(
|
(function_name.clone(), format!(
|
||||||
"#[get(\"{}\")]\nfn {}() -> Content<{}> {{\n\tContent(ContentType::{}, {})\n}}\n\n",
|
"#[get(\"{}\")]\nfn {}() -> Content<{}> {{\n\tContent(ContentType::{}, {})\n}}\n\n",
|
||||||
mount_at.endpoint,
|
mount_at.endpoint,
|
||||||
function_name,
|
function_name,
|
||||||
function_return_type,
|
function_return_type,
|
||||||
mount_at.content_type,
|
mount_at.content_type,
|
||||||
function_return_value
|
function_return_value
|
||||||
));
|
))
|
||||||
routes_fn.push_str(&function_name);
|
|
||||||
routes_fn.push(',');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref handlebars_name) = resource.template_name {
|
fn write_rocket_routes(&self, outfile: &mut File, routes: &str, routes_fn: &str) {
|
||||||
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, Response, Request};\nuse rocket::response::{Content, Responder};\nuse rocket::http::{ContentType, Status};\n\n");
|
outfile.write(b"use rocket::{Route, Response, Request};\nuse rocket::response::{Content, Responder};\nuse rocket::http::{ContentType, Status};\n\n");
|
||||||
outfile.write(b"use std::io::Cursor;\n\n");
|
outfile.write(b"use std::io::Cursor;\n\n");
|
||||||
outfile.write(b"struct RawByteResource(&'static [u8]);\n");
|
outfile.write(b"struct RawByteResource(&'static [u8]);\n");
|
||||||
@ -131,29 +146,29 @@ impl ResourcesGenerator {
|
|||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
}");
|
}");
|
||||||
}
|
|
||||||
|
|
||||||
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.as_bytes());
|
||||||
outfile.write(routes_fn.as_bytes());
|
outfile.write(routes_fn.as_bytes());
|
||||||
outfile.write(b"]\n}");
|
outfile.write(b"]\n}");
|
||||||
}
|
}
|
||||||
|
|
||||||
if !handlebars.is_empty() {
|
fn create_handlebars_template(&self, resource: &Resource, include: &str) -> String {
|
||||||
|
let template_name = resource.template_name.unwrap();
|
||||||
|
format!(
|
||||||
|
"\n\thbs.register_template_string(\"{}\", {}).expect(\"Failed to register template: {}\");",
|
||||||
|
template_name,
|
||||||
|
resource.constant.unwrap_or(&include),
|
||||||
|
template_name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_handlebars_templates(&self, outfile: &mut File, handlebars: &str) {
|
||||||
|
outfile.write(b"use handlebars::Handlebars;\n\n");
|
||||||
outfile.write(b"pub fn handlebars() -> Handlebars {\n");
|
outfile.write(b"pub fn handlebars() -> Handlebars {\n");
|
||||||
outfile.write(b"\tlet mut hbs = Handlebars::new();");
|
outfile.write(b"\tlet mut hbs = Handlebars::new();");
|
||||||
outfile.write(handlebars.as_bytes());
|
outfile.write(handlebars.as_bytes());
|
||||||
outfile.write(b"hbs\n}");
|
outfile.write(b"hbs\n}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Resource {
|
pub struct Resource {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user