From 9203b71b73053d133d36bc8793d0dccebcb98acc Mon Sep 17 00:00:00 2001
From: Greg Anderson <greg.1.anderson@greenknowe.org>
Date: Wed, 6 Mar 2013 21:26:54 +0900
Subject: Issue #1408654 by Greg Anderson, moshe weitzman: Add concurrency to
 pm-download by default

---
 commands/pm/pm.drush.inc |    1 +
 drush.php                |   23 +++++++++++++++++++++--
 includes/command.inc     |    5 +++++
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/commands/pm/pm.drush.inc b/commands/pm/pm.drush.inc
index 73b7212..4021e8d 100644
--- a/commands/pm/pm.drush.inc
+++ b/commands/pm/pm.drush.inc
@@ -300,6 +300,7 @@ function pm_drush_command() {
     ),
     'bootstrap' => DRUSH_BOOTSTRAP_MAX,
     'aliases' => array('dl'),
+    'auto-concurrency' => TRUE, // Can also be an array. See _drush_bootstrap_and_dispatch().
     'engines' => array(
       'version_control',
       'package_handler',
diff --git a/drush.php b/drush.php
index 91563bf..9bc5fcb 100755
--- a/drush.php
+++ b/drush.php
@@ -88,8 +88,27 @@ function _drush_bootstrap_and_dispatch() {
           drush_log(dt("Found command: !command (commandfile=!commandfile)", array('!command' => $command['command'], '!commandfile' => $command['commandfile'])), 'bootstrap');
 
           $command_found = TRUE;
-          // Dispatch the command(s).
-          $return = drush_dispatch($command);
+
+          // Dispatch the command.
+          $auto = $command['auto-concurrency'];
+          $affirmative = drush_get_context('DRUSH_AFFIRMATIVE') || !empty($auto['common_options']['yes']);
+          if ($affirmative && $auto && count($command['arguments']) >= 2 && drush_get_option('concurrency', 4)) {
+            drush_log(dt("Concurrently executing command with a concurrency level of !concurrency", array('!concurrency' => drush_get_option('concurrency', 4))), 'notice');
+            // Commands can get auto-concurrency with special command definition.
+            foreach ($command['arguments'] as $arg) {
+              $invocations[] = array(
+                'site' => '',
+                'args' => array($arg),
+                'options' => drush_redispatch_get_options(),
+              );
+            }
+            $common_options = isset($auto['common_options']) ? $auto['common_options'] : array();
+            $common_backend_options = array_merge(array('output' => FALSE), isset($auto['common_backend_options']) ? $auto['common_backend_options'] : array());
+            $return = drush_backend_invoke_concurrent($invocations, $common_options, $common_backend_options, $command['command']);
+          }
+          else {
+            $return = drush_dispatch($command);
+          }
 
           // prevent a '1' at the end of the output
           if ($return === TRUE) {
diff --git a/includes/command.inc b/includes/command.inc
index 6f42b41..94755e9 100644
--- a/includes/command.inc
+++ b/includes/command.inc
@@ -927,6 +927,10 @@ function drush_get_commands() {
 
         // Translate command.
         drush_command_translate($command);
+        // Add concurrency option if command supports uses auto-concurrency.
+        if ($command['auto-concurrency']) {
+          $command['options']['concurrency'] = 'Changes the number of concurrent processes for this command. Must run with the --yes option to get concurrency. Default is 4.';
+        }
 
         // If command callback function name begins with "drush_$commandfile_",
         // then fix up the command entry so that drush_invoke will be
@@ -964,6 +968,7 @@ function drush_command_defaults($key, $commandfile, $path) {
   return array(
     'command' => $key,
     'command-hook' => $key,
+    'auto-concurrency' => FALSE,
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
     'callback arguments' => array(),
     'commandfile' => $commandfile,
-- 
1.7.9.5

