Skip to content

Commit

Permalink
Merge c00184c into e6c2484
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jan 13, 2014
2 parents e6c2484 + c00184c commit ff4a2f2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 58 deletions.
94 changes: 52 additions & 42 deletions lib/base.js
Expand Up @@ -313,55 +313,64 @@ Base.prototype.run = function run(args, cb) {

var methods = Object.keys(Object.getPrototypeOf(this));

var resolve = function (method) {
var rules = {
underscore: method.charAt(0) !== '_',
runnable: _.isFunction(Object.getPrototypeOf(self)[method]),
initialize: !/^(constructor|initialize)$/.test(method),
valid: function () {
return this.underscore && this.initialize && this.runnable;
}
};
if (!methods.length) {
throw new Error('This Generator is empty. Add at least one method for it to be runned.');
}

return function (next) {
if (!rules.valid()) {
return next();
}
this.env.runLoop.once('end', function () {
self.emit('end');
cb();
});

var done = function (err) {
if (err) {
self.emit('error', err);
methods
.filter(function (name) {
var rules = {
underscore: name.charAt(0) !== '_',
runnable: _.isFunction(Object.getPrototypeOf(self)[name]),
initialize: !/^(constructor|initialize)$/.test(name),
valid: function () {
return this.underscore && this.initialize && this.runnable;
}

// resolve file conflicts after every method completes.
self.conflicter.resolve(function (err) {
if (err) {
return self.emit('error', err);
}

next();
});
};

var running = false;
self.async = function () {
running = true;
return done;
};

self.emit(method);
self.emit('method', method);
return rules.valid();
})
.forEach(function (name) {

// Make sure we run the method assigned to the prototype, not an instance value.
Object.getPrototypeOf(self)[method].apply(self, args);
var method = Object.getPrototypeOf(self)[name];

if (!running) {
done();
}
};
};
self.env.runLoop.add(function (completed) {
var done = function (err) {
if (err) {
self.emit('error', err);
}

// resolve file conflicts after every method completes.
self.conflicter.resolve(function (err) {
if (err) {
return self.emit('error', err);
}
completed();
});
};

var running = false;
self.async = function () {
running = true;
return done;
};

self.emit(name);
self.emit('method', name);
method.apply(self, args);

if (!running) {
done();
}
});
});

async.series(methods.map(resolve), this.runHooks.bind(this, cb));
this.runHooks();

return this;
};
Expand All @@ -376,8 +385,9 @@ Base.prototype.runHooks = function runHooks(cb) {
var self = this;
var hooks = this._hooks;

cb = _.isFunction(cb) ? cb : function () {};

var callback = function (err) {
self.emit('end');
cb(err);
};

Expand Down
8 changes: 3 additions & 5 deletions lib/env/index.js
Expand Up @@ -6,6 +6,7 @@ var events = require('events');
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var _ = require('lodash');
var GroupedQueue = require('grouped-queue');

var debug = require('debug')('generators:environment');

Expand Down Expand Up @@ -48,6 +49,7 @@ var Environment = module.exports = function Environment(args, opts, adapter) {
this.adapter = adapter || new TerminalAdapter();
this.cwd = this.options.cwd || process.cwd();
this.store = new Store();
this.runLoop = new GroupedQueue();

this.lookups = ['.', 'generators', 'lib/generators'];
this.aliases = [];
Expand Down Expand Up @@ -296,10 +298,6 @@ Environment.prototype.run = function run(args, options, done) {
return generator;
}

if (typeof done === 'function') {
generator.on('end', done);
}

if (options.help) {
return console.log(generator.help());
}
Expand All @@ -314,7 +312,7 @@ Environment.prototype.run = function run(args, options, done) {
generator.on('end', this.emit.bind(this, name + ':end'));
generator.on('end', this.emit.bind(this, 'generators:end'));

return generator.run();
return generator.run(done);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -47,7 +47,8 @@
"download": "~0.1.6",
"request": "~2.30.0",
"file-utils": "~0.1.1",
"class-extend": "~0.1.0"
"class-extend": "~0.1.0",
"grouped-queue": "~0.1.2"
},
"devDependencies": {
"proxyquire": "~0.5.1",
Expand Down
11 changes: 10 additions & 1 deletion test/base.js
Expand Up @@ -195,7 +195,7 @@ describe('yeoman.generators.Base', function () {
this.TestGenerator.prototype.async1 = function () {
async1Running = true;
var done = this.async();
setTimeout(function() {
setTimeout(function () {
async1Running = false;
async1Runned = true;
done();
Expand All @@ -208,6 +208,15 @@ describe('yeoman.generators.Base', function () {
};
this.testGen.run();
});

it('throws if no method is available', function () {
var gen = new (yo.generators.Base.extend())([], {
resolved: 'generator-ember/all/index.js',
namespace: 'dummy',
env: this.env
});
assert.throws(gen.run.bind(gen));
});
});

describe('#_', function () {
Expand Down
22 changes: 13 additions & 9 deletions test/env.js
Expand Up @@ -186,13 +186,15 @@ describe('Environment', function () {
describe('#run()', function () {
beforeEach(function () {
var self = this;
this.stub = function () {
self.args = arguments;
Base.apply(this, arguments);
};
this.Stub = Base.extend({
constructor: function () {
self.args = arguments;
Base.apply(this, arguments);
},
exec: function () {}
});
this.runMethod = sinon.spy(Base.prototype, 'run');
util.inherits(this.stub, Base);
this.env.registerStub(this.stub, 'stub:run');
this.env.registerStub(this.Stub, 'stub:run');
});

afterEach(function () {
Expand Down Expand Up @@ -314,10 +316,12 @@ describe('Environment', function () {
assert.equal(this.completeDummy, this.env.get('dummy:complete'));
});

it('extend simple function with Base', function () {
it('extend simple function with Base', function (done) {
assert.implement(this.env.get('dummy:simple'), Base);
this.env.run('dummy:simple');
assert.ok(this.simpleDummy.calledOnce);
this.env.run('dummy:simple', function () {
assert.ok(this.simpleDummy.calledOnce);
done();
}.bind(this));
});

it('throws if invalid generator', function () {
Expand Down

0 comments on commit ff4a2f2

Please sign in to comment.