Permalink
Browse files

Merge branch 'master' into extending_template_engine_support

  • Loading branch information...
2 parents e1221bb + d950b22 commit c8d35aa4eaf3d1fa296073679e65881b05f2a2f4 @larzconwell larzconwell committed Jun 24, 2012
View
@@ -2,198 +2,9 @@
require('./lib/geddy')
var fs = require('fs')
- , child_process = require('child_process')
- , path = require('path')
- , exec = child_process.exec
- , inflection = require('./deps/inflection')
- , utils = require('./lib/utils')
- , Templato = require('./deps/templato')
- , ejs = require('./lib/template/adapters/ejs')
- , createPackageTask;
-
-var JSPAT = /\.js$/;
-
-namespace('gen', function () {
- var _writeTemplate = function (name, filename, dirname, opts) {
- var templato = new Templato
- , names = _getInflections(name)
- , text = fs.readFileSync(path.join(__dirname,
- 'templates', filename +'.ejs'), 'utf8').toString()
- , templ
- , filePath;
- // Render with the right model name
- templ = new ejs.Template({text: text, templato: templato});
- templ.process({data: {names: names}});
- filePath = path.join('app', dirname,
- names.filename[opts.inflection] + '.js');
- fs.writeFileSync(filePath, templ.markup, 'utf8');
- console.log('[ADDED] ' + filePath);
- }
-
- , _getInflections = function (nameParam) {
- var name = utils.string.snakeize(nameParam)
- , namePlural = inflection.pluralize(name)
- , names = {
- filename: {
- singular: name
- , plural: namePlural
- }
- , constructor: {
- singular: utils.string.camelize(name, true)
- , plural: utils.string.camelize(namePlural, true)
- }
- , property: {
- singular: utils.string.camelize(name)
- , plural: utils.string.camelize(namePlural)
- }
- };
- return names;
- };
-
- // Creates a new Geddy app scaffold
- task('app', [], function (appName) {
- if (!appName) {
- throw new Error('No app-name specified.');
- }
- var mkdirs = [
- ''
- , 'config'
- , 'app/models'
- , 'app/controllers'
- , 'lib'
- , 'log'
- , 'node_modules'
- ]
- , cps = [
- , ['views', 'app']
- , ['public', '']
- , ['router.js', 'config']
- , ['init.js', 'config']
- , ['environment.js', 'config']
- , ['development.js', 'config']
- , ['production.js', 'config']
- , ['main.js', 'app/controllers']
- , ['application.js', 'app/controllers']
- , ['favicon.ico', 'public']
- ];
- mkdirs.forEach(function (d) {
- jake.mkdirP(path.join(appName, d));
- });
- cps.forEach(function (cp) {
- jake.cpR(path.join(__dirname, 'templates/base', cp[0]),
- path.join(appName, cp[1]));
- });
- console.log('Created app ' + appName + '.');
- });
-
- // Creates a resource-based route with model and controller
- task('resource', function (name) {
- jake.Task['gen:model'].invoke(name);
- jake.Task['gen:controller'].invoke(name);
- jake.Task['gen:route'].invoke(name);
- jake.Task['gen:views'].invoke(name);
- });
-
- task('model', [], function (name) {
- _writeTemplate(name, 'resource_model', 'models',
- {inflection: 'singular'});
- });
-
- task('controller', [], function (name) {
- _writeTemplate(name, 'resource_controller', 'controllers',
- {inflection: 'plural'});
- });
-
- task('route', [], function (name, opts) {
- var names = _getInflections(name)
- , options = opts || {}
- , routeType = options.bare ? 'Bare' : 'Resource'
- , filePath = path.normalize('config/router.js')
- , newRoute
- , text = fs.readFileSync(filePath, 'utf8').toString()
- , routeArr;
-
- if (options.bare) {
- newRoute = 'router.match(\'/' + names.filename.plural +
- '\').to({controller: \'' + names.constructor.plural +
- '\', action: \'index\'});';
- }
- else {
- newRoute = 'router.resource(\'' + names.filename.plural + '\');'
- }
-
- // Don't add the same route over and over
- if (text.indexOf(newRoute) == -1) {
- // Add the new resource route just above the export
- routerArr = text.split('exports.router');
- routerArr[0] += newRoute + '\n';
- text = routerArr.join('exports.router');
- fs.writeFileSync(filePath, text, 'utf8');
- console.log(routeType + ' ' + names.filename.plural +
- ' route added to ' + filePath);
- }
- else {
- console.log('(' + routeType + ' ' + names.filename.plural +
- ' route already defined in ' + filePath + ')');
- }
- });
-
- task('views', [], function (name, opts) {
- var names = _getInflections(name)
- , options = opts || {}
- , viewDir = path.join('app/views', names.filename.plural)
- , actions
- , cmds = []
- , addActionView = function (action) {
- jake.cpR(path.join(__dirname, 'templates/views',
- action + '.html.ejs'), viewDir);
- };
-
- jake.mkdirP(viewDir);
- jake.mkdirP('app/views/layouts');
-
- addActionView('index');
- // Add views for the other CRUD actions when doing a full-on resource
- if (!options.bare) {
- ['add', 'edit', 'show'].forEach(function (action) {
- addActionView(action);
- });
- }
-
- // Create an app-layout if one doesn't exist
- if (!path.existsSync(path.join(process.cwd(),
- 'app/views/layouts/application.html.ejs'))) {
- jake.cpR(path.join(__dirname, '/templates/views/layout.html.ejs'),
- 'app/views/layouts/application.html.ejs');
- }
-
- console.log('Created view templates.');
- });
-
- task('bareController', [], function (name) {
- _writeTemplate(name, 'bare_controller', 'controllers',
- {inflection: 'plural'});
- jake.Task['gen:route'].invoke(name, {bare: true});
- jake.Task['gen:views'].invoke(name, {bare: true});
- });
-
- task('secret', [], function (name) {
- var filename = path.join(process.cwd(), 'config/environment.js')
- , conf = fs.readFileSync(filename).toString()
- , confArr
- , secret = utils.string.uuid(128);
-
- // Remove any old secret
- conf = conf.replace(/\nconfig.secret.+;\n/, '');
-
- confArr = conf.split('module.exports = config;');
- conf = confArr[0] + "config.secret = '" + secret + "';\n\n" +
- 'module.exports = config;' + confArr[1];
- fs.writeFileSync(filename, conf);
- console.log('app-secret added to environment.js config.');
- });
-
-});
+ , createPackageTask
+ , JSPAT = /\.js$/
+ , testTask;
namespace('doc', function () {
task('generate', ['doc:clobber'], function () {
@@ -219,7 +30,6 @@ namespace('doc', function () {
desc('Generate docs for Geddy');
task('doc', ['doc:generate']);
-
desc('Runs the tests.');
task('test', function () {
var dir = process.cwd()
@@ -251,12 +61,30 @@ var p = new jake.NpmPublishTask('geddy', [
, 'test/**'
]);
-// Don't create the package-tasks when being called as a generator
-if (!process.env.generator) {
- jake.Task['npm:definePackage'].invoke();
- var t = jake.TestTask('Geddy model-adapters', function () {
- this.testName = 'testModelAdapters'
- this.testFiles.include('lib/model/adapters/**/test.js');
+// TODO: This is hacky -- need a better way to poke Jake to
+// set up the package part included in the publish-task
+jake.Task['npm:definePackage'].invoke();
+
+testTask = new jake.TestTask('Geddy', function () {
+ this.testName = 'testBase';
+ this.testFiles.include('test/*.js');
+ this.showDescription = false;
+});
+
+desc('Run the Geddy tests');
+task('test', function () {
+ var t = jake.Task.testBase;
+ t.addListener('error', function (e) {
+ // TODO: Check the type of error, npm-install the libs, and re-run
+ console.error('If you have errors in the templating tests, ' +
+ 'install jade and handlebars in the base geddy directory.');
+ throw e;
});
-}
+ t.invoke();
+}, {async: true});
+
+testTask = new jake.TestTask('Geddy model-adapters', function () {
+ this.testName = 'testModelAdapters'
+ this.testFiles.include('lib/model/adapters/**/test.js');
+});
View
@@ -90,7 +90,7 @@ else if (typeof opts.version != 'undefined') {
else {
// `geddy app foo` or `geddy resource bar` etc. -- run generators
if (cmds.length) {
- filepath = path.normalize(path.join(__dirname, '../Jakefile'));
+ filepath = path.normalize(path.join(__dirname, '..', 'templates', 'Jakefile'));
if (process.platform == 'win32') {
filepath = '"' + filepath + '"';
}
@@ -117,7 +117,7 @@ else {
default:
die(cmds[0] + ' is not a Geddy command.');
}
- cmd += ' generator=true';
+ cmd += ' --quiet'
exec(cmd, function (err, stdout, stderr) {
if (err) {
throw err;
@@ -1,106 +0,0 @@
-var Master
- , master = require('./master')
- , MasterBase = master.Master
- , WorkerData = master.WorkerData
- , MessageParser = require('./hack_message_parser').MessageParser
- , net = require('net')
- , childProcess = require('child_process')
- , binding = process.binding('net')
- , nodeBinary = process.argv[0];
-
-Master = function () {
- // Hack-only FD-stuff
- this._fd = null;
-
- this.init();
-};
-
-// Inherit from normal Master
-Master.prototype = geddy.mixin({}, MasterBase.prototype);
-// Overrides
-geddy.mixin(Master.prototype, new (function () {
-
- this.createWorker = function (dt) {
- var fds
- , retireAt = dt || (new Date()).getTime() + this.config.rotationWindow
- , w
- , data
- , id;
-
- // Hack-only FD-stuff -- lazy-init the shared FD
- // now that we have a config
- if (!this._fd) {
- this._fd = binding.socket('tcp4');
- binding.bind(this._fd, parseInt(this.config.port, 10));
- binding.listen(this._fd, 128);
- }
-
- // Create a pair of sockets that the master process and the
- // child will use to communicate
- // http://osr507doc.sco.com/en/netguide/dusockD.socketpairs_codetext.html
- // Credits: Ext's Connect, http://github.com/extjs/Connect
- fds = binding.socketpair();
- // Spawn the child process, running the original cli script, with the
- // 'spawned' flag, and half of the socket-pair
- // Holy balls, this is hacky
- w = childProcess.spawn(nodeBinary, [__dirname + '/../../bin/cli.js',
- '--spawned=true'], {customFds: [fds[1], -1, -1]});
- // Patch child's stdin
- if (!w.stdin) {
- w.stdin = new net.Stream(fds[0], 'unix');
- }
-
- id = w.pid.toString();
- data = new WorkerData(id, w, retireAt);
- data._fd = this._fd;
- this.workers.addItem(id, data);
-
- this.addWorkerListeners(data);
-
- this.sendMessage(id, {
- method: 'config'
- , data: this.config
- });
- };
-
- this.addWorkerListeners = function (workerData) {
- var self = this
- , worker = workerData.worker
- , id = worker.pid.toString();
-
- workerData._parser = new MessageParser(function (msg) {
- self.receiveMessage(msg);
- });
-
- worker.stdout.addListener('data', function (data) {
- var d = data.toString();
- workerData._parser.parse(d);
- });
-
- worker.stderr.addListener('data', function (data) {
- process.stderr.write(data.toString());
- });
-
- worker.addListener('exit', function (code) {
- self.handleWorkerExit(id);
- });
-
- };
-
- this.sendMessage = function (workerId, msg) {
- var id = workerId.toString()
- , worker = this.workers.getItem(id).worker
- , output;
- try {
- output = JSON.stringify(msg);
- worker.stdin.write(output + '\n', 'ascii', this._fd);
- }
- catch (err) {
- this.killWorker(workerId);
- }
- };
-
-})());
-
-module.exports.Master = Master;
-
Oops, something went wrong. Retry.

0 comments on commit c8d35aa

Please sign in to comment.