Permalink
Browse files

second attempt to support multiple application.json

  • Loading branch information...
1 parent e5a717d commit 2a191b3b5cee7e8380a22a9169cb61d4736a47e8 @caridy committed Jan 10, 2013
@@ -50,6 +50,7 @@ YUI.add('addon-rs-config', function(Y, NAME) {
this._jsonCache = {}; // fullPath: contents as JSON object
this._ycbCache = {}; // fullPath: context: YCB config object
this._ycbDims = this._readYcbDimensions();
+ this._ycbAppConfig = this._readYcbAppConfig();
},
@@ -64,6 +65,16 @@ YUI.add('addon-rs-config', function(Y, NAME) {
/**
+ * Returns the YCB library object for the application config.
+ * @method getAppConfigYCB
+ * @return {YCB} YCB library object for the application config
+ */
+ getAppConfigYCB: function() {
+ return this._ycbAppConfig;
+ },
+
+
+ /**
* Same as readConfigSync except the result is cached for future calls.
* @method readConfigSimple
* @param {string} fullPath path to JSON or YAML file
@@ -241,10 +252,6 @@ YUI.add('addon-rs-config', function(Y, NAME) {
if (mojitType && 'shared' !== mojitType) {
use = true;
}
- // use application.{json,yaml,yml} in bundles as well
- if ('bundle' === fs.rootType && 'application' === fs.basename) {
- use = true;
- }
if (!use) {
return;
}
@@ -283,7 +290,7 @@ YUI.add('addon-rs-config', function(Y, NAME) {
affinity: 'common',
selector: '*'
};
- if ('mojit' === source.fs.rootType) {
+ if ('app' !== source.fs.rootType) {
res.mojit = mojitType;
}
res.name = libpath.join(source.fs.subDir, baseParts.join('.'));
@@ -305,8 +312,46 @@ YUI.add('addon-rs-config', function(Y, NAME) {
path = libpath.join(this.mojitoRoot, 'dimensions.json');
}
return this.readConfigSimple(path);
- }
+ },
+
+ /**
+ * Initializes the special multi-file YCB library for all the application
+ * files. By default, we try to load `application.json`, then mix any other
+ * relative config file specified in the master section under the
+ * `applicationConfigFiles` array, which is optional.
+ * @private
+ * @method _readYcbAppConfig
+ * @return {object} libycb object
+ */
+ _readYcbAppConfig: function() {
+ var ycb,
+ i,
+ rootAppJSON = libpath.join(this.appRoot, 'application.json'),
+ paths = [],
+ relativePaths;
+
+ // trying to collect the app-level application.json which happens
+ // to be optional.
+ if (Y.Lang.isArray(this.readConfigSync(rootAppJSON))) {
+ ycb = this.readConfigYCB(rootAppJSON, {});
+ // adding the master application.json as the top level
+ paths = [rootAppJSON];
+ // optional applicationConfigFiles to mix in more configs
+ relativePaths = ycb.applicationConfigFiles || [];
+ for (i = 0; i < relativePaths.length; i += 1) {
+ paths.push(libpath.join(this.appRoot, relativePaths[i]));
+ }
+ }
+
+ // Note: it doesn't matter if we try to reach the same file multiple
+ // times (application.json) because readConfigSync will cache it anyway
+ ycb = this.createMultipartYCB(paths);
+ if (!ycb) {
+ throw new Error("failed to create a YCB config from the following files:\n " + paths.join("\n "));
+ }
+ return ycb;
+ }
});
Y.namespace('mojito.addons.rs');
@@ -4,7 +4,7 @@
* See the accompanying LICENSE file for terms.
*/
-/*jslint anon:true, sloppy:true, nomen:true, stupid:true*/
+/*jslint anon:true, sloppy:true, nomen:true*/
/*global YUI*/
@@ -18,9 +18,6 @@
*/
YUI.add('addon-rs-selector', function(Y, NAME) {
- var libpath = require('path'),
- libycb = require('ycb');
-
function RSAddonSelector() {
RSAddonSelector.superclass.constructor.apply(this, arguments);
}
@@ -39,7 +36,7 @@ YUI.add('addon-rs-selector', function(Y, NAME) {
this.appRoot = config.appRoot;
this.mojitoRoot = config.mojitoRoot;
- this._appConfigYCB = config.host.getAppConfigYCB();
+ this._appConfigYCB = config.host.config.getAppConfigYCB();
this._poslCache = {}; // context: POSL
},
@@ -224,14 +224,8 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
this.validateContext(this._config.context);
this._fwConfig = this.config.readConfigSimple(this._libs.path.join(this._config.mojitoRoot, 'config.json'));
- // We use this during preload() before we even know about all the
- // application.jsons. This might be OK, since we'll have the real
- // values during the second pass.
- // In practice, this only applies to "mojitDirs" and "mojitsDirs",
- // which can only be given in the app-level application.json.
+ this._appConfigYCB = this.config.getAppConfigYCB();
this._appConfigStatic = this.getAppConfig({});
-
- this._appConfigYCB = null; // libycb object
},
destructor: function() {},
@@ -364,13 +358,7 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
return JSON.parse(this._appConfigCache[key]);
}
- if (this._appConfigYCB) {
- ycb = this._appConfigYCB.read(ctx);
- } else {
- // This is generally only used to populate the initial this._appConfigStatic.
- // After preload() we'll have this._appConfigYCB.
- ycb = this.config.readConfigYCB(this._libs.path.join(this._config.root, 'application.json'), ctx);
- }
+ ycb = this._appConfigYCB.read(ctx);
appConfig = Y.mojito.util.blend(this._fwConfig.appConfigBase, this._config.appConfig);
appConfig = Y.mojito.util.blend(appConfig, ycb);
@@ -382,17 +370,6 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
/**
- * Returns the YCB library object for the application config.
- * This is rarely what you want to use. getAppConfig() is a better choice.
- * @method getAppConfigYCB
- * @return {YCB} YCB library object for the application config
- */
- getAppConfigYCB: function() {
- return this._appConfigYCB;
- },
-
-
- /**
* Preloads everything in the app, and as well pertinent parts of
* the framework.
*
@@ -403,9 +380,6 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
// We need to do an initial sweep to find the resource store addons.
this.preloadResourceVersions();
- // Now that we've found all the application.jsons, we can aggregate them.
- this._initAppConfig();
-
// We need to rebuild _appConfigStatic -before- we load the addons,
// since they might (they do) cache the results of getStaticAppConfig().
this._appConfigStatic = this.getAppConfig({});
@@ -2118,36 +2092,6 @@ YUI.add('mojito-resource-store', function(Y, NAME) {
});
Y.use.apply(Y, Object.keys(modules));
Y.applyConfig({ useSync: false });
- },
-
-
- /**
- * Initializes the special multi-file YCB library for all the application.jsons.
- * The object is used by getAppConfig().
- * @private
- * @method _initAppConfig
- * @return {nothing}
- */
- _initAppConfig: function() {
- var r,
- res,
- ress,
- paths = [];
-
- // TODO: detect conflicting peers (i.e. both application.json and application.yaml)
-
- ress = this.getResourceVersions({type: 'config', name: 'application'});
- for (r = 0; r < ress.length; r += 1) {
- res = ress[r];
- paths.push(res.source.fs.fullPath);
- }
- this._appConfigYCB = this.config.createMultipartYCB(paths);
- if (!this._appConfigYCB) {
- throw new Error("failed to create a YCB config from the following files:\n " + paths.join("\n "));
- }
- // New way to load means invalidating the whole cache.
- // In practice, the cache likely only has the static version.
- this._appConfigCache = {};
}
@@ -1,6 +1,10 @@
[
{
"settings": [ "master" ],
+ "applicationConfigFiles": [
+ "node_modules/devices/application.json",
+ "node_modules/runtimes/application.json"
+ ],
"testKey1": "testVal1",
"testKey2": "testVal2",
"testKey3": "testVal3"
@@ -1,18 +0,0 @@
-{
- "name": "devices",
- "version": "666.0.0",
- "description": "devices package",
- "author": "Team Mojito (http://developer.yahoo.com/cocktails/mojito)",
- "dependencies": {},
- "main": ".",
- "engines": {
- "node": ">= 0.4.0",
- "npm": ">= 1.0.0"
- },
- "devDependencies": {},
- "yahoo": {
- "mojito": {
- "type": "bundle"
- }
- }
-}
@@ -1,18 +0,0 @@
-{
- "name": "runtimes",
- "version": "666.0.0",
- "description": "runtimes package",
- "author": "Team Mojito (http://developer.yahoo.com/cocktails/mojito)",
- "dependencies": {},
- "main": ".",
- "engines": {
- "node": ">= 0.4.0",
- "npm": ">= 1.0.0"
- },
- "devDependencies": {},
- "yahoo": {
- "mojito": {
- "type": "bundle"
- }
- }
-}
@@ -81,9 +81,9 @@ YUI().use('addon-rs-config', 'mojito-util', 'mojito-test-extra', 'base', 'oop',
suite.add(new YUITest.TestCase({
-
+
name: 'config rs addon tests',
-
+
'read dimensions': function() {
// from mojito
var fixtures = libpath.join(__dirname, '../../../../../fixtures/store');
@@ -155,28 +155,6 @@ YUI().use('addon-rs-config', 'mojito-util', 'mojito-test-extra', 'base', 'oop',
have = store.findResourceVersionByConvention(source, 'shared');
want = { type: 'config' };
Y.TEST_CMP(have, want, 'include package.json in the "shared" mojit');
-
- // include application.json in packages
- source = {
- fs: {
- fullPath: libpath.join(fixtures, 'node_modules', 'foo', 'application.json'),
- rootDir: libpath.join(fixtures, 'node_modules', 'foo'),
- rootType: 'bundle',
- subDir: '.',
- subDirArray: ['.'],
- isFile: true,
- basename: 'application',
- ext: '.json'
- },
- pkg: {
- name: 'foo',
- version: '999.666.999',
- depth: 999
- }
- };
- have = store.findResourceVersionByConvention(source, 'shared');
- want = { type: 'config' };
- Y.TEST_CMP(have, want, 'include application.json in NPM modules');
},
@@ -303,7 +281,7 @@ YUI().use('addon-rs-config', 'mojito-util', 'mojito-test-extra', 'base', 'oop',
Y.TEST_CMP(have, want);
},
-
+
"readConfigSync JSON file": function () {
var fixtures = libpath.join(__dirname, '../../../../../fixtures'),
@@ -390,7 +368,7 @@ YUI().use('addon-rs-config', 'mojito-util', 'mojito-test-extra', 'base', 'oop',
paths = [
libpath.join(fixtures, 'application.json'),
libpath.join(fixtures, 'node_modules', 'devices', 'application.json'),
- libpath.join(fixtures, 'node_modules', 'runtimes', 'application.json'),
+ libpath.join(fixtures, 'node_modules', 'runtimes', 'application.json')
];
ycb = store.config.createMultipartYCB(paths);
A.isObject(ycb);
@@ -424,11 +402,27 @@ YUI().use('addon-rs-config', 'mojito-util', 'mojito-test-extra', 'base', 'oop',
];
ycb = store.config.createMultipartYCB(paths);
A.isUndefined(ycb);
- }
+ },
+ 'test applicationExtras in _readYcbAppConfig': function () {
+ var fixtures = libpath.join(__dirname, '../../../../../fixtures/app-jsons'),
+ store = new MockRS({ root: fixtures });
+ store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
+
+ var ycb, config;
+ ycb = store.config._readYcbAppConfig();
+ A.isObject(ycb);
+ config = ycb.read({runtime: 'client'});
+ A.isObject(config);
+ A.areSame('testVal2-client', config.testKey2);
+ config = ycb.read({device: 'android'});
+ A.isObject(config);
+ A.areSame('droid', config.selector);
+ }
+
}));
-
+
Y.Test.Runner.add(suite);
-
+
});
@@ -36,10 +36,8 @@ YUI().use(
'*': true
};
},
- getAppConfigYCB: function() {
- return this.config.createMultipartYCB([
- libpath.join(fixtures, 'application.json')
- ]);
+ blendStaticContext: function() {
+ return {};
},
validateContext: function() {},
cloneObj: function(o) {

0 comments on commit 2a191b3

Please sign in to comment.