Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Shaker V4 initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljimenez committed Mar 13, 2013
1 parent a60b1c1 commit a37452b
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 496 deletions.
364 changes: 194 additions & 170 deletions addons/ac/shaker.server.js

Large diffs are not rendered by default.

405 changes: 192 additions & 213 deletions addons/rs/shaker.server.js

Large diffs are not rendered by default.

39 changes: 8 additions & 31 deletions bin/mojito-shake
@@ -1,15 +1,4 @@
#!/usr/bin/env node
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

/*jslint anon:true, sloppy:true*/

var libmojito = require('mojito'),
utils = require('mojito/lib/management/utils'),
libpath = require('path');
/*
* A command is expected to export the following:
* run - The function that executes the command. The signature is:
Expand All @@ -24,7 +13,6 @@ var libmojito = require('mojito'),
* hasValue - True is this option requires a value. Optional; default false.
*/


/*
* Creates a map keyed by both short and long option names, to simplify lookup
* of option info from command line args.
Expand Down Expand Up @@ -88,44 +76,33 @@ function parseArgs(args, optionInfo) {
return { params: params, options: options, errors: errors };
}

// ---------- Start of mainline code ----------


function main() {
var args = process.argv.slice(2),
command,
var utils,
command = require('../commands/shake'),
args = process.argv.slice(2),
argInfo;

try {
command = require('../commands/shake');
} catch (e) {
utils.error('Invalid command!');
return;
}

if (args.length === 0) {
argInfo = { command: 'help', params: [] };
} else {
argInfo = parseArgs(args, command.options);
}

if (argInfo.errors && argInfo.errors.length > 0) {
utils = require('mojito/lib/management/utils')
argInfo.errors.forEach(function(e) {
utils.log(e);
});
utils.error('Invalid command line.', "Try 'mojito help <command>'.");
//utils.error('Invalid command line.', "Try 'mojito help <command>'.");
return;
}

command.run(argInfo.params, argInfo.options, function(err) {
if (err) {
utils.error(err);
} else {
utils.success('mojito done.');
}
});
command.run(argInfo.params, argInfo.options);
}

// Execute the main() function. Note that this occurs as part of the
// require/import process so simply importing/require()ing the cli.js will cause
// require/import process so simply importing/requiring the cli.js will cause
// the main() operation to be invoked.
main();
42 changes: 27 additions & 15 deletions commands/shake.js
Expand Up @@ -4,8 +4,10 @@
* See the accompanying LICENSE file for terms.
*/

start = require('mojito/lib/app/commands/start');
Shaker = require('mojito-shaker/lib/shaker').Shaker;
var utils = require('mojito/lib/management/utils'),
mojitoStart = require('mojito/lib/app/commands/start'),
ShakerCompiler = require('../lib/compiler').ShakerCompiler;

/**
* Convert a CSV string into a context object.
* @param {string} s A string of the form: 'key1:value1,key2:value2'.
Expand Down Expand Up @@ -35,10 +37,9 @@ function contextCsvToObject(s) {
* Standard usage string export.
*/
exports.usage = '\nShaker Options:\n' +
'\t--context A comma-separated list of key:value pairs that define the' +
' base\n' +
'\t context used to read configuration files\n' +
'\t--run Run Mojito Server after running Shaker\n';
' --context A comma-separated list of key:value pairs that define the base\n' +
' context used to read configuration files (e.g. "environment:dev")\n' +
' --run Run the Mojito server after running the Shaker compiler\n';

