Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support All file formats and pass file config in temp file #11

Merged
merged 8 commits into from
Mar 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
tmp
test/output/
80 changes: 54 additions & 26 deletions lib/parallelizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var path = require('path');
var util = require('util');
var async = require('async');
var _ = require('lodash');
var uuid = require('uuid');
var fs = require('fs');
var os = require('os');

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

Expand All @@ -29,32 +32,41 @@ Parallelizer.prototype.exec = function(task, target) {
// spawnOptions = { stdio: 'inherit' };
// }

lpad(' ');
var ok = true;
async.forEach(splittedFilesSrc, function(filesSrc, next) {
var filesOption = '--grunt-parallelize-child-filesSrc=' + filesSrc.join(path.delimiter);
var cp = this.grunt_.util.spawn({
grunt: true,
args: [['parallelize', task, target].join(':')].concat(this.grunt_.option.flags(), filesOption),
opts: spawnOptions
}, function(err, result, code) {
if ((err || code > 0)) {
ok = false;
if (result.stderr) {
this.grunt_.warn(result.stderr);
}
var self = this;
// create /tmp if not exists
fs.mkdir(os.tmpdir(), function (err) {
lpad(' ');
var ok = true;
async.forEach(splittedFilesSrc, function(filesSrc, next) {
// need to put filesSrc in a temp file because of command line arg data limit
var tmpFile = path.join(os.tmpdir(), task + target + uuid.v4());
fs.writeFile(tmpFile, JSON.stringify(filesSrc), function (err) {
if (err) { throw err; }
var filesOption = '--grunt-parallelize-child-filesSrc=' + tmpFile;
var cp = self.grunt_.util.spawn({
grunt: true,
args: [['parallelize', task, target].join(':')].concat(self.grunt_.option.flags(), filesOption),
opts: spawnOptions
}, function(err, result, code) {
if ((err || code > 0)) {
ok = false;
if (result.stderr) {
self.grunt_.warn(result.stderr);
}
}
self.grunt_.log.write('\n' + self.filterOutput_(result.stdout, task, target) + '\n');
next();
});
self.cpCache_.push(cp);
});
}, function() {
lpad();
if (!ok) {
self.grunt_.log.writeln('');
}
this.grunt_.log.write('\n' + this.filterOutput_(result.stdout, task, target) + '\n');
next();
}.bind(this));
this.cpCache_.push(cp);
}.bind(this), function() {
lpad();
if (!ok) {
this.grunt_.log.writeln('');
}
cb(ok);
}.bind(this));
cb(ok);
});
});
};

Parallelizer.prototype.filterOutput_ = function(output, task, target) {
Expand Down Expand Up @@ -101,7 +113,23 @@ Parallelizer.prototype.getNormalizedFilesSrc_ = function(task, target) {
var configPath = [task, target];
var config = this.grunt_.config(configPath);
var files = this.grunt_.task.normalizeMultiTaskFiles(config);
return _(files).chain().pluck('src').flatten().uniq().value();
// if any file objects have a destination, can assume that not all src files
// can be merged into single list
var hasDest = false;
files.forEach(function (fileObj) {
if (fileObj.dest !== undefined){ hasDest = true; }
});
var normalizedFiles;
if (hasDest){
normalizedFiles = files;
} else {
normalizedFiles = _(files).chain().pluck('src').flatten().uniq().value();
normalizedFiles = normalizedFiles.map(function (srcFile) {
return { src: srcFile, };
});
}

return normalizedFiles;
};

module.exports = Parallelizer;
46 changes: 21 additions & 25 deletions lib/taskrunner.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
'use strict';

var _ = require('lodash');
var path = require('path');
var fs = require('fs');

module.exports = function(grunt, childFilesSrcOption, task, target) {
var childFilesSrc = childFilesSrcOption.split(path.delimiter);
var configPath = [task, target];
var config = grunt.config(configPath);
var kindOf = grunt.util.kindOf;
var type = kindOf(config);
if (type === 'array') {
grunt.config(configPath, childFilesSrc);
} else if (type === 'object') {
if (_.contains(['string', 'array'], kindOf(config.src))) {
// Compact Format
grunt.config(configPath.concat('src'), childFilesSrc);
} else if ('array' === kindOf(config.files)) {
// Files Array Format
grunt.config(configPath.concat('files'), [{src: childFilesSrc}]);
} else if ('object' === kindOf(config.files)) {
// Files Object Format
throw new Error('The config of ' + task + ' is Files Object Format.');
} else {
throw new Error('The config of ' + task + ' is not supported.');
var done = grunt.task.current.async();
fs.readFile(childFilesSrcOption, function (err, data) {
if (err) { throw err; }
var childFilesSrc = JSON.parse(data);
var configPath = [task, target];

// replace the original file config with the smaller file set assigned by
// the parallelizer
var targetCfg = grunt.config.get(configPath);
if (targetCfg.hasOwnProperty('dest')){
delete targetCfg.dest;
}
if (targetCfg.hasOwnProperty('src')){
delete targetCfg.src;
}
} else {
throw new Error('The config of ' + task + ' is not supported.');
}
grunt.task.run(configPath.join(':'));
targetCfg.files = childFilesSrc;
grunt.config.set(configPath, targetCfg);

grunt.task.run(configPath.join(':'));
done();
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"async": "~0.9.0",
"lodash": "~3.1.0",
"lpad": "~1.0.0"
"lpad": "~1.0.0",
"uuid": "^2.0.1"
},
"devDependencies": {
"expect.js": "~0.3.1",
Expand Down
25 changes: 25 additions & 0 deletions test/cases/compactFormatWithDest.Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = function(grunt) {
grunt.initConfig({
writefilesrc: {
test_src: {
dest: '../output/compactFormatWithDest-1.txt',
src: '../fixtures/*.txt',
}
},

parallelize: {
writefilesrc: {
test_src: 2,
}
}
});

// Load this tasks.
grunt.loadTasks('../../tasks');
// Load tasks for testing.
grunt.loadTasks('../tasks');
// Set defaut task.
grunt.registerTask('default', ['parallelize:writefilesrc:test_src']);
};
33 changes: 33 additions & 0 deletions test/cases/filesArrayFormatWithDest.Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = function(grunt) {
grunt.initConfig({
writefilesrc: {
test: {
files: [
{ dest: '../output/filesArrayFormatWithDest-1.txt', src: '../fixtures/*.txt', },
],
},
test_multi: {
files: [
{ dest: '../output/filesArrayFormatWithDest-2.txt', src: '../fixtures/1.txt', },
{ dest: '../output/filesArrayFormatWithDest-3.txt', src: ['../fixtures/3.txt', '../fixtures/4.txt'], },
],
},
},

parallelize: {
writefilesrc: {
test: 2,
test_multi: 2,
}
}
});

// Load this tasks.
grunt.loadTasks('../../tasks');
// Load tasks for testing.
grunt.loadTasks('../tasks');
// Set defaut task.
grunt.registerTask('default', ['parallelize:writefilesrc:test', 'parallelize:writefilesrc:test_multi']);
};
33 changes: 33 additions & 0 deletions test/cases/filesObjectFormatWithDest.Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = function(grunt) {
grunt.initConfig({
writefilesrc: {
test: {
files: {
'../output/filesObjectFormatWithDest-1.txt': '../fixtures/*.txt',
},
},
test_multi: {
files: {
'../output/filesObjectFormatWithDest-2.txt': '../fixtures/1.txt',
'../output/filesObjectFormatWithDest-3.txt': ['../fixtures/3.txt', '../fixtures/4.txt'],
},
},
},

parallelize: {
writefilesrc: {
test: 2,
test_multi: 2,
}
}
});

// Load this tasks.
grunt.loadTasks('../../tasks');
// Load tasks for testing.
grunt.loadTasks('../tasks');
// Set defaut task.
grunt.registerTask('default', ['parallelize:writefilesrc:test', 'parallelize:writefilesrc:test_multi']);
};
1 change: 1 addition & 0 deletions test/fixtures/file_output/compactFormatWithDest-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/1.txt","../fixtures/2.txt","../fixtures/3.txt","../fixtures/4.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesArrayFormatWithDest-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/1.txt","../fixtures/2.txt","../fixtures/3.txt","../fixtures/4.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesArrayFormatWithDest-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/1.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesArrayFormatWithDest-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/3.txt","../fixtures/4.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesObjectFormatWithDest-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/1.txt","../fixtures/2.txt","../fixtures/3.txt","../fixtures/4.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesObjectFormatWithDest-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/1.txt"]
1 change: 1 addition & 0 deletions test/fixtures/file_output/filesObjectFormatWithDest-3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["../fixtures/3.txt","../fixtures/4.txt"]
57 changes: 55 additions & 2 deletions test/parallelize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('grunt-parallelize', function() {
});

describe('grunt parallelize', function() {
this.timeout(5000);
this.timeout(10000);
it('runs all tasks', function(done) {
testGruntfile('runAllTasks', done);
});
Expand All @@ -48,8 +48,44 @@ describe('grunt-parallelize', function() {
});
});

describe('Writes files', function() {
it('Compact Format', function(done){
testGruntfileWithFileWrite('compactFormatWithDest', done);
});

it('Files Array Format', function(done){
testGruntfileWithFileWrite('filesArrayFormatWithDest', done);
});

it('Files Object Format', function(done){
testGruntfileWithFileWrite('filesObjectFormatWithDest', done);
});
});
});

function testGruntfileWithFileWrite(name, callback){
var prefix = __dirname + '/cases/' + name;
var gruntfile = prefix + '.Gruntfile.js';
var expectedDir = __dirname + '/fixtures/file_output/';
var expectedFiles = [];
fs.readdirSync(expectedDir).forEach(function(file){
if (file.indexOf(name) === 0) {
expectedFiles.push(file);
}
});
var outputDir = __dirname + '/output/';
// clean up the output dir
deleteFolderRecursive(outputDir);

runGruntfile(gruntfile, function(err, stdout, stderr) {
expectedFiles.forEach(function(file){
expect(fs.readFileSync(outputDir + file, {encoding: 'utf8'}))
.to.be(fs.readFileSync(expectedDir + file, {encoding: 'utf8'}));
});
callback(err);
});
}

function testGruntfile(name, callback) {
var prefix = __dirname + '/cases/' + name;
var gruntfile = prefix + '.Gruntfile.js';
Expand All @@ -65,7 +101,8 @@ function testGruntfile(name, callback) {
return;
}
try {
expect(stdout).to.be(expected);
expect(stdout.split('\n').sort().join('\n'))
.to.be(expected.split('\n').sort().join('\n'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change could pass failure result. but ok, I'll fix it later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #14

} catch (e) {
console.log('expected:');
console.log(expected);
Expand All @@ -82,3 +119,19 @@ function runGruntfile(gruntfile, callback) {
var options = {};
exec(cmd, options, callback);
}

function deleteFolderRecursive(path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + '/' + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
34 changes: 34 additions & 0 deletions test/tasks/writefilesrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* grunt-parallelize
* https://github.com/teppeis/grunt-parallelize
*
* Copyright (c) 2013 Teppei Sato <teppeis@gmail.com>
* Licensed under the MIT license.
*/
'use strict';

/*
* Write src files to dest file for test.
*/
function createMultiTask(grunt, name) {
grunt.registerMultiTask(name, 'Check the files exits.', function() {
var done = this.async();
grunt.util.async.forEachSeries(this.files, function(file, next) {
if (!file.dest) {
throw new Error('Missing dest');
}

grunt.file.write(file.dest, JSON.stringify(file.src) + '\n', {encoding: 'utf8'});
next();
}, function(err) {
done(err);
});
});
}

module.exports = function(grunt) {
createMultiTask(grunt, 'writefilesrc');
};

module.exports.createMultiTask = createMultiTask;