Skip to content

Commit

Permalink
Break up init and statup of Jake program
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Oct 5, 2012
1 parent 059941a commit 35847ea
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 77 deletions.
109 changes: 53 additions & 56 deletions lib/jake.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ var Invocation = function (taskName, args) {
this.args = args;
};

/**
* @namespace
* The main namespace for Jake
*/
// And so it begins
jake = new EventEmitter();

// Globalize jake and top-level API methods (e.g., `task`, `desc`)
global.jake = jake;
utils.mixin(global, api);

// Copy utils onto base jake
utils.mixin(jake, utils);
// File utils should be aliased directly on base jake as well
utils.mixin(jake, utils.file);

utils.mixin(jake, new (function () {

Expand Down Expand Up @@ -90,6 +94,15 @@ utils.mixin(jake, new (function () {
// Saves the description created by a 'desc' call that prefaces a
// 'task' call that defines a task.
this.currentTaskDescription = null;
this.program = new Program()
this.FileList = require('./file_list').FileList;
this.PackageTask = require('./package_task').PackageTask;
this.NpmPublishTask = require('./npm_publish_task').NpmPublishTask;
this.TestTask = require('./test_task').TestTask;
this.Task = Task;
this.FileTask = FileTask;
this.DirectoryTask = DirectoryTask;
this.Namespace = Namespace;

this.parseAllTasks = function () {
var _parseNs = function (name, ns) {
Expand Down Expand Up @@ -230,73 +243,57 @@ utils.mixin(jake, new (function () {
return task;
};

this.run = function () {
var args = Array.prototype.slice.call(arguments)
, program = new Program()
, opts
, envVars
, taskNames
, rootTask;

this.init = function () {
var self = this;
process.addListener('uncaughtException', function (err) {
program.handleErr(err);
self.program.handleErr(err);
});

program.parseArgs(args);
};

if (!program.preemptiveOption()) {
opts = program.opts
envVars = program.envVars;
this.run = function () {
var args = Array.prototype.slice.call(arguments)
, program = this.program
, loader = this.loader
, preempt
, opts;

// Globalize top-level API methods (e.g., `task`, `desc`)
for (var p in api) {
global[p] = api[p];
}
program.parseArgs(args);
program.init();

// Convenience aliases
// ======
jake.program = program;
utils.mixin(jake, utils);
// File utils should be aliased directly on the base namespace
utils.mixin(jake, utils.file);
jake.FileList = require('./file_list').FileList;
jake.PackageTask = require('./package_task').PackageTask;
jake.NpmPublishTask = require('./npm_publish_task').NpmPublishTask;
jake.TestTask = require('./test_task').TestTask;

// Enhance env with any env vars passed in
for (var p in envVars) { process.env[p] = envVars[p]; }

this.loader.load(opts);

// Set working dir
var dirname = opts.directory;
if (dirname) {
process.chdir(dirname);
}
preempt = program.firstPreemptiveOption();
if (preempt) {
preempt();
}
else {
opts = program.opts;

taskNames = program.taskNames;
taskNames = taskNames.length ? taskNames : ['default'];
task('__root__', taskNames, function () {});
// Load Jakefile and jakelibdir files
loader.loadFile(opts.jakefile);
loader.loadDirectory(opts.jakelibdir);

// Run with `jake -T`, just show descriptions
if (opts.tasks) {
jake.showAllTaskDescriptions(opts.tasks);
this.showAllTaskDescriptions(opts.tasks);
}
else {
rootTask = jake.Task['__root__'];
rootTask.once('complete', function () {
jake.emit('complete');
});
rootTask.invoke();
// Set working dir
var dirname = opts.directory;
if (dirname) {
if (utils.file.existsSync(directory) &&
fs.statSync(directory).isDirectory()) {
process.chdir(dirname);
}
else {
throw new Error(dirname + ' is not a valid directory path');
}
}

program.run();
}
}
};

})());

jake.Task = Task;
jake.FileTask = FileTask;
jake.DirectoryTask = DirectoryTask;
jake.Namespace = Namespace;

module.exports = jake;
24 changes: 9 additions & 15 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ Loader = function () {

var JAKEFILE_PAT = /\.jake(\.js|\.coffee)?$/;

var _requireCoffee = function () {
try {
return require('coffee-script');
}
catch (e) {
fail('CoffeeScript is missing! Try `npm install coffee-script`');
}
};

this.loadFile = function (file) {
var jakefile = file ?
file.replace(/\.js$/, '').replace(/\.coffee$/, '') : 'Jakefile'
Expand Down Expand Up @@ -80,21 +89,6 @@ Loader = function () {
});
}
};

var _requireCoffee = function() {
try {
return require('coffee-script');
}
catch (e) {
fail('CoffeeScript is missing! Try `npm install coffee-script`');
}
};

this.load = function (opts) {
this.loadFile(opts.jakefile);
this.loadDirectory(opts.jakelibdir);
};

};

module.exports.Loader = Loader;
53 changes: 47 additions & 6 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,63 @@ Program.prototype = new (function () {

this.parseArgs = function (args) {
var result = (new parseargs.Parser(optsReg)).parse(args);
this.opts = result.opts;
this.taskNames = result.taskNames;
this.envVars = result.envVars;
this.setOpts(result.opts);
this.setTaskNames(result.taskNames);
this.setEnvVars(result.envVars);
};

this.preemptiveOption = function () {
this.setOpts = function (options) {
var opts = options || {};
utils.mixin(this.opts, opts);
};

this.setTaskNames = function (names) {
if (names && !Array.isArray(names)) {
throw new Error('Task names must be an array');
}
this.taskNames = names.length ? names : ['default'];
};

this.setEnvVars = function (vars) {
this.envVars = vars || null;
};

this.firstPreemptiveOption = function () {
var opts = this.opts;
for (var p in opts) {
if (preempts[p]) {
preempts[p]();
return true;
return preempts[p];
}
}
return false;
};

this.init = function (options) {
var self = this;
process.addListener('uncaughtException', function (err) {
self.handleErr(err);
});
if (this.envVars) {
utils.mixin(process.env, this.envVars);
}
this.setOpts(options);
};

this.run = function () {
var taskNames = this.taskNames;

if (!(Array.isArray(taskNames) && taskNames.length)) {
throw new Error('Please pass jake.runTasks an array of task-names');
}
task('__root__', taskNames, function () {});

rootTask = jake.Task['__root__'];
rootTask.once('complete', function () {
jake.emit('complete');
});
rootTask.invoke();
};

})();

die = function (msg) {
Expand Down

0 comments on commit 35847ea

Please sign in to comment.