/**
* Standard options list export.
Expand Down Expand Up @@ -73,7 +74,7 @@ exports.options = [
* @param {object} opts Options/flags for the command.
* @param {function} callback An optional callback to invoke on completion.
*/
exports.run = function(params, options, callback) {
exports.run = function(params, options) {
options = options || {};
var context = {},
debug = options.debug || 0;
Expand All @@ -86,15 +87,26 @@ exports.run = function(params, options, callback) {
console.log(this.usage);
return;
}

var shaker = new Shaker({context: context, debugLevel: debug});
shaker.run(function (err, data) {
if (options.run) {
delete options.run;
start.run(params, options, callback);

// shaker compiler
process.shakerCompiler = true;

var compiler = new ShakerCompiler(context);
compiler.compile(function (err) {
if (err) {
utils.error(err);
} else {
callback(err, data);
utils.success('Shaker done.');
if (options.run) {
delete options.run;
mojitoStart.run(params, options, function (err) {
if (err) {
utils.error(err);
} else {
utils.success('Mojito done.');
}
});
}
}
});
//if we do async stuff move callback
};
2 changes: 1 addition & 1 deletion index.js
@@ -1 +1 @@
module.exports = require('./lib/shaker');
module.exports = require('./lib/compiler');
19 changes: 14 additions & 5 deletions lib/compiler.js
Expand Up @@ -9,8 +9,7 @@ var validateConfig = require('./config.js').validateConfig,
mojitrollup: require('./rollups/mojitrollup.js').mojitrollup
},
locations: {
local: require('./locations/local.js').local,
mobstor: require('shaker-mobstor').mobstor
local: require('./locations/local.js').location
}
},
typeHierarchy = {
Expand All @@ -23,13 +22,23 @@ var validateConfig = require('./config.js').validateConfig,
};

function ShakerCompiler(context) {
debugger;
this.compile = function (done) {
var resources = new ShakerResources(context),
config = resources.shakerConfig;

config.appConfig = resources.appConfig;

// initialize locations
Y.Object.each(config.locations, function (locationConfig, location) {
if (!modules.locations[location]) {
try {
modules.locations[location] = require('mojito-shaker-' + location).location;
} catch (e) {
console.log(e);
}
}
});

applyTasks(config, resources.appResources, function (err) {
applyRollups(config, resources.appResources, resources.organizedResources, function (err) {
applyLocations(config.locations, config.appConfig, resources.appResources, function (err) {
Expand Down Expand Up @@ -480,7 +489,7 @@ debugger;

// TODO check if write files, sometimes no permission
// TODO check it can write file first before compiling
require('fs').writeFile("shakerResources.json", JSON.stringify(organizedResources));
require('fs').writeFile("shaker-meta.json", JSON.stringify(organizedResources));

}

Expand All @@ -507,7 +516,7 @@ require('fs').writeFile("shakerResources.json", JSON.stringify(organizedResource
locations[locationName].resources[resource.url] = resource.locations[locationName];
//}
});

debugger;
resources.resolveResourceVersions(locations[locationName].resources);
loaderResources = resources.getLoaderResources();

Expand Down
2 changes: 1 addition & 1 deletion lib/locations/local.js
Expand Up @@ -3,7 +3,7 @@ var fs = require('fs');
var basedir = 'assets/compiled';


exports.local = function (config) {
exports.location = function (config) {
var root = "/" + config.appConfig.prefix + "/" + config.appConfig.appName + "/" + basedir + "/";
this.yuiConfig = {
"groups": {
Expand Down
8 changes: 3 additions & 5 deletions lib/resources.js
Expand Up @@ -40,8 +40,8 @@ function ShakerResources(context) {


// TODO remove posl if no resoruces
require('fs').writeFile("appResources.json", JSON.stringify(self.appResources));
require('fs').writeFile("organizedResources.json", JSON.stringify(self.organizedResources));
//require('fs').writeFile("appResources.json", JSON.stringify(self.appResources));
//require('fs').writeFile("organizedResources.json", JSON.stringify(self.organizedResources));



Expand Down Expand Up @@ -387,8 +387,7 @@ function ShakerResources(context) {

function getShakerConfig(context) {
var config = store.getAppConfig(context || {});
// TODO: change to shaker
return config.shaker2 || {};
return config.shaker || {};
};

function getShakerConfigByContext(mojitName, context) {
Expand Down Expand Up @@ -416,7 +415,6 @@ function ShakerResources(context) {
}

function getStoreConfigs() {
debugger;
var appConfig = store.getAppConfig(context);

return {
Expand Down
1 change: 0 additions & 1 deletion lib/rollups/mojitrollup.js
Expand Up @@ -95,6 +95,5 @@ exports.mojitrollup = function (config, resources) {
});
});
});
debugger;
return rollups;
}
113 changes: 67 additions & 46 deletions mojits/ShakerHTMLFrameMojit/controller.server.js
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
Expand All @@ -9,17 +9,74 @@
/*global YUI*/


YUI.add('ShakerHTMLFrameMojit', function(Y, NAME) {
YUI.add('HTMLFrameMojit', function (Y, NAME) {

'use strict';

Y.namespace('mojito.controllers')[NAME] = {

index: function(ac) {
index: function (ac) {
this.__call(ac);
},

__call: function(ac) {
__call: function (ac) {

this._renderChild(ac, function (data, meta) {

ac.shaker.run(meta.assets);

// meta.assets from child should be piped into
// the frame's assets before doing anything else.
ac.assets.addAssets(meta.assets);

if (ac.config.get('deploy') === true) {
ac.deploy.constructMojitoClientRuntime(ac.assets,
meta.binders);
}

// we don't care much about the views specified in childs
// and for the parent, we have a fixed one.

meta.view = meta.view || {};

meta.view.name = 'index';

// 1. mixing bottom and top fragments from assets into
// the template data, along with title and mojito version.
// 2. mixing meta with child metas, along with some extra
// headers.

ac.done(
Y.merge(data, ac.assets.renderLocations(), {

title: ac.shaker.title || ac.config.get('title') || 'Powered by Mojito',
mojito_version: Y.mojito.version

}),
Y.mojito.util.metaMerge(meta, {

http: {
headers: {
'content-type': 'text/html; charset="utf-8"'
}
}

}, true)
);

});

},

/**
* Renders a child mojit based on a config called "child" and
* the "assets" collection specified in the specs.
* @method _renderChild
* @protected
* @param {Object} ac Action Context Object.
* @param {Function} callback The callback.
*/
_renderChild: function (ac, callback) {
// Grab the "child" from the config an add it as the
// only item in the "children" map.
var child = ac.config.get('child'),
Expand All @@ -37,55 +94,19 @@ YUI.add('ShakerHTMLFrameMojit', function(Y, NAME) {
assets: ac.config.get('assets')
};

Y.log('executing ShakerHTMLFrameMojit child', 'mojito', 'qeperf');

// Now execute the child as a composite
ac.composite.execute(cfg, function(data, meta) {

// Make sure we have meta
meta.http = meta.http || {};
meta.http.headers = meta.http.headers || {};

// Make sure our Content-type is HTML
meta.http.headers['content-type'] =
'text/html; charset="utf-8"';

// Set the default data
data.title = ac.config.get('title') ||
'Powered by Mojito ' + Y.mojito.version;
data.mojito_version = Y.mojito.version;

data.enableDynamicTitle = ac.config.get('enableDynamicTitle');

// Add all the assets we have been given to our local store
ac.assets.addAssets(meta.assets);

// SHAKER RUNTIME!
// NOTE: We move the deployment of the client to within Shaker addon...
ac.shaker.run(meta);

// Attach assets found in the "meta" to the page
Y.Object.each(ac.assets.getAssets(), function(types, location) {
if (!data[location]) {
data[location] = '';
}
Y.Object.each(types, function(assets, type) {
data[location] += ac.shaker.renderListAsHtmlAssets(assets, type);
});
});

meta.view = {name: 'index'};

Y.log('ShakerHTMLFrameMojit done()', 'mojito', 'qeperf');

ac.done(data, meta);
});
ac.composite.execute(cfg, callback);
}

};

}, '0.1.0', {requires: [
'mojito-composite-addon',
'mojito',
'mojito-util',
'mojito-assets-addon',
'mojito-deploy-addon',
'mojito-config-addon',
'mojito-composite-addon',
'mojito-shaker-addon'
]});

0 comments on commit a37452b

Please sign in to comment.