Skip to content

Commit

Permalink
Merge pull request #8 from tschaub/reconfig
Browse files Browse the repository at this point in the history
Reconfigure modified targets after successful runs.  This allows composed tasks to use template syntax for files config.
  • Loading branch information
tschaub committed Sep 21, 2013
2 parents 2d4ba9f + 98d4c80 commit a3955c1
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tasks/newer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@ function getStamp(dir, name, target) {
return path.join(dir, name, target, 'timestamp');
}

var counter = 0;
var configCache = {};

function cacheConfig(config) {
++counter;
configCache[counter] = config;
return counter;
}

function pluckConfig(id) {
if (!configCache.hasOwnProperty(id)) {
throw new Error('Failed to find id in cache');
}
var config = configCache[id];
delete configCache[id];
return config;
}


function createTask(grunt, any) {
return function(name, target) {
var tasks = [];
if (!target) {
var tasks = [];
var prefix = this.name;
Object.keys(grunt.config(name)).forEach(function(target) {
if (!/^_|^options$/.test(target)) {
Expand All @@ -24,6 +42,8 @@ function createTask(grunt, any) {
timestamps: path.join(__dirname, '..', '.cache')
});
var config = grunt.config.get([name, target]);
var id = cacheConfig(config);
config = grunt.util._.clone(config);

/**
* Special handling for watch task. This is a multitask that expects
Expand Down Expand Up @@ -110,10 +130,15 @@ function createTask(grunt, any) {
grunt.config.set([name, target], config);
}
// case 1, 3 or 4
grunt.task.run([
tasks = [
qualified + (args ? ':' + args : ''),
'newer-timestamp:' + qualified + ':' + options.timestamps
]);
];
// if we modified the config (case 3), reset it to the original after
if (repeat && modified && !any) {
tasks.push('newer-reconfigure:' + qualified + ':' + id);
}
grunt.task.run(tasks);
}
};
}
Expand All @@ -137,4 +162,9 @@ module.exports = function(grunt) {
grunt.file.write(getStamp(dir, name, target), '');
});

grunt.registerTask(
'newer-reconfigure', 'Internal task.', function(name, target, id) {
grunt.config.set([name, target], pluckConfig(id));
});

};
100 changes: 100 additions & 0 deletions test/fixtures/newer-reconfigure/gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
var assert = require('assert');
var path = require('path');


/**
* @param {Object} grunt Grunt.
*/
module.exports = function(grunt) {

var log = [];

grunt.initConfig({
newer: {
options: {
timestamps: path.join(__dirname, '.cache')
}
},
modified: {
one: {
files: [{
expand: true,
cwd: 'src/',
src: 'one.coffee',
dest: 'dest/',
ext: '.js'
}]
},
all: {
files: [{
expand: true,
cwd: 'src/',
src: '**/*.coffee',
dest: 'dest/',
ext: '.js'
}]
},
none: {
src: []
}
},
log: {
all: {
files: [{
expand: true,
cwd: 'src/',
src: '**/*.coffee',
dest: 'dest/',
ext: '.js'
}],
getLog: function() {
return log;
}
}
},
assert: {
that: {
getLog: function() {
return log;
}
}
}
});

grunt.loadTasks('../../../tasks');
grunt.loadTasks('../../../test/tasks');

grunt.registerTask('assert-reconfigured', function() {
var config = grunt.config.get(['log', 'all']);
assert.deepEqual(Object.keys(config).sort(), ['files', 'getLog']);
var files = config.files;
assert.equal(files.length, 1);
assert.deepEqual(Object.keys(files[0]).sort(),
['cwd', 'dest', 'expand', 'ext', 'src']);
assert.equal(files[0].src, '**/*.coffee');
});

grunt.registerTask('default', function() {

grunt.task.run([
// run the log task with newer, expect all files
'newer:log',
'assert:that:modified:all',

// HFS+ filesystem mtime resolution
'wait:1001',

// modify one file
'modified:one',

// run assert task again, expect one file
'newer:log',
'assert:that:modified:one',

// check that log:all task has been reconfigured with original config
'assert-reconfigured'
]);

});

};
3 changes: 3 additions & 0 deletions test/fixtures/newer-reconfigure/src/one.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coffee =
is: 'good'
hot: true
3 changes: 3 additions & 0 deletions test/fixtures/newer-reconfigure/src/two.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
semicolons = true
coffee = true
semicolons = false if coffee
23 changes: 23 additions & 0 deletions test/newer-reconfigure.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var path = require('path');

var helper = require('./helper');

var name = 'newer-reconfigure';
var gruntfile = path.join(name, 'gruntfile.js');

describe(name, function() {
var fixture;

it('runs the default task (see ' + gruntfile + ')', function(done) {
this.timeout(3000);
helper.buildFixture(name, function(error, dir) {
fixture = dir;
done(error);
});
});

after(function(done) {
helper.afterFixture(fixture, done);
});

});

0 comments on commit a3955c1

Please sign in to comment.