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