source: Dev/trunk/grunt-tasks/svn-ignore.js @ 517

Last change on this file since 517 was 517, checked in by hendrikvanantwerpen, 11 years ago

Deployment and database management now done through Grunt. Look mom, no shell\!

File size: 4.5 KB
Line 
1var _ = require('underscore')
2  , path = require('path');
3
4var regexp = "(\n?)### begin grunt-svn-ignore(.|\n)*### end grunt-svn-ignore[^\n]*(\n?)";
5
6module.exports = function(grunt) {
7
8    grunt.registerMultiTask(
9        'svn-ignore',
10        "Makes sure the specified source files are ignored in Subversion.",
11        function(){
12            var done = this.async();
13
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();
25
26            grunt.util.async.series(tasks,function(error, results){
27                done(error);
28            });
29        });
30
31    grunt.registerMultiTask(
32        'svn-ignore-clean',
33        "Clean generated ignore sections in Subversion.",
34        function(){
35            var done = this.async();
36
37            var tasks = _.chain(this.filesSrc)
38                         .map(function(file) {
39                             return _.partial(clearSvnIgnore, file);
40                         })
41                         .value();
42
43            grunt.util.async.series(tasks,function(error, results){
44                done(error);
45            });
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) {
127        return svn(['propget', 'svn:ignore', dir],
128                   callback);
129    }
130
131    function writeSvnIgnore(dir,ignore,callback) {
132        return svn(['propset', 'svn:ignore', ignore, dir],
133                   callback);
134    }
135   
136    function deleteSvnIgnore(dir,callback) {
137        return svn(['propdel', 'svn:ignore', dir],
138                   callback);
139    }
140   
141    function svn(args,callback) {
142        grunt.util.spawn({
143            cmd: 'svn',
144            args: (args || [])
145        }, function(error, result) {
146            if ( error ) {
147                callback(error);
148            } else {
149                callback(null,result.stdout);
150            }
151        });
152    }
153   
154};
Note: See TracBrowser for help on using the repository browser.