<commit_before>/*
 * grunt-version-build
 * https://github.com/robwierzbowski/grunt-version-build
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {
  var fs = require('fs');
  var path = require('path');
  var shelljs = require('shelljs');

  grunt.registerMultiTask('version_build', 'Version built code next to your project\'s source.', function() {

    var done = this.async();
    var options = this.options({
      commit: false,
      // tag: false,
      push: false,
      commitMsg: 'Built from commit %sourceCommit% on branch %sourceBranch%',
      force: false
    });

    var sourceInfo = {
      branch: '(unavailable)',
      commit: '(unavailable)',
      name:   '(unavailable)'
    };

    var currentDir = shelljs.pwd();

    // Check requirements
    function checkRequirements (next) {
      // Check that required options are set.
      ['branch', 'dir', 'remote'].forEach( function (element) {
        if (!options.hasOwnProperty(element)) {
          throw('The "' + element + '" option is required.');
        }
      });

      // Check that the dist directory exists
      if(!shelljs.test('-d', options.dir)) {
        grunt.log.writeln('Creating target directory "' + options.dir + '".');

        if(shelljs.mkdir(options.dir)) {
          throw('Unable to create the target directory "' + options.dir + '".');
        }
      }
    }

    // Get source project information for %tokens%
    function buildSourceInfo () {
      var sourceBranch = shelljs.exec('git symbolic-ref --quiet HEAD', {silent: true});
      var sourceCommit = shelljs.exec('git rev-parse --short HEAD', {silent: true});

      if(sourceBranch.code === 0) {
        sourceInfo.branch = sourceBranch.output.split('/').pop().replace(/\n/g, '');
      }

      if (sourceCommit.code === 0) {
        sourceInfo.commit = sourceCommit.output.replace(/\n/g, '');
      }

      if (shelljs.test('-f', 'package.json', {silent: true})) {
        sourceInfo.name = JSON.parse(fs.readFileSync('package.json', 'utf8')).name;
      }
    }

    // Initialize git repo if one doesn't exist
    function initGit () {
      if(!shelljs.test('-d', path.join(options.dir, '.git'))) {
        grunt.log.writeln("Creating local git repo.");

        if(shelljs.exec('git init').code !== 0) {
          throw("Could not initialize the local git repo."); // TODO: show stderrs for easier debugging
        }
      }
    }

    // Create branch if it doesn't exist
    function initBranch () {

      // TODO: simplify this a bit
      // if branch does not exist
      //   if remote exists
      //     fetch remote
      //   else
      //     create branch
      //     create empty first commit
      // else
      //   return
<commit_msg>Switch direcory checks to node methods, minor copy edit<commit_after>/*
 * grunt-version-build
 * https://github.com/robwierzbowski/grunt-version-build
 *
 * Copyright (c) 2013 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {
  var fs = require('fs');
  var path = require('path');
  var shelljs = require('shelljs');

  grunt.registerMultiTask('version_build', 'Version built code next to your project\'s source.', function() {

    var done = this.async();
    var options = this.options({
      commit: false,
      // tag: false,
      push: false,
      commitMsg: 'Built from commit %sourceCommit% on branch %sourceBranch%',
      force: false
    });

    var sourceInfo = {
      branch: '(unavailable)',
      commit: '(unavailable)',
      name:   '(unavailable)'
    };

    var currentDir = shelljs.pwd();

    // Check requirements
    function checkRequirements (next) {
      // Check that required options are set.
      ['branch', 'dir', 'remote'].forEach( function (element) {
        if (!options.hasOwnProperty(element)) {
          throw('The "' + element + '" option is required.');
        }
      });

      // Check that the dist directory exists
      if(!fs.existsSync(options.dir)) {
        grunt.log.writeln('Creating target directory "' + options.dir + '".');

        if(shelljs.mkdir(options.dir)) {
          throw('Unable to create the target directory "' + options.dir + '".');
        }
      }
    }

    // Get source project information for %tokens%
    function buildSourceInfo () {
      var sourceBranch = shelljs.exec('git symbolic-ref --quiet HEAD', {silent: true});
      var sourceCommit = shelljs.exec('git rev-parse --short HEAD', {silent: true});

      if(sourceBranch.code === 0) {
        sourceInfo.branch = sourceBranch.output.split('/').pop().replace(/\n/g, '');
      }

      if (sourceCommit.code === 0) {
        sourceInfo.commit = sourceCommit.output.replace(/\n/g, '');
      }

      if (shelljs.test('-f', 'package.json', {silent: true})) {
        sourceInfo.name = JSON.parse(fs.readFileSync('package.json', 'utf8')).name;
      }
    }

    // Initialize git repo if one doesn't exist
    function initGit () {
      if (!fs.existsSync(path.join(options.dir, '.git'))) {
        grunt.log.writeln('Creating git repository in ' + options.dir + '.');

        if(shelljs.exec('git init').code !== 0) {
          throw("Could not initialize the local git repo."); // TODO: show stderrs for easier debugging
        }
      }
    }

    // Create branch if it doesn't exist
    function initBranch () {

      // TODO: simplify this a bit
      // if branch does not exist
      //   if remote exists
      //     fetch remote
      //   else
      //     create branch
      //     create empty first commit
      // else
      //   return

{'fraction_non_alphanumeric': 0.14932767162066526, 'fraction_numerical': 0.0028308563340410475, 'mean_word_length': 2.301985981308411, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2813113', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1635, 'n_words': 442}