/*
 * grunt-namespace
 * https://github.com/reputation.com/grunt-namespace
 *
 * Copyright (c) 2012 Reputation.com
 * Authored by: Jeff Harnois jeff.harnois@reputation.com
 * Licensed under the MIT license.
 *
 */

module.exports = function(grunt) {

  // Please see the grunt documentation for more information regarding task and
  // helper creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md
  
  // ==========================================================================
  // GLOBAL VARS
  // ==========================================================================
  var file = grunt.file,
      log = grunt.log,
      config = grunt.config;

  // ==========================================================================
  // TASKS
  // ==========================================================================

  grunt.registerMultiTask('namespace', 'Add a namespace to a file.', function() {
    // make sure we get some data
    if (!this.data) { return false; }
    
    // make sure the destination we get is a file, not a path
    if (this.file.dest[this.file.dest.length - 1] === '/') {
       grunt.fatal('never use path as filename');
       return false;
     }
    
    // send unprocessed file.src object to namespace, it will extract css and tpl as it needs
    var files = this.file.src,
        dest = this.file.dest,
        filepath = dest.lastIndexOf("/"),
        folder = filepath(0, filepath),
        namespace = folder.slice(0,filepath.length - 14);

    grunt.file.write(dest, grunt.helper('namespace', files, namespace));

    // Fail task if errors were logged.
    if (this.errorCount) { return false; }
    console.log('no errors');
    // 
    // // Otherwise, print a success message.
    log.writeln('File "' + this.file.dest + '" created.');
  });

  // ==========================================================================
  // HELPERS
  // ==========================================================================
  
  grunt.registerHelper('namespace', function(files, namespace) {
    var contents = '';

    files.map(function(filepath) {
      var raw = file.read(filepath);
      contents += raw;
    });

    return "this." + namespace + " = {\n  " + contents + " \n};";
  });

};
@@ -1381,22 +1381,30 @@
 est.
-lastIndexOf(%22/
+replace(%22Apps/dist/%22,%22
 %22),%0A
@@ -1411,22 +1411,25 @@
         
-folder
+namespace
  = filep
@@ -1435,78 +1435,44 @@
 path
-(0, filepath),%0A        namespace = folder.slice(0,filepath.length - 14
+.replace(%22/debug/js/namespace.js%22,%22%22
 );%0A%0A

updating task