Skip to content

Commit

Permalink
refactor: making the entire BuildConfig possible from string inputs only
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 5, 2018
1 parent bcdc147 commit f5e637e
Show file tree
Hide file tree
Showing 10 changed files with 7,820 additions and 23 deletions.
3 changes: 2 additions & 1 deletion from-file/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;

Expand All @@ -18,7 +19,7 @@ pub enum FromFileError {
}

///
/// Implement this trait to enable your Struct's to deserialized
/// Implement this trait to enable your Struct's to be deserialized
/// from a file-path like
///
/// - conf/app.yaml
Expand Down
55 changes: 53 additions & 2 deletions rjs-parse/src/build_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use bundle_config::BundleConfig;
use modules;
use modules::BuildModuleId;
use parse::ConfigParseError;
use serde_json;
use std::collections::HashMap;
use BuildModule;
use RequireJsClientConfig;
use modules::ModuleData;

///
/// This struct is a combination of RequireJsClientConfig
Expand Down Expand Up @@ -115,6 +118,52 @@ impl RequireJsBuildConfig {
output.deps = client.deps;
Ok(output)
}
///
/// Add a bundle_config data structure - this will popuplate
/// the `modules` field on [RequireJsBuildConfig]
///
/// # Examples
///
/// ```
/// use rjs::*;
/// use rjs::bundle_config::*;
///
/// let input = include_str!("../test/fixtures/requirejs-config-generated.js");
/// let rjx = RequireJsBuildConfig::from_generated_string(input).expect("parsed");
/// let bundle_config_str = r#"
/// bundles:
/// - name: bundles/main
/// children: []
/// urls: ["/"]
/// "#;
/// let rjx2 = rjx.with_bundle_config(bundle_config_str.into(), &vec![]);
/// assert_eq!(rjx2.modules.expect("has modules"), vec![
/// BuildModule {
/// name: "requirejs/require".to_string(),
/// include: vec![],
/// exclude: vec![],
/// create: false
/// },
/// BuildModule {
/// name: "bundles/main".to_string(),
/// include: vec![],
/// exclude: vec![
/// "requirejs/require".to_string()
/// ],
/// create: true
/// }
/// ]);
/// ```
///
pub fn with_bundle_config(
mut self,
bundle_config: BundleConfig,
req_log: &Vec<ModuleData>
) -> RequireJsBuildConfig {
self.modules = Some(modules::generate_modules(req_log, bundle_config));
self
}

///
/// Just a passthrough for `from_generated_string` above
///
Expand Down Expand Up @@ -237,14 +286,16 @@ impl RequireJsBuildConfig {
true => format!(" // mixin trigger: \"{}\",", name),
false => format!(" \"{}\",", name),
}
}).collect();
})
.collect();

format!(
"require.config({{\n bundles: {{\n \"{}\": [\n{}\n ]\n }}\n}});",
module.name,
module_list.join("\n")
)
}).collect();
})
.collect();
items.join("\n")
}
///
Expand Down
69 changes: 67 additions & 2 deletions rjs-parse/src/bundle_config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,78 @@
extern crate serde_json;
extern crate serde_yaml;

use from_file::FromFile;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct BundleConfig {
pub bundles: Vec<ConfigItem>,
pub module_blacklist: Option<Vec<String>>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Debug)]
pub enum BundleConfigError {
SerdeJsonError(serde_json::Error),
SerdeYamlError(serde_yaml::Error),
}

