Skip to content
Browse files

initial port to seperate repo.

  • Loading branch information...
0 parents commit 47e5c0b43f87a3ad80ecc316b376233bcadc3170 @ctalkington ctalkington committed Sep 7, 2012
3 .gitignore
@@ -0,0 +1,3 @@
+node_modules
+npm-debug.log
+tmp
4 AUTHORS
@@ -0,0 +1,4 @@
+Chris Talkington (http://christalkington.com/)
+Tyler Kellen (http://goingslowly.com/)
+Kyle Robinson Young (http://twitter.com/shamakry)
+Nathan Bleigh (http://www.nathanbleigh.com)
22 LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Chris Talkington, contributors.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
74 README.md
@@ -0,0 +1,74 @@
+# grunt-contrib-copy
+> Copy files and folders (part of the [grunt-contrib](https://github.com/gruntjs/grunt-contrib) collection). Submitted by [Chris Talkington](https://github.com/ctalkington).
+
+### 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`.
+
+### Configuration
+
+Inside your `grunt.js` file add a section named `copy`. This section specifies the files to copy.
+
+#### Parameters
+
+##### files ```object```
+
+This defines what files this task will copy and should contain key:value pairs.
+
+The key (destination) should be an unique path (supports [grunt.template](https://github.com/cowboy/grunt/blob/master/docs/api_template.md)) and the value (source) should be a filepath or an array of filepaths (supports [minimatch](https://github.com/isaacs/minimatch)).
+
+##### options ```object```
+
+This controls how this task operates and should contain key:value pairs, see options below.
+
+#### Options
+
+##### basePath ```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.
+
+##### flatten ```boolean```
+
+This option performs a flat copy that dumps all the files into the root of the destination directory, overwriting files if they exist.
+
+##### processName ```function```
+
+This option accepts a function that adjusts the filename of the copied file. Function is passed filename and should return a string.
+
+``` javascript
+options: {
+ processName: function(filename) {
+ if (filename == "test.jpg") {
+ filename = "newname.jpg";
+ }
+ return filename;
+ }
+}
+```
+
+##### processContent ```function```
+
+This option is passed to `grunt.file.copy` as an advanced way to control the file contents that are copied.
+
+##### processContentExclude ```string```
+
+This option is passed to `grunt.file.copy` as an advanced way to control which file contents are processed.
+
+##### minimatch ```object```
+
+These options will be forwarded on to expandFiles, as referenced in the [minimatch options section](https://github.com/isaacs/minimatch/#options)
+
+#### Config Example
+
+``` javascript
+copy: {
+ dist: {
+ files: {
+ "path/to/directory": "path/to/source/*", // includes files in dir
+ "path/to/directory": "path/to/source/**", // includes files in dir and subdirs
+ "path/to/project-<%= pkg.version %>": "path/to/source/**", // variables in destination
+ "path/to/directory": ["path/to/sources/*.js", "path/to/more/*.js"] // include JS files in two diff dirs
+ }
+ }
+}
+```
95 grunt.js
@@ -0,0 +1,95 @@
+/*
+ * grunt-contrib-copy
+ * http://gruntjs.com/
+ *
+ * Copyright (c) 2012 Chris Talkington, contributors
+ * Licensed under the MIT license.
+ * https://github.com/gruntjs/grunt-contrib-copy/blob/master/LICENSE-MIT
+ */
+
+module.exports = function(grunt) {
+ 'use strict';
+
+ // Project configuration.
+ grunt.initConfig({
+ test_vars: {
+ name: 'grunt-contrib-copy',
+ version: '0.1.0'
+ },
+
+ lint: {
+ all: ['grunt.js', 'tasks/*.js', '<config:nodeunit.tasks>']
+ },
+
+ jshint: {
+ options: {
+ curly: true,
+ eqeqeq: true,
+ immed: true,
+ latedef: true,
+ newcap: true,
+ noarg: true,
+ sub: true,
+ undef: true,
+ boss: true,
+ eqnull: true,
+ node: true,
+ es5: true
+ }
+ },
+
+ // Before generating any new files, remove any previously-created files.
+ clean: {
+ test: ['tmp']
+ },
+
+ // Configuration to be run (and then tested).
+ copy: {
+ test: {
+ files: {
+ 'tmp/copy_test_files': ['test/fixtures/*'],
+ 'tmp/copy_test_v<%= test_vars.version %>': ['test/fixtures/**']
+ }
+ },
+
+ flatten: {
+ options: {
+ flatten: true
+ },
+ files: {
+ 'tmp/copy_test_flatten': ['test/fixtures/**']
+ }
+ },
+
+ minimatch: {
+ options: {
+ minimatch: {
+ dot: true
+ }
+ },
+ files: {
+ 'tmp/copy_minimatch': ['test/fixtures/*']
+ }
+ }
+ },
+
+ // Unit tests.
+ nodeunit: {
+ tasks: ['test/*_test.js']
+ }
+ });
+
+ // Actually load this plugin's task(s).
+ grunt.loadTasks('tasks');
+
+ // The clean plugin helps in testing.
+ grunt.loadNpmTasks('grunt-contrib-clean');
+
+ // Whenever the 'test' task is run, first clean the 'tmp' dir, then run this
+ // plugin's task(s), then test the result.
+ grunt.renameTask('test', 'nodeunit');
+ grunt.registerTask('test', 'clean copy nodeunit');
+
+ // By default, lint and run all tests.
+ grunt.registerTask('default', 'lint test');
+};
40 package.json
@@ -0,0 +1,40 @@
+{
+ "name": "grunt-contrib-copy",
+ "description": "Copy files and folders.",
+ "version": "0.2.0",
+ "homepage": "https://github.com/gruntjs/grunt-contrib-copy",
+ "author": {
+ "name": "Chris Talkington",
+ "url": "http://christalkington.com/"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/gruntjs/grunt-contrib-copy.git"
+ },
+ "bugs": {
+ "url": "https://github.com/gruntjs/grunt-contrib-copy/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/gruntjs/grunt-contrib-copy/blob/master/LICENSE-MIT"
+ }
+ ],
+ "main": "grunt.js",
+ "engines": {
+ "node": "*"
+ },
+ "scripts": {
+ "test": "grunt test"
+ },
+ "dependencies": {
+ "grunt-contrib-lib": "~0.1.0",
+ "grunt-contrib-clean": "~0.1.0"
+ },
+ "devDependencies": {
+ "grunt": "~0.3.15"
+ },
+ "keywords": [
+ "gruntplugin"
+ ]
+}
105 tasks/copy.js
@@ -0,0 +1,105 @@
+/*
+ * grunt-contrib-copy
+ * http://gruntjs.com/
+ *
+ * Copyright (c) 2012 Chris Talkington, contributors
+ * Licensed under the MIT license.
+ * https://github.com/gruntjs/grunt-contrib-copy/blob/master/LICENSE-MIT
+ */
+
+module.exports = function(grunt) {
+ 'use strict';
+
+ var path = require('path');
+
+ // TODO: ditch this when grunt v0.4 is released
+ grunt.util = grunt.util || grunt.utils;
+
+ var _ = grunt.util._;
+ var kindOf = grunt.util.kindOf;
+ var helpers = require('grunt-contrib-lib').init(grunt);
+
+ grunt.registerMultiTask('copy', 'Copy files.', function() {
+ var options = helpers.options(this, {
+ basePath: false,
+ flatten: false,
+ processName: false,
+ processContent: false,
+ processContentExclude: [],
+ minimatch: {}
+ });
+
+ // TODO: ditch this when grunt v0.4 is released
+ this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);
+
+ var copyOptions = {
+ process: options.processContent,
+ noProcess: options.processContentExclude
+ };
+
+ if (options.basePath) {
+ options.basePath = path.normalize(options.basePath);
+ options.basePath = _(options.basePath).trim(path.sep);
+ }
+
+ grunt.verbose.writeflags(options, 'Options');
+
+ var srcFiles;
+
+ var basePath;
+ var filename;
+ var relative;
+ var destFile;
+
+ this.files.forEach(function(file) {
+ file.dest = path.normalize(file.dest);
+ srcFiles = grunt.file.expandFiles(options.minimatch, file.src);
+
+ basePath = options.basePath || findBasePath(srcFiles);
+
+ grunt.verbose.writeln('Base Path: ' + basePath.cyan);
+ grunt.verbose.or.write('Copying file(s)' + ' to ' + file.dest + '...');
+
+ srcFiles.forEach(function(srcFile) {
+ srcFile = path.normalize(srcFile);
+ filename = path.basename(srcFile);
+ relative = path.dirname(srcFile);
+
+ if (options.flatten) {
+ relative = '';
+ } else if (basePath && basePath.length > 1) {
+ relative = _(relative).chain().strRight(basePath).trim(path.sep).value();
+ }
+
+ if (options.processName && kindOf(options.processName) === 'function') {
+ filename = options.processName(filename);
+ }
+
+ // make paths outside grunts working dir relative
+ relative = relative.replace(/\.\.(\/|\\)/g, '');
+
+ destFile = path.join(file.dest, relative, filename);
+
+ grunt.file.copy(srcFile, destFile, copyOptions);
+ });
+
+ grunt.verbose.or.ok();
+ });
+ });
+
+ var findBasePath = function(srcFiles) {
+ var basePaths = [];
+ var dirName;
+
+ srcFiles.forEach(function(srcFile) {
+ dirName = path.dirname(srcFile);
+ dirName = path.normalize(dirName);
+
+ basePaths.push(dirName.split(path.sep));
+ });
+
+ basePaths = _.intersection.apply([], basePaths);
+
+ return path.join.apply(path, basePaths);
+ };
+};
30 test/copy_test.js
@@ -0,0 +1,30 @@
+var grunt = require('grunt');
+var fs = require('fs');
+
+exports['copy'] = {
+ main: function(test) {
+ 'use strict';
+
+ var expect, result;
+
+ test.expect(4);
+
+ expect = ['test.css', 'test.js'].sort();
+ result = fs.readdirSync('tmp/copy_test_files').sort();
+ test.deepEqual(expect, result, 'should copy several files');
+
+ expect = ['folder_one', 'folder_two', 'test.css', 'test.js'].sort();
+ result = fs.readdirSync('tmp/copy_test_v0.1.0').sort();
+ test.deepEqual(expect, result, 'should copy several folders and files (with template support)');
+
+ expect = ['one.css', 'one.js', 'test.css', 'test.js', 'two.css', 'two.js'].sort();
+ result = fs.readdirSync('tmp/copy_test_flatten').sort();
+ test.deepEqual(expect, result, 'should create a flat structure');
+
+ expect = ['.hidden', 'test.css', 'test.js'].sort();
+ result = fs.readdirSync('tmp/copy_minimatch').sort();
+ test.deepEqual(expect, result, 'should allow for minimatch dot option');
+
+ test.done();
+ }
+};
1 test/fixtures/.hidden
@@ -0,0 +1 @@
+#This is a hidden file!!!
1 test/fixtures/folder_one/one.css
@@ -0,0 +1 @@
+body,td{}a{}a:hover{}
1 test/fixtures/folder_one/one.js
@@ -0,0 +1 @@
+$(document).ready(function(){$.noConflict();});
1 test/fixtures/folder_two/two.css
@@ -0,0 +1 @@
+body,td{color:blue;}a{}a:hover{}
1 test/fixtures/folder_two/two.js
@@ -0,0 +1 @@
+$(document).ready(function(){jQuery});
1 test/fixtures/test.css
@@ -0,0 +1 @@
+body,td{background:green;}a{}a:hover{}
1 test/fixtures/test.js
@@ -0,0 +1 @@
+$(document).ready(function(){});

0 comments on commit 47e5c0b

Please sign in to comment.
Something went wrong with that request. Please try again.