<commit_before>'use strict';

var gulp         = require('gulp'),
    gutil        = require('gulp-util'),
    wordwrap     = require('wordwrap'),
    runSequence  = require('run-sequence'),
    fs           = require('fs'),
    path         = require('path'),
    template     = require('lodash.template'),
    ideTemplate  = require('ide-template');

var defaults = require('../lib/config/defaults'),
    platform = require('../lib/config/platform'),
    streams  = require('../lib/config/streams'),
    yargs    = require('../lib/util/yargs'),
    hr       = require('../lib/util/hr');

var TEMPLATE_PATH = path.join(__dirname, '..', 'templates', 'webstorm');

var cliArgs;
// Load the global config for defaults
var config = defaults.getInstance('webstorm')
  .file(platform.userHomeDirectory(), '.angularity')
  .defaults({
    'project'  : true,
    'tools'    : true,
    'codestyle': true,
    'templates': true,
    'launch'   : true
  });

var check = yargs.createCheck()
  // don't check if we are just accessing help
  .withGate(function (argv) {
    return !(argv.help);
  })
  .withTest(validateLaunchPath)
  .withTest({subdir: validateSubDirectory})
  // don't check for a project if we are just setting defaults
  .withGate(function (argv) {
    return !(argv.defaults);
  })
  .withTest(angularityProjectPresent)
  .commit();

yargs.getInstance('webstorm')
  .usage(wordwrap(2, 80)([
    'The "webstorm" task initialises webstorm for a project in the current working directory and launches the IDE.',
    '',
    'Where the IDE is installed in a non-standard location the full path to the IDE should be used in place of the ' +
    'boolean in --launch.',
    '',
    'The following steps are taken. Some steps are gated by respective a flag. Default options may be globally ' +
    'defined or reset using the --defaults option.',
    '',
    '* Setup project (resources, debug config, suppressors)   --project',
    '* Create external tools that launch angularity           --tools',
    '* Set coding style                                       --codestyle',
    '* Add code templates                                     --templates',
    '* Launch IDE                                             --launch',
  ].join('\n')))
  .example('angularity webstorm', 'Run this task')
  .example('angularity webstorm --defaults -l <some-path>', 'Set a default executable path')
  .example('angularity webstorm --defaults reset', 'Reset defaults')
  .options('help', {
    describe: 'This help message',
    alias   : ['h', '?'],
    boolean : true
  })
  .options('defaults', {
    describe: 'Set defaults',
    alias   : 'z',
    string  : true
  })
  .options('subdir', {
    describe: 'Navigate to the sub-directory specified',
    alias   : 's',
    string  : true,
    default : config.get('subdir')
  })
  .options('project', {
    describe: 'Setup project',
    alias   : 'p',
    boolean : true,
    default : config.get('project')
  })
  .options('tools', {
    describe: 'Install external tools',
    alias   : 't',
    boolean : true,
    default : config.get('tools')
  })
  .options('codestyle', {
    describe: 'Install the Javascript code style',
    alias   : 'r',
    boolean : true,
    default : config.get('codestyle')
  })
  .options('templates', {
    describe: 'Add code templates',
    alias   : 't',
    boolean : true,
    default : config.get('templates')
  })
  .options('launch', {
    describe: 'Launch the IDE following setup',
    alias   : 'l',
    string  : true,
    default : config.get('launch')
  })
  .strict()
  .check(yargs.subCommandCheck)
  .check(check)
  .wrap(80);

gulp.task('webstorm', function (done) {
  console.log(hr('-', 80, 'webstorm'));

  // Find the yargs instance that is most appropriate for the given command line parameters
  cliArgs = validateLaunchPath(yargs.resolveArgv());

  cliArgs = yargs.resolveArgv();

  if (cliArgs.taskName === 'init') {
    cliArgs = config.get(); // default arguments when called by the "init" task
  }

  // set defaults
  if (cliArgs.defaults) {
    ((cliArgs.defaults === 'reset') ? config.revert() : config.set(cliArgs))
      .changeList()
      .forEach(function (key) {
        gutil.log('default ' + key + ': ' + JSON.stringify(config.get(key)));
      });
    gutil.log('wrote file ' + config.commit());
  }

  // else run the selected items
  else {
    var taskList = [
      cliArgs.subdir && 'webstorm:subdir',
      cliArgs.project && 'webstorm:project',
      cliArgs.templates && 'webstorm:templates',
      cliArgs.tools && 'webstorm:tools',
      cliArgs.codestyle && 'webstorm:codestyle',
      cliArgs.launch && 'webstorm:launch'
    ].filter(Boolean).concat(done);

    runSequence.apply(runSequence, taskList);
  }
});

