'use strict';

var debug = require('debug')('base:generators:tasks');
var utils = require('./utils');

module.exports = function(options) {
  return function(app) {
    if (this.isRegistered('base-generators-tasks')) return;

    this.define('resolveTasks', function(names, tasks) {
      var prop = this.stringifyTasks(names, tasks);
      var segs = prop.split(':');

      debug('normalizing task args: "%s"', prop);
      var res = { generator: this, tasks: [] };

      if (this.hasGenerator(segs[0])) {
        res.generator = this.getGenerator(segs[0]);
        res.tasks = segs[1] ? segs[1].split(',') : ['default'];
      } else {
        res.generator = this;
        res.tasks = segs[0].split(',');
      }
      return res;
    });

    /**
     * Return true if the given `name` exists on the
     * `app.tasks` object.
     *
     * @param {String} `name`
     */

    this.define('hasTask', function(name) {
      if (!this.tasks.hasOwnProperty(name)) {
        return false;
      }
      var task = this.tasks[name];
      return typeof task.fn === 'function';
    });

    /**
     * Create a task string from the given `name` or array of names and
     * `task` or array of tasks.
     *
     * @param {String|Array} `name`
     * @param {String|Array} `tasks`
     * @return {String}
     */

    this.define('stringifyTasks', function(names, tasks) {
      if (typeof tasks === 'function') {
        tasks = [];
      }

      names = utils.arrayify(names);
      tasks = utils.arrayify(tasks);

      var res = [names.join('.')]
        .concat(tasks.join(','))
        .filter(Boolean)
        .join(':');

      return res || 'default';
    });
  };
};

@@ -413,16 +413,17 @@
  prop);%0A
+%0A
       va
@@ -460,50 +460,139 @@
 ks: 
-%5B%5D %7D;%0A%0A      if (this.hasGenerator(segs%5B0%5D
+segs%5B0%5D.split(',') %7D;%0A      var len = segs.length;%0A%0A      if (this.hasGenerator(segs%5B0%5D) && (!this.hasTask(res.tasks%5B0%5D) %7C%7C len %3E 1
 )) %7B
@@ -712,93 +712,8 @@
 '%5D;%0A
-      %7D else %7B%0A        res.generator = this;%0A        res.tasks = segs%5B0%5D.split(',');%0A
     

optimize with @doowb