/*
 * grunt-regex-replace
 * https://github.com/bomsy/grunt-regex-replace
 *
 * Copyright (c) 2012 Hubert Boma Manilla
 * Licensed under the MIT license.
 *
 */

module.exports = function(grunt) {
  "use strict";
  // Please see the grunt documentation for more information regarding task and
  // helper creation: https://github.com/gruntjs/grunt/blob/master/docs/toc.md

  // ==========================================================================
  // TASKS
  // ==========================================================================
  grunt.registerMultiTask('regex-replace', 'find & replace content of a file based regex patterns', function(){
    var files = grunt.file.expand(this.files[0].src),
      actions = this.data.actions,
      arrString = "[object Array]",
      regexString = "[object RegExp]",
      toString = Object.prototype.toString,
      GLOBAL = 'g',
      options = null,
      srchAction = null,
      rplAction = null,
      updatedContent;
      for(var i = 0; i< files.length; i++){
        if(toString.call(actions) === arrString){
          updatedContent = grunt.file.read(files[i]);
          for(var j = 0; j < actions.length; j++){
            srchAction = actions[j].search,
            rplAction = actions[j].replace; 
            options = actions[j].flags;
            if(typeof options === 'undefined'){
              options = GLOBAL;
            }
            if( (typeof srchAction !== 'string' && toString.call(srchAction) !== regexString ) || (typeof rplAction !== 'string' && typeof rplAction !== 'function') || typeof options !== 'string' ){
              grunt.warn('An error occured while processing (Invalid type passed for \'search\' or \'replace\' of \'flags\', only strings accepted.)' );
            }
            if(typeof srchAction === 'string'){
              srchAction = grunt.template.process(srchAction);
            }
            if(typeof rplAction === 'string'){
              rplAction = grunt.template.process(rplAction);
            }
            updatedContent = regexReplace( updatedContent, srchAction, rplAction , options, j, actions[j].name);
          }
          grunt.file.write(files[i], updatedContent);
          if(this.errorCount){
            return false;
          } 
          grunt.log.writeln('File \'' + files[i] + '\' replace complete.');
        }
      }
  });
  // ==========================================================================
  // HELPERS
  // ==========================================================================

 var regexReplace = function(src, regex, substr, options, index, actionName){
    //takes the src content and changes the content
    var regExp = null,
        updatedSrc;
    if(typeof regex ===  'string'){ 
      regExp = new RegExp(regex , options); //regex => string
    } else {
      regExp = regex; //regex => RegExp object
    }
    updatedSrc = String(src).replace(regExp, substr); //note: substr can be a function
    index = typeof index === 'undefined' ? '' : index;
    if(!actionName){
      grunt.log.writeln(index + 1 + ' action(s) completed.');
    } else {
      grunt.log.writeln(actionName + ' action completed.');
    }
    return updatedSrc;
  };

};

@@ -661,60 +661,8 @@
 var 
-files = grunt.file.expand(this.files%5B0%5D.src),%0A      
 acti
@@ -922,104 +922,88 @@
 nt;%0A
-  
+%0A
     
-for(var i = 0; i%3C files.length; i++)%7B%0A        if(toString.call(actions) === arrString)%7B%0A  
+this.files.forEach(function(file) %7B%0A      file.src.forEach(function(src) %7B%0A
     
@@ -1043,21 +1043,14 @@
 ead(
-files%5Bi%5D
+src
 );%0A
-  
     
@@ -1096,34 +1096,32 @@
 j++)%7B%0A          
-  
 srchAction = act
@@ -1136,18 +1136,16 @@
 search,%0A
-  
         
@@ -1181,34 +1181,32 @@
 ace; %0A          
- 
- 
 options = action
@@ -1217,18 +1217,16 @@
 .flags;%0A
-  
         
@@ -1267,34 +1267,32 @@
 ')%7B%0A            
- 
- 
 options = GLOBAL
@@ -1295,38 +1295,34 @@
 OBAL;%0A          
-  %7D%0A  
+%7D%0A
           if( (t
@@ -1510,26 +1510,24 @@
             
-  
 grunt.warn('
@@ -1653,18 +1653,16 @@
 d.)' );%0A
-  
         
@@ -1667,34 +1667,32 @@
     %7D%0A          
-  
 if(typeof srchAc
@@ -1703,34 +1703,32 @@
  === 'string')%7B%0A
-  
             srch
@@ -1774,38 +1774,34 @@
 ion);%0A          
- 
- 
 %7D%0A
-  
           if(typ
@@ -1833,34 +1833,32 @@
 ')%7B%0A            
- 
- 
 rplAction = grun
@@ -1890,38 +1890,34 @@
 ion);%0A          
-  %7D%0A  
+%7D%0A
           update
@@ -2011,38 +2011,34 @@
 .name);%0A        
-  %7D%0A  
+%7D%0A
         grunt.fi
@@ -2046,24 +2046,19 @@
 e.write(
-files%5Bi%5D
+src
 , update
@@ -2072,80 +2072,8 @@
 t);%0A
-          if(this.errorCount)%7B%0A            return false;%0A          %7D %0A  
     
@@ -2110,16 +2110,11 @@
 ' + 
-files%5Bi%5D
+src
  + '
@@ -2139,30 +2139,84 @@
 e.');%0A      
-  %7D%0A  
+%7D);%0A    %7D);%0A%0A    if(this.errorCount)%7B%0A        return false;%0A
     %7D%0A  %7D);%0A

Support file globbing patterns