gulp.task('webstorm:subdir', function () {
  process.chdir(path.resolve(cliArgs.subdir));  // !! changing cwd !!
});

gulp.task('webstorm:project', function () {
  var properties = require(path.resolve('angularity.json'));

  var context = {
    projectName         : properties.name,
    jshintPath          : '$PROJECT_DIR$/.jshintrc',
    jsDebugPort         : properties.port,
    javascriptVersion   : 'es6',
    watcherSuppressedTasks: 'Traceur compiler;SCSS',
    contentPaths        : [
      {content: 'file://' + process.cwd()}
    ],
    libraries           : [
      'jasmine-DefinitelyTyped',
      'angular'
    ],
    selectedDebugName   : 'JavaScript Debug.' + properties.name,
    jsDebugConfiguration: subdirectoriesWithFile(streams.APP, 'index.html')
      .map(function (directory) {
        var directoryTerms = directory.split(path.sep).slice(1);
        return {
          name   : [properties.name].concat(directoryTerms).join('/'),
          uri    : 'http://localhost:' + [properties.port].concat(directoryTerms).join('/'),
          mapping: {
            url: 'http://localhost:' + properties.port, localFile: '$PROJECT_DIR$'
          }
        };
      }),
    plainText           : [
      'file://$PROJECT_DIR$/app-build/index.css',
      'file://$PROJECT_DIR$/app-build/index.js',
      'file://$PROJECT_DIR$/app-test/index.js'
    ],<commit_msg>add karma debug to webstorm<commit_after>'use strict';

var gulp         = require('gulp'),
    gutil        = require('gulp-util'),
    wordwrap     = require('wordwrap'),
    runSequence  = require('run-sequence'),
    fs           = require('fs'),
    path         = require('path'),
    template     = require('lodash.template'),
    ideTemplate  = require('ide-template');

var defaults = require('../lib/config/defaults'),
    platform = require('../lib/config/platform'),
    streams  = require('../lib/config/streams'),
    yargs    = require('../lib/util/yargs'),
    hr       = require('../lib/util/hr');

var TEMPLATE_PATH = path.join(__dirname, '..', 'templates', 'webstorm');

var cliArgs;
// Load the global config for defaults
var config = defaults.getInstance('webstorm')
  .file(platform.userHomeDirectory(), '.angularity')
  .defaults({
    'project'  : true,
    'tools'    : true,
    'codestyle': true,
    'templates': true,
    'launch'   : true
  });

var check = yargs.createCheck()
  // don't check if we are just accessing help
  .withGate(function (argv) {
    return !(argv.help);
  })
  .withTest(validateLaunchPath)
  .withTest({subdir: validateSubDirectory})
  // don't check for a project if we are just setting defaults
  .withGate(function (argv) {
    return !(argv.defaults);
  })
  .withTest(angularityProjectPresent)
  .commit();

yargs.getInstance('webstorm')
  .usage(wordwrap(2, 80)([
    'The "webstorm" task initialises webstorm for a project in the current working directory and launches the IDE.',
    '',
    'Where the IDE is installed in a non-standard location the full path to the IDE should be used in place of the ' +
    'boolean in --launch.',
    '',
    'The following steps are taken. Some steps are gated by respective a flag. Default options may be globally ' +
    'defined or reset using the --defaults option.',
    '',
    '* Setup project (resources, debug config, suppressors)   --project',
    '* Create external tools that launch angularity           --tools',
    '* Set coding style                                       --codestyle',
    '* Add code templates                                     --templates',
    '* Launch IDE                                             --launch',
  ].join('\n')))
  .example('angularity webstorm', 'Run this task')
  .example('angularity webstorm --defaults -l <some-path>', 'Set a default executable path')
  .example('angularity webstorm --defaults reset', 'Reset defaults')
  .options('help', {
    describe: 'This help message',
    alias   : ['h', '?'],
    boolean : true
  })
  .options('defaults', {
    describe: 'Set defaults',
    alias   : 'z',
    string  : true
  })
  .options('subdir', {
    describe: 'Navigate to the sub-directory specified',
    alias   : 's',
    string  : true,
    default : config.get('subdir')
  })
  .options('project', {
    describe: 'Setup project',
    alias   : 'p',
    boolean : true,
    default : config.get('project')
  })
  .options('tools', {
    describe: 'Install external tools',
    alias   : 't',
    boolean : true,
    default : config.get('tools')
  })
  .options('codestyle', {
    describe: 'Install the Javascript code style',
    alias   : 'r',
    boolean : true,
    default : config.get('codestyle')
  })
  .options('templates', {
    describe: 'Add code templates',
    alias   : 't',
    boolean : true,
    default : config.get('templates')
  })
  .options('launch', {
    describe: 'Launch the IDE following setup',
    alias   : 'l',
    string  : true,
    default : config.get('launch')
  })
  .strict()
  .check(yargs.subCommandCheck)
  .check(check)
  .wrap(80);

gulp.task('webstorm', function (done) {
  console.log(hr('-', 80, 'webstorm'));

  // Find the yargs instance that is most appropriate for the given command line parameters
  cliArgs = validateLaunchPath(yargs.resolveArgv());

  cliArgs = yargs.resolveArgv();

  if (cliArgs.taskName === 'init') {
    cliArgs = config.get(); // default arguments when called by the "init" task
  }

  // set defaults
  if (cliArgs.defaults) {
    ((cliArgs.defaults === 'reset') ? config.revert() : config.set(cliArgs))
      .changeList()
      .forEach(function (key) {
        gutil.log('default ' + key + ': ' + JSON.stringify(config.get(key)));
      });
    gutil.log('wrote file ' + config.commit());
  }

  // else run the selected items
  else {
    var taskList = [
      cliArgs.subdir && 'webstorm:subdir',
      cliArgs.project && 'webstorm:project',
      cliArgs.templates && 'webstorm:templates',
      cliArgs.tools && 'webstorm:tools',
      cliArgs.codestyle && 'webstorm:codestyle',
      cliArgs.launch && 'webstorm:launch'
    ].filter(Boolean).concat(done);

    runSequence.apply(runSequence, taskList);
  }
});

gulp.task('webstorm:subdir', function () {
  process.chdir(path.resolve(cliArgs.subdir));  // !! changing cwd !!
});

gulp.task('webstorm:project', function () {
  var properties = require(path.resolve('angularity.json'));

  var context = {
    projectName         : properties.name,
    jshintPath          : '$PROJECT_DIR$/.jshintrc',
    jsDebugPort         : properties.port,
    javascriptVersion   : 'es6',
    watcherSuppressedTasks: 'Traceur compiler;SCSS',
    JsKarmaPackageDirSetting: 'C:\\Program Files\\nodejs\\node_modules\\karma', // todo resolve by platform
    contentPaths        : [
      {content: 'file://' + process.cwd()}
    ],
    libraries           : [
      'jasmine-DefinitelyTyped',
      'angular'
    ],
    selectedDebugName   : 'JavaScript Debug.' + properties.name,
    karmaDebugConfiguration : [
      {
        name: properties.name,
        configFile: '$PROJECT_DIR$/' + streams.TEST + '/karma.conf.js',
        browsers: 'Chrome',
        env: [],
        tasks: [
          {
            name: 'Tool_Angularity_test --debug'
          }
        ]
      }
    ],
    jsDebugConfiguration: subdirectoriesWithFile(streams.APP, 'index.html')
      .map(function (directory) {
        var directoryTerms = directory.split(path.sep).slice(1);
        return {
          name   : [properties.name].concat(directoryTerms).join('/'),
          uri    : 'http://localhost:' + [properties.port].concat(directoryTerms).join('/'),
          mapping: {
            url: 'http://localhost:' + properties.port, localFile: '$PROJECT_DIR$'
          }
        };
      }),
    plainText           : [
      'file://$PROJECT_DIR$/app-build/index.css',
      'file://$PROJECT_DIR$/app-build/index.js',
      'file://$PROJECT_DIR$/app-test/index.js'
    ],
{'fraction_non_alphanumeric': 0.15428932207402732, 'fraction_numerical': 0.0014205666482519138, 'mean_word_length': 2.588785046728972, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '720172', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3783, 'n_words': 901}