Skip to content

Commit

Permalink
Auto-expand this.file.src inside of tasks per gh-532.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Nov 15, 2012
1 parent a239982 commit e394ca8
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 72 deletions.
21 changes: 17 additions & 4 deletions lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,28 @@ task.normalizeMultiTaskFiles = function(data, target) {

// Process each normalized file object as a template.
files.forEach(function(obj) {
// Process src as a template (recursively, if necessary).
if ('src' in obj) {
obj.src = grunt.util.recurse(obj.src, function(src) {
// Expose the un-expanded (but normalized) src list as ._src.
obj._src = obj.src;
// Process obj._src as a template (recursively, if necessary).
obj._src = grunt.util.recurse(obj._src, function(src) {
if (typeof src !== 'string') { return src; }
return grunt.template.process(src);
});
// If src is an array, flatten it. Otherwise, make it into an array.
obj.src = Array.isArray(obj.src) ? grunt.util._.flatten(obj.src) : [obj.src];
// If obj._src is an array, flatten it. Otherwise, make it into an array.
obj._src = Array.isArray(obj._src) ? grunt.util._.flatten(obj._src) : [obj._src];
// Expose an expand-on-demand getter method as .src.
Object.defineProperty(obj, 'src', {
enumerable: true,
get: function fn() {
if (!('result' in fn)) {
fn.result = grunt.file.expand(obj._src);
}
return fn.result;
}
});
}

if ('dest' in obj) {
// Process dest as a template.
obj.dest = grunt.template.process(obj.dest);
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
102 changes: 63 additions & 39 deletions test/grunt/task_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,75 @@ if (grunt.task.searchDirs.length === 0) {
}

exports['task.normalizeMultiTaskFiles'] = {
setUp: function(done) {
this.cwd = process.cwd();
process.chdir('test/fixtures/files');
done();
},
tearDown: function(done) {
process.chdir(this.cwd);
done();
},
'normalize': function(test) {
test.expect(6);
var actual, expected;

test.deepEqual(grunt.task.normalizeMultiTaskFiles('src/file1.js', 'dist/built.js'), [
{dest: 'dist/built.js', src: ['src/file1.js']}
], 'should normalize destTarget: srcString.');
actual = grunt.task.normalizeMultiTaskFiles('src/*1.js', 'dist/built.js');
expected = [
{dest: 'dist/built.js', _src: ['src/*1.js'], src: ['src/file1.js']}
];
test.deepEqual(actual, expected, 'should normalize destTarget: srcString.');

test.deepEqual(grunt.task.normalizeMultiTaskFiles([['src/file1.js'], ['src/file2.js']], 'dist/built.js'), [
{dest: 'dist/built.js', src: ['src/file1.js', 'src/file2.js']}
], 'should normalize destTarget: srcArray.');
actual = grunt.task.normalizeMultiTaskFiles([['src/*1.js'], ['src/*2.js']], 'dist/built.js');
expected = [
{dest: 'dist/built.js', _src: ['src/*1.js', 'src/*2.js'], src: ['src/file1.js', 'src/file2.js']}
];
test.deepEqual(actual, expected, 'should normalize destTarget: srcArray.');

test.deepEqual(grunt.task.normalizeMultiTaskFiles({
src: ['src/file1.js', 'src/file2.js'],
actual = grunt.task.normalizeMultiTaskFiles({
src: ['src/*1.js', 'src/*2.js'],
dest: 'dist/built.js'
}, 'target'), [
{dest: 'dist/built.js', src: ['src/file1.js', 'src/file2.js']}
], 'should normalize target: {src: srcStuff, dest: destStuff}.');
}, 'target');
expected = [
{dest: 'dist/built.js', _src: ['src/*1.js', 'src/*2.js'], src: ['src/file1.js', 'src/file2.js']}
];
test.deepEqual(actual, expected, 'should normalize target: {src: srcStuff, dest: destStuff}.');

test.deepEqual(grunt.task.normalizeMultiTaskFiles({
actual = grunt.task.normalizeMultiTaskFiles({
files: {
'dist/built-a.js': 'src/file1.js',
'dist/built-b.js': ['src/file1.js', [['src/file2.js']]]
'dist/built-a.js': 'src/*1.js',
'dist/built-b.js': ['src/*1.js', [['src/*2.js']]]
}
}, 'target'), [
{dest: 'dist/built-a.js', src: ['src/file1.js']},
{dest: 'dist/built-b.js', src: ['src/file1.js', 'src/file2.js']}
], 'should normalize target: {files: {destTarget: srcStuff, ...}}.');
}, 'target');
expected = [
{dest: 'dist/built-a.js', _src: ['src/*1.js'], src: ['src/file1.js']},
{dest: 'dist/built-b.js', _src: ['src/*1.js', 'src/*2.js'], src: ['src/file1.js', 'src/file2.js']}
];
test.deepEqual(actual, expected, 'should normalize target: {files: {destTarget: srcStuff, ...}}.');

test.deepEqual(grunt.task.normalizeMultiTaskFiles({
actual = grunt.task.normalizeMultiTaskFiles({
files: [
{'dist/built-a.js': 'src/file1.js'},
{'dist/built-b.js': [[['src/file1.js'], 'src/file2.js']]}
{'dist/built-a.js': 'src/*.whoops'},
{'dist/built-b.js': [[['src/*1.js'], 'src/*2.js']]}
]
}, 'target'), [
{dest: 'dist/built-a.js', src: ['src/file1.js']},
{dest: 'dist/built-b.js', src: ['src/file1.js', 'src/file2.js']}
], 'should normalize target: {files: [{destTarget: srcStuff}, ...]}.');
}, 'target');
expected = [
{dest: 'dist/built-a.js', _src: ['src/*.whoops'], src: []},
{dest: 'dist/built-b.js', _src: ['src/*1.js', 'src/*2.js'], src: ['src/file1.js', 'src/file2.js']}
];
test.deepEqual(actual, expected, 'should normalize target: {files: [{destTarget: srcStuff}, ...]}.');

test.deepEqual(grunt.task.normalizeMultiTaskFiles({
actual = grunt.task.normalizeMultiTaskFiles({
files: [
{dest: 'dist/built-a.js', src: 'src/file1.js'},
{dest: 'dist/built-b.js', src: ['src/file1.js', 'src/file2.js']}
{dest: 'dist/built-a.js', src: 'src/*2.js'},
{dest: 'dist/built-b.js', src: ['src/*1.js', 'src/*2.js']}
]
}, 'target'), [
{dest: 'dist/built-a.js', src: ['src/file1.js']},
{dest: 'dist/built-b.js', src: ['src/file1.js', 'src/file2.js']}
], 'should normalize target: {files: [{src: srcStuff, dest: destStuff}, ...]}.');
}, 'target');
expected = [
{dest: 'dist/built-a.js', _src: ['src/*2.js'], src: ['src/file2.js']},
{dest: 'dist/built-b.js', _src: ['src/*1.js', 'src/*2.js'], src: ['src/file1.js', 'src/file2.js']}
];
test.deepEqual(actual, expected, 'should normalize target: {files: [{src: srcStuff, dest: destStuff}, ...]}.');

test.done();
},
Expand All @@ -72,15 +94,17 @@ exports['task.normalizeMultiTaskFiles'] = {
PROP3: '3'
});

test.deepEqual(grunt.task.normalizeMultiTaskFiles({
var actual = grunt.task.normalizeMultiTaskFiles({
files: [
{dest: 'dist/built-<%= TEST %>-a.js', src: 'src/file1-<%= TEST %>.js'},
{dest: 'dist/built-<%= TEST %>-b.js', src: ['src/file1-<%= TEST %>.js', 'src/file2-<%= TEST %>.js']}
{dest: 'dist/built-<%= TEST %>-a.js', src: 'src/file?-<%= TEST %>.js'},
{dest: 'dist/built-<%= TEST %>-b.js', src: ['src/*1-<%= TEST %>.js', 'src/*2-<%= TEST %>.js']}
]
}, 'target'), [
{dest: 'dist/built-123-a.js', src: ['src/file1-123.js']},
{dest: 'dist/built-123-b.js', src: ['src/file1-123.js', 'src/file2-123.js']}
], 'should process templates recursively.');
}, 'target');
var expected = [
{dest: 'dist/built-123-a.js', _src: ['src/file?-123.js'], src: ['src/file1-123.js', 'src/file2-123.js']},
{dest: 'dist/built-123-b.js', _src: ['src/*1-123.js', 'src/*2-123.js'], src: ['src/file1-123.js', 'src/file2-123.js']}
];
test.deepEqual(actual, expected, 'should process templates recursively.');

test.done();
}
Expand Down
99 changes: 70 additions & 29 deletions test/gruntfile/multi-task-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@
'use strict';

module.exports = function(grunt) {
grunt.file.setBase('../fixtures/files');

grunt.initConfig({
version: '1.0.0',
build: '123',
run: {
options: {a: 1, b: 11},
// This is the "compact" format, where the target name is actually the
// dest filename. Doesn't support per-target options, templated dest, or
// >1 srcs-dest grouping.
'dist/built.js': 'src/file.js',
'dist/built1.js': ['src/file.js', 'src/file_1a.js'],
'dist/built.js': 'src/*1.js',
'dist/built1.js': ['src/*1.js', 'src/*2.js'],
// This is the "medium" format. The target name is arbitrary and can be
// used like "grunt run:built". Supports per-target options, templated
// dest, and arbitrary "extra" paramters. Doesn't support >1 srcs-dest
// grouping.
built: {
options: {a: 2, c: 22},
src: ['src/file.js', 'src/file_2a.js'],
dest: 'dist/built-<%= version %>.js',
src: ['src/*1.js', 'src/*2.js'],
dest: 'dist/built-<%= build %>.js',
extra: 123,
},
// This is the "full" format. The target name is arbitrary and can be
Expand All @@ -38,23 +40,23 @@ module.exports = function(grunt) {
long1: {
options: {a: 3, c: 33},
files: {
'dist/built-<%= version %>-3a.js': ['src/file.js', 'src/file_3a.js'],
'dist/built-<%= version %>-3b.js': ['src/file.js', 'src/file_3b.js'],
'dist/built-<%= build %>-a.js': ['src/*1.js'],
'dist/built-<%= build %>-b.js': ['src/*1.js', 'src/*2.js'],
}
},
long2: {
options: {a: 4, c: 44},
files: [
{'dist/built-<%= version %>-4a.js': ['src/file.js', 'src/file_4a.js']},
{'dist/built-<%= version %>-4b.js': ['src/file.js', 'src/file_4b.js']},
{'dist/built-<%= build %>-a.js': ['src/*.whoops']},
{'dist/built-<%= build %>-b.js': ['src/*1.js', 'src/*2.js']},
]
},
// This "full" variant supports per srcs-dest arbitrary "extra" paramters.
long3: {
options: {a: 5, c: 55},
files: [
{dest: 'dist/built-<%= version %>-5a.js', src: ['src/file.js', 'src/file_5a.js'], extra: 456},
{dest: 'dist/built-<%= version %>-5b.js', src: ['src/file.js', 'src/file_5b.js'], extra: 789},
{dest: 'dist/built-<%= build %>-a.js', src: ['src/*2.js'], extra: 456},
{dest: 'dist/built-<%= build %>-b.js', src: ['src/*1.js', 'src/*2.js'], extra: 789},
]
},
// Need to ensure the task function is run if no files or options were
Expand Down Expand Up @@ -95,49 +97,88 @@ module.exports = function(grunt) {
'run:dist/built.js': [
{
options: {a: 1, b: 11, d: 9},
file: {dest: 'dist/built.js', src: ['src/file.js']},
file: {
dest: 'dist/built.js',
_src: ['src/*1.js'],
src: ['src/file1.js'],
},
},
],
'run:dist/built1.js': [
{
options: {a: 1, b: 11, d: 9},
file: {dest: 'dist/built1.js', src: ['src/file.js', 'src/file_1a.js']},
file: {
dest: 'dist/built1.js',
_src: ['src/*1.js', 'src/*2.js'],
src: ['src/file1.js', 'src/file2.js'],
},
},
],
'run:built': [
{
options: {a: 2, b: 11, c: 22, d: 9},
file: {dest: 'dist/built-1.0.0.js', src: ['src/file.js', 'src/file_2a.js'], extra: 123},
file: {
dest: 'dist/built-123.js',
_src: ['src/*1.js', 'src/*2.js'],
src: ['src/file1.js', 'src/file2.js'],
extra: 123,
},
},
],
'run:long1': [
{
options: {a: 3, b: 11, c: 33, d: 9},
file: {dest: 'dist/built-1.0.0-3a.js', src: ['src/file.js', 'src/file_3a.js']},
file: {
dest: 'dist/built-123-a.js',
_src: ['src/*1.js'],
src: ['src/file1.js'],
},
},
{
options: {a: 3, b: 11, c: 33, d: 9},
file: {dest: 'dist/built-1.0.0-3b.js', src: ['src/file.js', 'src/file_3b.js']},
file: {
dest: 'dist/built-123-b.js',
_src: ['src/*1.js', 'src/*2.js'],
src: ['src/file1.js', 'src/file2.js'],
},
},
],
'run:long2': [
{
options: {a: 4, b: 11, c: 44, d: 9},
file: {dest: 'dist/built-1.0.0-4a.js', src: ['src/file.js', 'src/file_4a.js']},
file: {
dest: 'dist/built-123-a.js',
_src: ['src/*.whoops'],
src: [],
},
},
{
options: {a: 4, b: 11, c: 44, d: 9},
file: {dest: 'dist/built-1.0.0-4b.js', src: ['src/file.js', 'src/file_4b.js']},
file: {
dest: 'dist/built-123-b.js',
_src: ['src/*1.js', 'src/*2.js'],
src: ['src/file1.js', 'src/file2.js'],
},
},
],
'run:long3': [
{
options: {a: 5, b: 11, c: 55, d: 9},
file: {dest: 'dist/built-1.0.0-5a.js', src: ['src/file.js', 'src/file_5a.js'], extra: 456},
file: {
dest: 'dist/built-123-a.js',
_src: ['src/*2.js'],
src: ['src/file2.js'],
extra: 456,
},
},
{
options: {a: 5, b: 11, c: 55, d: 9},
file: {dest: 'dist/built-1.0.0-5b.js', src: ['src/file.js', 'src/file_5b.js'], extra: 789},
file: {
dest: 'dist/built-123-b.js',
_src: ['src/*1.js', 'src/*2.js'],
src: ['src/file1.js', 'src/file2.js'],
extra: 789,
},
},
],
'run:no_files_or_options': [
Expand Down Expand Up @@ -187,8 +228,8 @@ module.exports = function(grunt) {
});

grunt.registerTask('default', [
'run',
'test:all',
// 'run',
// 'test:all',
'run:no_files_or_options',
'test:no_files_or_options',
'run:dist/built.js',
Expand All @@ -197,13 +238,13 @@ module.exports = function(grunt) {
'test:dist/built1.js',
'run:built',
'test:built',
'run:long1',
'test:long1',
'run:long2',
'test:long2',
'run:long3',
'test:long3',
'test:counters',
// 'run:long1',
// 'test:long1',
// 'run:long2',
// 'test:long2',
// 'run:long3',
// 'test:long3',
// 'test:counters',
]);

};

0 comments on commit e394ca8

Please sign in to comment.