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

Added ability to copy symlinked directories #1

Merged
merged 3 commits into from
Feb 9, 2014
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
9 changes: 9 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ module.exports = function(grunt) {
{expand: true, cwd: 'test/fixtures', src: ['*.js'], dest: 'tmp/copy_test_symlink'}
]
},

dirlink: {
options: {
copySymlinkAsSymlink: true,
},
files: [
{expand: true, cwd: 'test/fixtures/dirlink', src: ['**/*'], dest: 'tmp/copy_test_dirlink'}
]
},
},

// Unit tests.
Expand Down
26 changes: 22 additions & 4 deletions tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = function(grunt) {
var srcStat;
var fileMode;
var isLink;
var copiedDirLinks = [];
var tally = {
dirs: 0,
files: 0
Expand All @@ -46,21 +47,38 @@ module.exports = function(grunt) {
isExpandedPair = filePair.orig.expand || false;

filePair.src.forEach(function(src) {
var skip = false;
if (copiedDirLinks.length) {
for (var i in copiedDirLinks) {
if (copiedDirLinks[i].test(src)) {
skip = true;
break;
}
}
}
if (skip) {
return;
}

if (detectDestType(filePair.dest) === 'directory') {
dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src));
} else {
dest = filePair.dest;
}

isLink = false;
srcStat = fs.lstatSync(src);
isLink = srcStat.isSymbolicLink();
if (grunt.file.isDir(src)) {
grunt.verbose.writeln('Creating ' + chalk.cyan(dest));
grunt.file.mkdir(dest);
if (options.copySymlinkAsSymlink && process.platform !== 'win32' && isLink) {
fs.symlinkSync(fs.readlinkSync(src), dest);
copiedDirLinks.push(new RegExp('^' + src.replace(/\/*$/,'/').replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")));
} else {
grunt.file.mkdir(dest);
}
tally.dirs++;
} else {
grunt.verbose.writeln('Copying ' + chalk.cyan(src) + ' -> ' + chalk.cyan(dest));
srcStat = fs.lstatSync(src);
isLink = srcStat.isSymbolicLink();
if (options.copySymlinkAsSymlink && process.platform !== 'win32' && isLink) {
grunt.file.mkdir(path.dirname(dest));
if (grunt.file.exists(dest)) {
Expand Down
16 changes: 16 additions & 0 deletions test/copy_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,20 @@ exports.copy = {

test.done();
},

dirlink: function(test) {
'use strict';

test.expect(1);

if (process.platform === 'win32') {
test.ok(true);
} else {
var actual = fs.readdirSync('tmp/copy_test_dirlink').sort();
var expected = fs.readdirSync('test/expected/copy_test_dirlink').sort();
test.deepEqual(expected, actual, 'should copy mix of directories, files and symlinks');
}

test.done();
},
};
1 change: 1 addition & 0 deletions test/expected/copy_test_dirlink/dir/file-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_dirlink/dir/file-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_dirlink/dir/link-b.js
1 change: 1 addition & 0 deletions test/expected/copy_test_dirlink/link
1 change: 1 addition & 0 deletions test/expected/copy_test_flatten/file-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_flatten/file-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_flatten/link-b.js
1 change: 1 addition & 0 deletions test/expected/copy_test_mix/dirlink/dir/file-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_mix/dirlink/dir/file-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/expected/copy_test_mix/dirlink/link
1 change: 1 addition & 0 deletions test/fixtures/dirlink/dir/file-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/fixtures/dirlink/dir/file-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$(document).ready(function(){$.noConflict();});
1 change: 1 addition & 0 deletions test/fixtures/dirlink/dir/link-b.js
1 change: 1 addition & 0 deletions test/fixtures/dirlink/link