Skip to content

Commit

Permalink
Renamed "extras" dir to "tasks" and moved all task .js files in there…
Browse files Browse the repository at this point in the history
…. Reworked (and simplified) code to support this change.
  • Loading branch information
cowboy committed Jan 22, 2012
1 parent 0e8f3af commit 2cf808e
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 48 deletions.
2 changes: 1 addition & 1 deletion grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ config.init({
files: ['test/**/*.js']
},
lint: {
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
files: ['grunt.js', 'lib/**/*.js', 'tasks/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.files>',
Expand Down
51 changes: 16 additions & 35 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,54 +156,35 @@ exports.clearRequireCache = function() {
});
};

// Get an array of user-specified task directories.
function userTasks() {
var tasksdirs = option('tasks') ? option('tasks').slice() : [];
return tasksdirs.map(function(relpath) {
return path.resolve(relpath);
});
}

// Access files in the user's ".grunt" folder.
exports.userpath = function(filepath) {
var win32 = process.platform === 'win32';
var homepath = process.env[win32 ? 'USERPROFILE' : 'HOME'];
return path.resolve(homepath, '.grunt', filepath);
};

// Path to built-in task "extras" files.
exports.extraspath = function(name) {
return path.resolve(__dirname, '../../extras', name);
};

// Get a list of relevant task-related "extras" paths.
exports.extraspaths = function(name) {
var extraspaths = [];
// Path to user's "extra" files.
extraspaths.push(exports.userpath(path.join('tasks', name)));
// Paths to user-specified --tasks dirs.
extraspaths = extraspaths.concat(userTasks().map(function(filepath) {
return path.join(filepath, name);
}));
// Path to task's built-in extra files.
extraspaths.push(exports.extraspath(name));
// Return only directories that actually exist!
return extraspaths.filter(function(filepath) {
return path.existsSync(filepath) && fs.statSync(filepath).isDirectory();
});
};

// Get a list of task paths.
// Get a list of task paths (or task-specific extraspaths).
exports.taskpaths = function() {
var args;
var taskpaths = [];
// Path to user's "tasks" files.
taskpaths.push(exports.userpath('tasks'));
// Paths to user-specified --tasks dirs.
taskpaths = taskpaths.concat(userTasks());
var optiontasks = option('tasks') ? option('tasks').slice() : [];
taskpaths = taskpaths.concat(optiontasks.map(function(relpath) {
return path.resolve(relpath);
}));
// Path to built-in task files.
taskpaths.push(path.join(__dirname, 'tasks'));
taskpaths.push(path.resolve(__dirname, '../../tasks'));
// If arguments were specified, join them to pathnames.
if (arguments.length > 0) {
args = util.toArray(arguments);
taskpaths = taskpaths.map(function(dirpath) {
return path.join.apply(path, [dirpath].concat(args));
});
}
// Return only directories that actually exist!
return taskpaths.filter(function(filepath) {
return path.existsSync(filepath) && fs.statSync(filepath).isDirectory();
return taskpaths.filter(function(dirpath) {
return path.existsSync(dirpath) && fs.statSync(dirpath).isDirectory();
});
};
4 changes: 2 additions & 2 deletions lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ exports.registerTask = function(name, info, fn) {
// Override task function.
var _fn = task.fn;
task.fn = function() {
// Make a list of task-related extras dirs available.
this.extraspaths = file.extraspaths.bind(file, this.name);
// Make a list of task-related extras paths available.
this.extraspaths = file.taskpaths.bind(file, this.name);
// Initialize the errorcount for this task.
errorcount = fail.errorcount;
// If this task was an alias or a basic task called without arguments,
Expand Down
File renamed without changes.
21 changes: 11 additions & 10 deletions lib/grunt/tasks/init.js → tasks/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function getDefaults() {
if (defaults) { return defaults; }
defaults = {};
// Search all available init-specific extras paths for a defaults.json file.
var paths = file.extraspaths('init').map(function(dirpath) {
var paths = file.taskpaths('init').map(function(dirpath) {
return path.join(dirpath, 'defaults.json');
}).filter(function(filepath) {
return path.existsSync(filepath);
Expand All @@ -45,10 +45,11 @@ function getDefaults() {

// An array of all available license files.
function availableLicenses() {
var licensespath = file.extraspath('init/licenses');
return fs.readdirSync(licensespath).map(function(filename) {
return filename.replace(/^LICENSE-/, '');
});
return file.taskpaths('init/licenses').reduce(function(arr, filepath) {
return arr.concat(fs.readdirSync(filepath).map(function(filename) {
return filename.replace(/^LICENSE-/, '');
}));
}, []);
}

task.registerInitTask('init', 'Initialize a project from a predefined template.', function(name) {
Expand Down Expand Up @@ -94,15 +95,15 @@ task.registerInitTask('init', 'Initialize a project from a predefined template.'
// Expose any user-specified default init values.
defaults: getDefaults(),
// Search init extras paths for filename.
srcpath: function(filename) {
srcpath: function(filepath) {
var result = null;
searchpaths.some(function(obj) {
return obj.subdirs.filter(function(dirname) {
return dirname === name;
}).some(function(dirname) {
var filepath = path.join(obj.path, dirname, filename);
if (path.existsSync(filepath)) {
result = filepath;
var abspath = path.join(obj.path, dirname, filepath);
if (path.existsSync(abspath)) {
result = abspath;
return true;
}
});
Expand Down Expand Up @@ -132,7 +133,7 @@ task.registerInitTask('init', 'Initialize a project from a predefined template.'
var abssrcpath = init.srcpath(srcpath);
var absdestpath = init.destpath(destpath);
if (!path.existsSync(abssrcpath)) {
abssrcpath = file.extraspath('init/misc/placeholder');
abssrcpath = init.srcpath('../misc/placeholder');
}
verbose.or.write('Writing ' + destpath + '...');
try {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2cf808e

Please sign in to comment.