[474] | 1 | var _ = require('underscore') |
---|
| 2 | , path = require('path'); |
---|
| 3 | |
---|
| 4 | var regexp = "(\n?)### begin grunt-svn-ignore(.|\n)*### end grunt-svn-ignore[^\n]*(\n?)"; |
---|
| 5 | |
---|
| 6 | module.exports = function(grunt) { |
---|
| 7 | |
---|
| 8 | grunt.registerMultiTask('svn-ignore', |
---|
| 9 | "Makes sure the specified source files are ignored in Subversion.", |
---|
| 10 | function(){ |
---|
| 11 | var done = this.async(); |
---|
| 12 | |
---|
| 13 | var tasks = _.chain(this.filesSrc) |
---|
| 14 | .groupBy(path.dirname) |
---|
| 15 | .map(function(files, dir) { |
---|
| 16 | return _.partial(updateSvnIgnore, |
---|
| 17 | dir, |
---|
| 18 | _.chain(files) |
---|
| 19 | .map(path.basename) |
---|
| 20 | .sortBy(_.identity) |
---|
| 21 | .value()); |
---|
| 22 | }) |
---|
| 23 | .value(); |
---|
| 24 | |
---|
| 25 | grunt.util.async.series(tasks,function(error, results){ |
---|
| 26 | done(error); |
---|
| 27 | }); |
---|
| 28 | }); |
---|
| 29 | |
---|
| 30 | grunt.registerMultiTask('svn-ignore-clean', |
---|
| 31 | "Clean generated ignore sections in Subversion.", |
---|
| 32 | function(){ |
---|
| 33 | var done = this.async(); |
---|
| 34 | |
---|
| 35 | var tasks = _.chain(this.filesSrc) |
---|
| 36 | .map(function(file) { |
---|
| 37 | return _.partial(clearSvnIgnore, file); |
---|
| 38 | }) |
---|
| 39 | .value(); |
---|
| 40 | |
---|
| 41 | grunt.util.async.series(tasks,function(error, results){ |
---|
| 42 | done(error); |
---|
| 43 | }); |
---|
| 44 | }); |
---|
| 45 | |
---|
| 46 | function updateSvnIgnore(dir,files,callback) { |
---|
| 47 | if ( !grunt.file.exists(dir) || !grunt.file.isDir(dir) ) { |
---|
| 48 | callback(null); |
---|
| 49 | return; |
---|
| 50 | } |
---|
| 51 | grunt.log.writeln("Updating svn:ignore for '"+dir+"'"); |
---|
| 52 | |
---|
| 53 | readSvnIgnore(dir, function(error, ignore) { |
---|
| 54 | if ( error ) { |
---|
| 55 | callback(error); |
---|
| 56 | return; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | ignore = createIgnore(files, ignore); |
---|
| 60 | |
---|
| 61 | writeSvnIgnore(dir, ignore, function(error) { |
---|
| 62 | callback(error); |
---|
| 63 | }); |
---|
| 64 | |
---|
| 65 | }); |
---|
| 66 | |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function clearSvnIgnore(dir,callback) { |
---|
| 70 | if ( !grunt.file.exists(dir) || !grunt.file.isDir(dir) ) { |
---|
| 71 | callback(null); |
---|
| 72 | return; |
---|
| 73 | } |
---|
| 74 | grunt.log.writeln("Cleaning svn:ignore for '"+dir+"'"); |
---|
| 75 | |
---|
| 76 | readSvnIgnore(dir, function(error, ignore) { |
---|
| 77 | if ( error ) { |
---|
| 78 | callback(error); |
---|
| 79 | return; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | ignore = clearIgnore(ignore); |
---|
| 83 | if ( /^\s*$/.test(ignore) ) { |
---|
| 84 | deleteSvnIgnore(dir, function(error) { |
---|
| 85 | callback(error); |
---|
| 86 | }); |
---|
| 87 | } else { |
---|
| 88 | writeSvnIgnore(dir, ignore, function(error) { |
---|
| 89 | callback(error); |
---|
| 90 | }); |
---|
| 91 | } |
---|
| 92 | }); |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | function createIgnore(files, currentIgnore) { |
---|
| 97 | // build autoIgnore section |
---|
| 98 | var autoIgnore = "### begin grunt-svn-ignore managed ignores\n### edits will be overwritten when grunt svn-ignore is run"; |
---|
| 99 | _.each(files, function(file){ |
---|
| 100 | autoIgnore += "\n"+file; |
---|
| 101 | }); |
---|
| 102 | autoIgnore += "\n### end grunt-svn-ignore managed ignores"; |
---|
| 103 | |
---|
| 104 | // replace iff exists or append |
---|
| 105 | var re = new RegExp(regexp); |
---|
| 106 | var newIgnore = currentIgnore || ""; |
---|
| 107 | if ( re.test(newIgnore) ) { |
---|
| 108 | newIgnore = newIgnore.replace(re,"$1"+autoIgnore+"$2"); |
---|
| 109 | } else { |
---|
| 110 | newIgnore += "\n"+autoIgnore; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | return newIgnore; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | function clearIgnore(currentIgnore) { |
---|
| 117 | // replace iff exists or append |
---|
| 118 | var re = new RegExp(regexp); |
---|
| 119 | var newIgnore = currentIgnore || ""; |
---|
| 120 | newIgnore = newIgnore.replace(re,""); |
---|
| 121 | return newIgnore; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | function readSvnIgnore(dir,callback) { |
---|
| 125 | return svnCommand('svn', |
---|
| 126 | ['propget', 'svn:ignore', dir], |
---|
| 127 | callback); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | function writeSvnIgnore(dir,ignore,callback) { |
---|
| 131 | return svnCommand('svn', |
---|
| 132 | ['propset', 'svn:ignore', ignore, dir], |
---|
| 133 | callback); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | function deleteSvnIgnore(dir,callback) { |
---|
| 137 | return svnCommand('svn', |
---|
| 138 | ['propdel', 'svn:ignore', dir], |
---|
| 139 | callback); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | function svnCommand(cmd,args,callback) { |
---|
| 143 | grunt.util.spawn({ |
---|
| 144 | cmd: cmd, |
---|
| 145 | args: (args || []) |
---|
| 146 | }, function(error, result, code) { |
---|
| 147 | if ( error ) { |
---|
| 148 | callback(error); |
---|
| 149 | return; |
---|
| 150 | } |
---|
| 151 | callback(null,result.stdout); |
---|
| 152 | }); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | }; |
---|