<commit_before>var _ = require('lodash'),
  cache = require('gulp-cached'),
  cfValidate = require('../../gulp-plugins/gulp-cf-validate'),
  es = require('event-stream'),
  fs = require('fs-extra'),
  gulp = require('gulp'),
  gulpif = require('gulp-if'),
  gutil = require('gulp-util'),
  matter = require('gray-matter'),
  path = require('path'),
  rename = require('gulp-rename'),
  through = require('through2'),
  util = require("util"),
  url = require('url');


module.exports = function(s3opts,distPath,awsS3) {
  var self = this;

  var templateData = {};

  templateData.s3 = s3opts.aws;
  templateData.s3.prefix = s3opts.prefix;
  var urlString = [awsS3.endpoint.href,path.posix.join(s3opts.aws.bucket,s3opts.prefix)].join('');
  templateData.s3.condensationUrl = url.parse(urlString);

  var stream = es.readable(function(esCount,streamCb) {
    var readable = this;

    var totalCount = 0;
    var lastTotalCount = 0;

    var runStreams = function(globs,options) {
      var s = gulp.src(globs,options)
      .pipe(cache(self.options.projectName+distPath))
      .pipe(gulpif(/\.hbs$/,through.obj(function(file,enc,cb) {
        var m = matter(file.contents.toString());
        var fn = self.handlebars.compile(m.content,{noEscape:true});
        file.contents = new Buffer(fn(_.merge(m.data,templateData),{data: {_file: file}}));
        cb(null,file);
      })))
      .pipe(through.obj(function(file,enc,cb) {
        totalCount = totalCount + 1;
        readable.emit('data',file);
        cb(null,file);
      }));
      s.on('data',function(){});
      s.on('end',function(err) {
        if (lastTotalCount === totalCount) {
          readable.emit('end');
          streamCb();
        }
        else {
          var paths = _.invokeMap(
            self.particleLoader.processablePaths(),
            function() {
              return this+"?(.hbs)";
            }
          );

          lastTotalCount = totalCount;
          runStreams(paths,{base:self.options.root});
        }
      });
    };

    runStreams(
      [
        "particles/cftemplates/**/*.json",
        "particles/cftemplates/**/*.template",
        "particles/cftemplates/**/*.hbs"
      ],
      {cwd:self.options.root,base:self.options.root}
    );

  });


  var parseTemplate = through.obj(function(file,enc,cb) {
    var error;
    if (file.isNull()) {
      //Do Nothing for now
    }
    else {
      try {
        var formatted = JSON.stringify(JSON.parse(file.contents.toString()), null, 2);
        file.contents = new Buffer(formatted);
      }
      catch(e) {
        e.message = "File " + file.path + " is not valid JSON. " + e.message;
        fs.outputFileSync(path.join('condensation_errors',file.path),file.contents);
        error = new gutil.PluginError('build',e);
      }
    }
    cb(error,file);
  });

  var doValidate = function(file) {
    return (file.path.match(/cftemplates[\/\\]/) && (process.env.FORCE_VALIDATE || s3opts.validate))
  }

  stream = stream
  .pipe(gulpif(/\.hbs$/,rename({extname:""})))
  //.pipe(gulpif(/cftemplates[\/\\]/,parseTemplate).on("error",function(e) {this.emit("end"); }))
  .pipe(gulpif(/cftemplates[\/\\]/,parseTemplate))
  .pipe(gulpif(doValidate,cfValidate({region: s3opts.aws.region})))
  .pipe(gulp.dest(distPath))

  return stream;
};<commit_msg>add templatePath to data object<commit_after>var _ = require('lodash');
var cache = require('gulp-cached');
var cfValidate = require('../../gulp-plugins/gulp-cf-validate');
var es = require('event-stream');
var fs = require('fs-extra');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var matter = require('gray-matter');
var path = require('path');
var rename = require('gulp-rename');
var through = require('through2');
var url = require('url');
var util = require("util");


module.exports = function(s3opts,distPath,awsS3) {
  var self = this;

  var templateData = {};
  var urlString = [awsS3.endpoint.href,path.posix.join(s3opts.aws.bucket,s3opts.prefix)].join('');

  templateData.s3 = s3opts.aws;
  templateData.s3.prefix = s3opts.prefix;
  templateData.s3.condensationUrl = url.parse(urlString);

  var stream = es.readable(function(esCount,streamCb) {
    var readable = this;

    var totalCount = 0;
    var lastTotalCount = 0;

    var runStreams = function(globs,options) {
      var s = gulp.src(globs,options)
      .pipe(cache(self.options.projectName+distPath))
      .pipe(gulpif(/\.hbs$/,through.obj(function(file,enc,cb) {
        var m = matter(file.contents.toString());
        var fn = self.handlebars.compile(m.content,{noEscape:true});
        var templatePath = file.relative.replace("particles/cftemplates/","");
        templatePath = templatePath.replace(/\.hbs$/,"");
        file.contents = new Buffer(fn(
          _.merge(m.data,templateData),
          {data: {_file: file, _templatePath: templatePath}}
        ));
        cb(null,file);
      })))
      .pipe(through.obj(function(file,enc,cb) {
        totalCount = totalCount + 1;
        readable.emit('data',file);
        cb(null,file);
      }));
      s.on('data',function(){});
      s.on('end',function(err) {
        if (lastTotalCount === totalCount) {
          readable.emit('end');
          streamCb();
        }
        else {
          var paths = _.invokeMap(
            self.particleLoader.processablePaths(),
            function() {
              return this+"?(.hbs)";
            }
          );

          lastTotalCount = totalCount;
          runStreams(paths,{base:self.options.root});
        }
      });
    };

    runStreams(
      [
        "particles/cftemplates/**/*.json",
        "particles/cftemplates/**/*.template",
        "particles/cftemplates/**/*.hbs"
      ],
      {cwd:self.options.root,base:self.options.root}
    );

  });


  var parseTemplate = through.obj(function(file,enc,cb) {
    var error;
    if (file.isNull()) {
      //Do Nothing for now
    }
    else {
      try {
        var formatted = JSON.stringify(JSON.parse(file.contents.toString()), null, 2);
        file.contents = new Buffer(formatted);
      }
      catch(e) {
        e.message = "File " + file.path + " is not valid JSON. " + e.message;
        fs.outputFileSync(path.join('condensation_errors',file.path),file.contents);
        error = new gutil.PluginError('build',e);
      }
    }
    cb(error,file);
  });

  var doValidate = function(file) {
    return (file.path.match(/cftemplates[\/\\]/) && (process.env.FORCE_VALIDATE || s3opts.validate))
  }

  stream = stream
  .pipe(gulpif(/\.hbs$/,rename({extname:""})))
  .pipe(gulpif(/cftemplates[\/\\]/,parseTemplate))
  .pipe(gulpif(doValidate,cfValidate({region: s3opts.aws.region})))
  .pipe(gulp.dest(distPath))

  return stream;
};
{'fraction_non_alphanumeric': 0.17571365182665286, 'fraction_numerical': 0.0050288418872947785, 'mean_word_length': 2.750415973377704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6177987', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2236, 'n_words': 340}