<commit_before>/*
 * grunt-manifest-concat
 * https://github.com/mcavallo
 *
 * Copyright (c) 2015 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

exports.init = function(grunt) {
  var exports = {};

  var fs = require('fs'),
      util = require('util'),
      path = require('path'),
      chalk = require('chalk');

  exports.new = function(filePath, dest, options) {
    return new ManifestFile(filePath, dest, options);
  }

  function ManifestFile(filePath, dest, options) {
    this.options = options;
    this.contents = [];
    this._init(filePath, dest);
  }

  ManifestFile.prototype.isValid = function() {
    // Must have files
    if (!this.contents.length)
      return false;

    // Must have a target (dest)
    if (!this.file.target)
      return false;

    return true;
  }

  ManifestFile.prototype.taskName = function() {
    return this.file.taskName;
  }

  ManifestFile.prototype.taskSettings = function() {
    var options = {};

    if (this.options.banner)
      options.banner = '// Manifest: ' + this.file.path + '\n\n';

    if (this.options.sourceMap)
      options.sourceMap = true;

    return ({
      options: options,
      src: this.contents,
      dest: this.file.target
    });
  }

  ManifestFile.prototype._init = function(filePath, dest) {
    grunt.log.writeln('\nProccessing ' + chalk.cyan(filePath));
    var json = grunt.file.readJSON(filePath);
    var pathObject = path.parse(filePath);

    this.file = {
      target: null,
      path: filePath,
      base: pathObject.base,
      dir: pathObject.dir,
      name: pathObject.name,
      taskName: 'manifest_' + pathObject.name.toLowerCase().replace(/[^a-z0-9]/gi, '_')
    };

    // Generate target based on the gruntfile config dest
    this.file.target = this._generateTarget(dest);

    this._readOptions(json.options);
    this._readContents(json.contents);
  };

  ManifestFile.prototype._processDirective = function(type, value) {
    switch(type) {
      case 'require':
        var item = path.join(this.file.dir, this.options.cwd, value);
        if (this.contents.indexOf(item) != -1)
          return;
        this.contents.push(item);
      break;
      default:
        grunt.verbose.error('Directive `' + type + '` unhandled.');
        return;
      break;
    }

    grunt.log.writeln(chalk.green('Added') + ' ' + item);
  }

  ManifestFile.prototype._readContents = function(contents) {
    if (!contents || !(contents instanceof Array))
      return false;

    for (var i = 0, t = contents.length; i < t; i++) {
      // Strings are considered a plain require
      if (typeof contents[i] === 'string') {
        this._processDirective('require', contents[i]);
        continue;
      }

      // If key/value pair is found proceed using the keys as type
      if (contents[i] instanceof Object) {
        for (var key in contents[i]) {
          this._processDirective(key.toLowerCase(), contents[i][key]);
        }
      }
    }
  };

  ManifestFile.prototype._readOptions = function(options) {
    // Options in manifest file take precedence
    // over any previously set options

    if (options) {
      if (typeof options.sourceMap != 'undefined')<commit_msg>Add require_directory and refactor directives.<commit_after>/*
 * grunt-manifest-concat
 * https://github.com/mcavallo
 *
 * Copyright (c) 2015 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

exports.init = function(grunt) {
  var exports = {};

  var fs = require('fs'),
      util = require('util'),
      path = require('path'),
      chalk = require('chalk');

  exports.new = function(filePath, dest, options) {
    return new ManifestFile(filePath, dest, options);
  }

  function ManifestFile(filePath, dest, options) {
    this.options = options;
    this.contents = [];
    this._init(filePath, dest);
  }

  ManifestFile.prototype.isValid = function() {
    // Must have files
    if (!this.contents.length)
      return false;

    // Must have a target (dest)
    if (!this.file.target)
      return false;

    return true;
  }

  ManifestFile.prototype.taskName = function() {
    return this.file.taskName;
  }

  ManifestFile.prototype.taskSettings = function() {
    var options = {};

    if (this.options.banner)
      options.banner = '// Manifest: ' + this.file.path + '\n\n';

    if (this.options.sourceMap)
      options.sourceMap = true;

    return ({
      options: options,
      src: this.contents,
      dest: this.file.target
    });
  }

  ManifestFile.prototype._init = function(filePath, dest) {
    grunt.log.writeln('\nProccessing ' + chalk.cyan(filePath));
    var json = grunt.file.readJSON(filePath);
    var pathObject = path.parse(filePath);

    this.file = {
      target: null,
      path: filePath,
      base: pathObject.base,
      dir: pathObject.dir,
      name: pathObject.name,
      taskName: 'manifest_' + pathObject.name.toLowerCase().replace(/[^a-z0-9]/gi, '_')
    };

    // Generate target based on the gruntfile config dest
    this.file.target = this._generateTarget(dest);

    this._readOptions(json.options);
    this._readContents(json.contents);
  };

  ManifestFile.prototype._addFile = function(filePath, skipDuplicated) {
    if (typeof skipDuplicated === 'undefined')
      skipDuplicated = true;

    if (skipDuplicated && this.contents.indexOf(filePath) != -1)
      return;

    this.contents.push(filePath);
    grunt.log.writeln(chalk.green('Added') + ' ' + filePath);
  }

  ManifestFile.prototype._addDirectory = function(dirPath) {
    var self = this;
    if (!grunt.file.isDir(dirPath))
      return false;

    grunt.file.expand(path.join(dirPath, '*.' + self.options.extension)).forEach(function(filePath) {
      self._addFile(filePath);
    });
  }

  ManifestFile.prototype._processDirective = function(type, value) {
    var self = this;
    switch(type) {
      case 'require':
        var subject = path.join(self.file.dir, self.options.cwd, value);
        self._addFile(subject);
      break;
      case 'require_directory':
        var subject = path.join(self.file.dir, value);
        grunt.file.expand(subject).forEach(function(dir) {
          self._addDirectory(dir);
        });
      break;
      default:
        grunt.verbose.error('Directive `' + type + '` unhandled.');
      break;
    }
  }

  ManifestFile.prototype._readContents = function(contents) {
    if (!contents || !(contents instanceof Array))
      return false;

    for (var i = 0, t = contents.length; i < t; i++) {
      // Strings are considered a plain require
      if (typeof contents[i] === 'string') {
        this._processDirective('require', contents[i]);
        continue;
      }

      // If key/value pair is found proceed using the keys as type
      if (contents[i] instanceof Object) {
        for (var key in contents[i]) {
          this._processDirective(key.toLowerCase(), contents[i][key]);
        }
      }
    }
  };

  ManifestFile.prototype._readOptions = function(options) {
    // Options in manifest file take precedence
    // over any previously set options

    if (options) {
      if (typeof options.sourceMap != 'undefined')
{'fraction_non_alphanumeric': 0.13729881035689293, 'fraction_numerical': 0.002239328201539538, 'mean_word_length': 2.672147995889003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4408579', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2178, 'n_words': 485}