/*
 * grunt-contrib-handlebars
 * http://gruntjs.com/
 *
 * Copyright (c) 2012 Tim Branyen, contributors
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

  var _ = grunt.util._;
  var helpers = require('grunt-lib-contrib').init(grunt);

  // filename conversion for templates
  var defaultProcessName = function(name) { return name; };

  // filename conversion for partials
  var defaultProcessPartialName = function(filePath) {
    var pieces = _.last(filePath.split('/')).split('.');
    var name   = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
    return name.substr(1, name.length);                       // strips leading _ character
  };

  grunt.registerMultiTask('handlebars', 'Compile handlebars templates and partials.', function() {

    var options = this.options({namespace: 'JST'});

    grunt.verbose.writeflags(options, 'Options');

    var compiled, srcFiles, src, filename;
    var partials = [];
    var templates = [];
    var output = [];
    var nsInfo = helpers.getNamespaceDeclaration(options.namespace);

    // assign regex for partial detection
    var isPartial = options.partialRegex || /^_/;

    // assign filename transformation functions
    var processName = options.processName || defaultProcessName;
    var processPartialName = options.processPartialName || defaultProcessPartialName;

    // iterate files, processing partials and templates separately
    this.files.forEach(function(files) {
      srcFiles = grunt.file.expandFiles(files.src);
      srcFiles.forEach(function(file) {
        src = grunt.file.read(file);

        try {
          compiled = require('handlebars').precompile(src);
          // if configured to, wrap template in Handlebars.template call
          if(options.wrapped) {
            compiled = 'Handlebars.template('+compiled+')';
          }
        } catch (e) {
          grunt.log.error(e);
          grunt.fail.warn('Handlebars failed to compile '+file+'.');
        }

        // register partial or add template to namespace
        if(isPartial.test(_.last(file.split('/')))) {
          filename = processPartialName(file);
          partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
        } else {
          filename = processName(file);
          templates.push(nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';');
        }
      });
      output = output.concat(partials, templates);

      if (output.length > 0) {
        output.unshift(nsInfo.declaration);
        grunt.file.write(files.dest, output.join('\n\n'));
        grunt.log.writeln('File "' + files.dest + '" created.');
      }
    });
  });

};

@@ -1505,56 +1505,13 @@
     
-this.files.forEach(function(files) %7B%0D%0A      srcF
+var f
 iles
@@ -1536,21 +1536,25 @@
 ndFiles(
+this.
 file
-s
 .src);%0D%0A
@@ -1561,14 +1561,9 @@
     
-  srcF
+f
 iles
@@ -1585,26 +1585,24 @@
 on(file) %7B%0D%0A
-  
       src = 
@@ -1637,19 +1637,15 @@
     
-  
 try %7B%0D%0A
-  
     
@@ -1707,18 +1707,16 @@
         
-  
 // if co
@@ -1779,18 +1779,16 @@
         
-  
 if(optio
@@ -1808,26 +1808,24 @@
 %0D%0A          
- 
- 
 compiled = '
@@ -1865,26 +1865,24 @@
 ';%0D%0A        
-  
 %7D%0D%0A        %7D
@@ -1878,18 +1878,16 @@
 %0D%0A      
- 
- 
 %7D catch 
@@ -1889,26 +1889,24 @@
 atch (e) %7B%0D%0A
-  
         grun
@@ -1918,26 +1918,24 @@
 .error(e);%0D%0A
-  
         grun
@@ -1996,18 +1996,16 @@
 %0D%0A      
-  
 %7D%0D%0A%0D%0A   
@@ -2007,18 +2007,16 @@
 %0D%0A      
- 
- 
 // regis
@@ -2063,18 +2063,16 @@
 %0D%0A      
-  
 if(isPar
@@ -2106,26 +2106,24 @@
 ('/')))) %7B%0D%0A
-  
         file
@@ -2164,18 +2164,16 @@
         
-  
 partials
@@ -2251,26 +2251,24 @@
 led+');');%0D%0A
-  
       %7D else
@@ -2275,26 +2275,24 @@
  %7B%0D%0A        
-  
 filename = p
@@ -2306,26 +2306,24 @@
 ame(file);%0D%0A
-  
         temp
@@ -2404,31 +2404,27 @@
 ;');%0D%0A      
-  
 %7D%0D%0A
-  
     %7D);%0D%0A   
@@ -2420,26 +2420,24 @@
    %7D);%0D%0A    
-  
 output = out
@@ -2476,18 +2476,16 @@
 %0D%0A%0D%0A    
-  
 if (outp
@@ -2508,18 +2508,16 @@
 %0D%0A      
- 
- 
 output.u
@@ -2547,26 +2547,24 @@
 on);%0D%0A      
-  
 grunt.file.w
@@ -2568,21 +2568,25 @@
 e.write(
+this.
 file
-s
 .dest, o
@@ -2605,34 +2605,32 @@
 %5Cn%5Cn'));%0D%0A      
-  
 grunt.log.writel
@@ -2642,21 +2642,25 @@
 le %22' + 
+this.
 file
-s
 .dest + 
@@ -2675,36 +2675,25 @@
 ed.');%0D%0A    
-  %7D%0D%0A    %7D);
+%7D
 %0D%0A  %7D);%0D%0A%0D%0A%7D

update to this.file api