Skip to content

Commit

Permalink
Merge pull request #11 from tschaub/royriojas-any-newer-fixed
Browse files Browse the repository at this point in the history
Run task with all src files if corresponding dest file is absent.
  • Loading branch information
tschaub committed Sep 26, 2013
2 parents 9c114e9 + da00be6 commit 7f97c76
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"chai": "1.7.2",
"grunt-cafe-mocha": "0.1.8",
"wrench": "1.5.1",
"tmp": "0.0.21"
"tmp": "0.0.21",
"grunt-contrib-clean": "0.5.0"
},
"peerDependencies": {
"grunt": "~0.4.1"
Expand Down
16 changes: 13 additions & 3 deletions tasks/newer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function pluckConfig(id) {
function createTask(grunt, any) {
return function(name, target) {
var tasks = [];
var prefix = this.name;
if (!target) {
var prefix = this.name;
Object.keys(grunt.config(name)).forEach(function(target) {
if (!/^_|^options$/.test(target)) {
tasks.push(prefix + ':' + name + ':' + target);
Expand Down Expand Up @@ -80,10 +80,16 @@ function createTask(grunt, any) {
* created. In this case, we don't need to re-run src files that map
* to dest files that were already created.
*/
if (obj.dest && grunt.file.exists(obj.dest)) {
var existsDest = obj.dest && grunt.file.exists(obj.dest);
if (existsDest) {
time = Math.max(fs.statSync(obj.dest).mtime, previous);
} else {
time = previous;
if (obj.dest) {
// The dest file may have been removed. Run with all src files.
time = 0;
} else {
time = previous;
}
}
var src = obj.src.filter(function(filepath) {
var newer = fs.statSync(filepath).mtime > time;
Expand All @@ -92,6 +98,10 @@ function createTask(grunt, any) {
}
return newer;
});

if (!existsDest && prefix === 'any-newer') {
modified = true;
}
return {src: src, dest: obj.dest};
}).filter(function(obj) {
return obj.src && obj.src.length > 0;
Expand Down
110 changes: 110 additions & 0 deletions test/fixtures/newer-clean-dest/gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var path = require('path');


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

var log = [];

grunt.initConfig({
newer: {
options: {
timestamps: path.join(__dirname, '.cache')
}
},
clean: {
one: 'dest/one.js',
all: 'dest'
},
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('../../../node_modules/grunt-contrib-clean/tasks');

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

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',

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

// modify nothing, expect no files
'newer:log',
'assert:that:modified:none',

// remove one dest file, expect one file
'clean:one',
'newer:log',
'assert:that:modified:one',

// remove all dest file, expect all
'clean:all',
'newer:log',
'assert:that:modified:all'

]);

});

};
3 changes: 3 additions & 0 deletions test/fixtures/newer-clean-dest/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-clean-dest/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-clean-dest.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-clean-dest';
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);
});

});
6 changes: 6 additions & 0 deletions test/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ module.exports = function(grunt) {
if (files.length > 0) {
this.data.getLog().push(files);
}
// create all dest files
files.forEach(function(obj) {
if (obj.dest) {
grunt.file.write(obj.dest, '');
}
});
});


Expand Down

0 comments on commit 7f97c76

Please sign in to comment.