[484] | 1 | /* |
---|
| 2 | * grunt-contrib-copy |
---|
| 3 | * http://gruntjs.com/ |
---|
| 4 | * |
---|
| 5 | * Copyright (c) 2012 Chris Talkington, contributors |
---|
| 6 | * Licensed under the MIT license. |
---|
| 7 | * https://github.com/gruntjs/grunt-contrib-copy/blob/master/LICENSE-MIT |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | module.exports = function(grunt) { |
---|
| 11 | 'use strict'; |
---|
| 12 | |
---|
| 13 | var path = require('path'); |
---|
| 14 | |
---|
| 15 | grunt.registerMultiTask('copy', 'Copy files.', function() { |
---|
| 16 | var kindOf = grunt.util.kindOf; |
---|
| 17 | |
---|
| 18 | var options = this.options({ |
---|
| 19 | processContent: false, |
---|
| 20 | processContentExclude: [] |
---|
| 21 | }); |
---|
| 22 | |
---|
| 23 | var copyOptions = { |
---|
| 24 | process: options.processContent, |
---|
| 25 | noProcess: options.processContentExclude |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | grunt.verbose.writeflags(options, 'Options'); |
---|
| 29 | |
---|
| 30 | var dest; |
---|
| 31 | var isExpandedPair; |
---|
| 32 | var tally = { |
---|
| 33 | dirs: 0, |
---|
| 34 | files: 0 |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | this.files.forEach(function(filePair) { |
---|
| 38 | isExpandedPair = filePair.orig.expand || false; |
---|
| 39 | |
---|
| 40 | filePair.src.forEach(function(src) { |
---|
| 41 | if (detectDestType(filePair.dest) === 'directory') { |
---|
| 42 | dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src)); |
---|
| 43 | } else { |
---|
| 44 | dest = filePair.dest; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | if (grunt.file.isDir(src)) { |
---|
| 48 | grunt.verbose.writeln('Creating ' + dest.cyan); |
---|
| 49 | grunt.file.mkdir(dest); |
---|
| 50 | tally.dirs++; |
---|
| 51 | } else { |
---|
| 52 | grunt.verbose.writeln('Copying ' + src.cyan + ' -> ' + dest.cyan); |
---|
| 53 | grunt.file.copy(src, dest, copyOptions); |
---|
| 54 | tally.files++; |
---|
| 55 | } |
---|
| 56 | }); |
---|
| 57 | }); |
---|
| 58 | |
---|
| 59 | if (tally.dirs) { |
---|
| 60 | grunt.log.write('Created ' + tally.dirs.toString().cyan + ' directories'); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if (tally.files) { |
---|
| 64 | grunt.log.write((tally.dirs ? ', copied ' : 'Copied ') + tally.files.toString().cyan + ' files'); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | grunt.log.writeln(); |
---|
| 68 | }); |
---|
| 69 | |
---|
| 70 | var detectDestType = function(dest) { |
---|
| 71 | if (grunt.util._.endsWith(dest, '/')) { |
---|
| 72 | return 'directory'; |
---|
| 73 | } else { |
---|
| 74 | return 'file'; |
---|
| 75 | } |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | var unixifyPath = function(filepath) { |
---|
| 79 | if (process.platform === 'win32') { |
---|
| 80 | return filepath.replace(/\\/g, '/'); |
---|
| 81 | } else { |
---|
| 82 | return filepath; |
---|
| 83 | } |
---|
| 84 | }; |
---|
| 85 | }; |
---|