Skip to content

Commit

Permalink
replace basePath in favor of cwd.
Browse files Browse the repository at this point in the history
also tweak for latest grunt v0.4 changes.
  • Loading branch information
ctalkington committed Nov 21, 2012
1 parent 07fecc7 commit c07faa8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ v0.4.0:
data: 2012-11-16
changes:
- Conversion to grunt v0.4 conventions.
- Replace basePath with cwd which is much smarter and understandable.
v0.3.2:
date: 2012-10-18
changes:
Expand Down
12 changes: 8 additions & 4 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ module.exports = function(grunt) {

// Configuration to be run (and then tested).
copy: {
test: {
main: {
options: {
cwd: 'test/fixtures'
},
files: {
'tmp/copy_test_files/': ['test/fixtures/*'],
'tmp/copy_test_v<%= test_vars.version %>/': ['test/fixtures/**']
'tmp/copy_test_files/': ['*'],
'tmp/copy_test_v<%= test_vars.version %>/': ['**']
}
},

Expand All @@ -52,12 +55,13 @@ module.exports = function(grunt) {

minimatch: {
options: {
cwd: 'test/fixtures',
minimatch: {
dot: true
}
},
files: {
'tmp/copy_minimatch/': ['test/fixtures/*']
'tmp/copy_minimatch/': ['*']
}
},

Expand Down
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ grunt.initConfig({
})
```

##### Recent Confusion

Many expect this task to work like `cp` on *nix systems but it was designed to use grunt conventions including the use of minimatch regex. We are working hard to make this and other tasks suitable for advanced users but there are no current plans to emulate `cp`.
### Options

#### files
Expand All @@ -59,10 +56,27 @@ The key (destination) should be an unique path (supports [grunt.template](https:

As of v0.3.0, when copying to a directory you must add a trailing slash to the destination due to added support of single file copy.

#### options.basePath
#### options.cwd
Type: `String`

This option adjusts the folder structure when copied to the destination directory. When not explicitly set, best effort is made to locate the basePath by comparing all source filepaths left to right for a common pattern.
This option sets the current working directory for use with the minimatch and copy process. This helps translate paths when copied so that the destination stucture matches the source structure exactly. Without a `cwd` set, all paths are relative to the gruntfile directory which can cause extra depth to be added to your copied structure when it may not be desired.

```js
copy: {
target: {
options: {
cwd: 'path/to/sources'
},
files: {
'tmp/test/': ['*', 'sub1/*']
}
}
}
```

#### options.basePath

As of v0.4, this option has been removed in favor of `cwd` which fits the copy process so much better.

#### options.flatten
Type: `Boolean`
Expand Down Expand Up @@ -118,7 +132,7 @@ copy: {

## Release History

* 2012-11-16 - v0.4.0 - Conversion to grunt v0.4 conventions.
* 2012-11-21 - v0.4.0 - Conversion to grunt v0.4 conventions. Replace basePath with cwd which is much smarter and understandable.
* 2012-10-17 - v0.3.2 - Pass copyOptions on single file copy
* 2012-10-11 - v0.3.1 - Rename grunt-contrib-lib dep to grunt-lib-contrib.
* 2012-09-23 - v0.3.0 - General cleanup and consolidation. Global options depreciated.
Expand All @@ -130,4 +144,4 @@ copy: {
--
Task submitted by <a href="http://christalkington.com/">Chris Talkington</a>.

*Generated on Fri Nov 16 2012 13:43:20.*
*Generated on Wed Nov 21 2012 00:51:53.*
21 changes: 19 additions & 2 deletions docs/copy-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ The key (destination) should be an unique path (supports [grunt.template](https:

As of v0.3.0, when copying to a directory you must add a trailing slash to the destination due to added support of single file copy.

## options.basePath
## options.cwd
Type: `String`

This option adjusts the folder structure when copied to the destination directory. When not explicitly set, best effort is made to locate the basePath by comparing all source filepaths left to right for a common pattern.
This option sets the current working directory for use with the minimatch and copy process. This helps translate paths when copied so that the destination stucture matches the source structure exactly. Without a `cwd` set, all paths are relative to the gruntfile directory which can cause extra depth to be added to your copied structure when it may not be desired.

```js
copy: {
target: {
options: {
cwd: 'path/to/sources'
},
files: {
'tmp/test/': ['*', 'sub1/*']
}
}
}
```

## options.basePath

As of v0.4, this option has been removed in favor of `cwd` which fits the copy process so much better.

## options.flatten
Type: `Boolean`
Expand Down
4 changes: 0 additions & 4 deletions docs/copy-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ grunt.initConfig({
},
})
```

### Recent Confusion

Many expect this task to work like `cp` on *nix systems but it was designed to use grunt conventions including the use of minimatch regex. We are working hard to make this and other tasks suitable for advanced users but there are no current plans to emulate `cp`.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test": "grunt test"
},
"dependencies": {
"grunt-lib-contrib": "~0.3.0"
"grunt-lib-contrib": "~0.4.0"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.0",
Expand Down
45 changes: 22 additions & 23 deletions tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(grunt) {
var helpers = require('grunt-lib-contrib').init(grunt);

var options = helpers.options(this, {
basePath: false,
cwd: '',
flatten: false,
processName: false,
processContent: false,
Expand All @@ -30,20 +30,26 @@ module.exports = function(grunt) {
noProcess: options.processContentExclude
};

if (options.cwd.length > 0) {
options.minimatch.cwd = options.cwd;
}

grunt.verbose.writeflags(options, 'Options');

var dest = path.normalize(this.file.dest);
var srcFiles = grunt.file.expandFiles(options.minimatch, this.file.src);
var srcFiles = grunt.file.expandFiles(options.minimatch, this.file.srcRaw);

if (srcFiles.length === 0) {
grunt.fail.warn('Unable to copy; no valid source files were found.');
}

var srcFile;

var destType = detectDestType(dest);

if (destType === 'file') {
if (srcFiles.length === 1) {
var srcFile = path.normalize(srcFiles[0]);
srcFile = path.join(options.cwd, srcFiles[0]);

grunt.verbose.or.write('Copying file' + ' to ' + dest.cyan + '...');
grunt.file.copy(srcFile, dest, copyOptions);
Expand All @@ -53,35 +59,28 @@ module.exports = function(grunt) {
grunt.fail.warn('Unable to copy multiple files to the same destination filename, did you forget a trailing slash?');
}
} else if (destType === 'directory') {
var basePath = helpers.findBasePath(srcFiles, options.basePath);

grunt.verbose.writeln('Base Path: ' + basePath.cyan);
grunt.verbose.or.write('Copying files' + ' to ' + dest.cyan + '...');

var destFile;
var filename;
var relative;

srcFiles.forEach(function(srcFile) {
srcFile = path.normalize(srcFile);
filename = path.basename(srcFile);
relative = path.dirname(srcFile);
var fileName;
var filePath;

if (options.flatten) {
relative = '';
} else if (basePath && basePath.length >= 1) {
relative = grunt.util._(relative).strRight(basePath);
relative = grunt.util._(relative).trim(path.sep);
}
srcFiles.forEach(function(file) {
fileName = path.basename(file);
filePath = path.dirname(file);

srcFile = path.join(options.cwd, file);

if (options.processName && kindOf(options.processName) === 'function') {
filename = options.processName(filename);
fileName = options.processName(fileName) || fileName;
}

// make paths outside grunts working dir relative
relative = relative.replace(/\.\.(\/|\\)/g, '');

destFile = path.join(dest, relative, filename);
if (options.flatten) {
destFile = path.join(dest, fileName);
} else {
destFile = path.join(dest, filePath, fileName);
}

grunt.file.copy(srcFile, destFile, copyOptions);
});
Expand Down
6 changes: 3 additions & 3 deletions test/copy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ var grunt = require('grunt');
var fs = require('fs');

exports.copy = {
test: function(test) {
main: function(test) {
'use strict';

test.expect(2);

var actual = fs.readdirSync('tmp/copy_test_files').sort();
var expected = fs.readdirSync('test/expected/copy_test_files').sort();
test.deepEqual(expected, actual, 'should copy several files');
test.deepEqual(expected, actual, 'should copy several files (with cwd support)');

actual = fs.readdirSync('tmp/copy_test_v0.1.0').sort();
expected = fs.readdirSync('test/expected/copy_test_v0.1.0').sort();
test.deepEqual(expected, actual, 'should copy several folders and files (with template support)');
test.deepEqual(expected, actual, 'should copy several folders and files (with template and cwd support)');

test.done();
},
Expand Down

0 comments on commit c07faa8

Please sign in to comment.