impl BundleConfig {
///
/// # Examples
///
/// ```
/// use rjs::*;
/// use rjs::bundle_config::*;
/// let input = r#"
/// {"bundles": [{"name": "main", "children": [], "urls": []}]}
/// "#;
/// let actual = BundleConfig::from_json_string(input).expect("valid json parse");
/// assert_eq!(actual, BundleConfig{
/// bundles: vec![
/// ConfigItem {
/// name: "main".to_string(),
/// urls: vec![],
/// children: vec![],
/// }
/// ],
/// module_blacklist: None,
/// });
/// ```
///
pub fn from_json_string(input: impl Into<String>) -> Result<BundleConfig, BundleConfigError> {
serde_json::from_str(&input.into()).map_err(|e| BundleConfigError::SerdeJsonError(e))
}

///
/// # Examples
///
/// ```
/// use rjs::*;
/// use rjs::bundle_config::*;
/// let input = r#"
/// bundles:
/// - name: main
/// children: []
/// urls: []
/// "#;
/// let actual = BundleConfig::from_yaml_string(input).expect("valid yaml parse");
/// assert_eq!(actual, BundleConfig{
/// bundles: vec![
/// ConfigItem {
/// name: "main".to_string(),
/// urls: vec![],
/// children: vec![],
/// }
/// ],
/// module_blacklist: None,
/// })
/// ```
///
pub fn from_yaml_string(input: impl Into<String>) -> Result<BundleConfig, BundleConfigError> {
serde_yaml::from_str(&input.into()).map_err(|e| BundleConfigError::SerdeYamlError(e))
}
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct ConfigItem {
pub name: String,
pub urls: Vec<String>,
Expand Down
2 changes: 2 additions & 0 deletions rjs-parse/src/client_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bundle_config::BundleConfig;
use modules;
use modules::BuildModuleId;
use parse::ConfigParseError;
use parse::ParsedConfig;
Expand Down
18 changes: 9 additions & 9 deletions rjs-parse/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct ModuleData {
/// It requires just 2 things - the request log & the bundle_config provided by the user
///
pub fn generate_modules(
items: Vec<ModuleData>,
req_log: &Vec<ModuleData>,
config: impl Into<BundleConfig>,
) -> Vec<BuildModule> {
let mut modules: Vec<BuildModule> = vec![BuildModule {
Expand All @@ -51,7 +51,7 @@ pub fn generate_modules(
let conf = config.into();
collect(
&mut modules,
&items,
req_log,
&conf.bundles,
vec!["requirejs/require".into()],
);
Expand All @@ -63,13 +63,13 @@ pub fn generate_modules(
///
pub fn collect<'a>(
modules: &'a mut Vec<BuildModule>,
items: &Vec<ModuleData>,
req_log: &Vec<ModuleData>,
children: &Vec<ConfigItem>,
exclude: Vec<String>,
) {
children.iter().for_each(|conf_item| {
let mut include: Vec<String> = vec![];
items.iter().for_each(|item| {
req_log.iter().for_each(|item| {
if let Some(..) = conf_item.urls.iter().find(|x| **x == item.referrer) {
include.push(create_entry_point(&item));
}
Expand All @@ -85,7 +85,7 @@ pub fn collect<'a>(
modules.push(this_item);
let mut exclude = exclude.clone();
exclude.push(conf_item.name.to_string());
collect(modules, items, &conf_item.children, exclude);
collect(modules, req_log, &conf_item.children, exclude);
});
}

Expand Down Expand Up @@ -167,8 +167,8 @@ fn test_create_modules() {
}
"#.into();
let reqs: Vec<ModuleData> =
serde_json::from_str(include_str!("../../../../test/fixtures/example-reqs.json")).unwrap();
let out = generate_modules(reqs, c);
serde_json::from_str(include_str!("../test/fixtures/example-reqs.json")).unwrap();
let out = generate_modules(&reqs, c);

assert_eq!(
out[0],
Expand All @@ -193,8 +193,8 @@ fn test_create_modules() {
"bundles/checkout-success",
"bundles/basket-other",
].iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.map(|x| x.to_string())
.collect::<Vec<String>>()
);

assert_eq!(out[5].exclude, vec!["requirejs/require", "bundles/main"]);
Expand Down
6 changes: 4 additions & 2 deletions rjs-parse/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ fn process_shim(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
Some(strip_literal(s).to_string())
}
_ => None,
}).map(|s| serde_json::Value::String(s))
})
.map(|s| serde_json::Value::String(s))
.collect();

output
Expand Down Expand Up @@ -352,7 +353,8 @@ fn get_object_value(xs: &Vec<ObjectMember>, name: &str) -> Option<Expression> {
.and_then(|x| match x {
ObjectMember::Value { value, .. } => Some(value.clone()),
_ => None,
}).or(None)
})
.or(None)
}

fn filter_items(x: &ObjectMember, name: &str) -> bool {
Expand Down

0 comments on commit f5e637e

Please sign in to comment.