From 12dc3e07b6b00768cf5c2f5d6c3ca3fc66978af6 Mon Sep 17 00:00:00 2001
From: Moshe Weitzman <weitzman@tejasa.com>
Date: Tue, 22 Mar 2011 13:16:32 -0400
Subject: [PATCH] Introduce major new feature - shell aliases. Modelled after git aliases - https://git.wiki.kernel.org/index.php/Aliases ... Still needs docs and unit tests.

---
 commands/core/core.drush.inc |   20 ++++++++++++++++++++
 drush.php                    |    7 +++++++
 includes/command.inc         |   28 ++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/commands/core/core.drush.inc b/commands/core/core.drush.inc
index 14aa3fa..f3623a7 100644
--- a/commands/core/core.drush.inc
+++ b/commands/core/core.drush.inc
@@ -149,6 +149,18 @@ function core_drush_command() {
       'immediate' => 'Rebuild the index immediately, instead of waiting for cron.',
     ),
   );
+  $items['core-execute'] = array(
+    'description' => 'Execute a shell command. Usually used with a site alias.',
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
+    'arguments' => array(
+      'command', 'The shell command to be executed',
+    ),
+    'examples' => array(
+      'drush exec \'git pull\'' => 'Retrieve latest code from git',
+    ),
+    'aliases' => array('exec', 'execute'),
+    'topics' => array('docs-aliases'),
+  );
   $items['core-rsync'] = array(
     'description' => 'Rsync the Drupal tree to/from another server using ssh.',
     'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
@@ -853,6 +865,14 @@ function drush_core_find_project_path($target) {
   return NULL;
 }
 
+/*
+ * Command callback. Execute specified shell code. Often used by shell aliases
+ * that start with !.
+ */
+function drush_core_execute($script) {
+  return drush_op_system($script);
+}
+
 function drush_core_cli() {
   // Do not allow cli to start recursively, or from backend invoke.
   if (drush_get_option('in-cli', FALSE)) {
diff --git a/drush.php b/drush.php
index accc228..76ac118 100755
--- a/drush.php
+++ b/drush.php
@@ -83,6 +83,13 @@ function drush_main() {
 
   foreach ($phases as $phase) {
     if (drush_bootstrap_to_phase($phase)) {
+      // If applicable swaps in shell alias value (or executes it).
+      $return = drush_shell_alias_replace();
+      if (isset($return)) {
+        $command_found = TRUE;
+        break;
+      }
+
       $command = drush_parse_command();
 
       // Process a remote command if 'remote-host' option is set.
diff --git a/includes/command.inc b/includes/command.inc
index 16208bf..f732a1b 100644
--- a/includes/command.inc
+++ b/includes/command.inc
@@ -1233,3 +1233,31 @@ function drush_enforce_requirement_core(&$command) {
   }
   $command['bootstrap_errors']['DRUSH_COMMAND_CORE_VERSION_ERROR'] = dt('Command !command requires Drupal core version !versions to run.', array('!command' => $command['command'], '!versions' => $versions));
 }
+
+/*
+ * Check if a shell alias exists for current request. If so, pretend like the
+ * alias value was requested, along with any additional arguments.
+ */
+function drush_shell_alias_replace() {
+  $args = drush_get_arguments();
+  $argv = drush_get_context('argv');
+  $first = current($args);
+  // @todo drush_get_option is awkward here.
+  $shell_aliases = drush_get_option('shell-aliases', array());
+  if (isset($shell_aliases[$first])) {
+    // Shell alias found for first argument in the request.
+    $alias_value = $shell_aliases[$first];
+    drush_log(dt('Shell alias found: !key => !value', array('!key' => $first, '!value' => $alias_value)), 'debug');
+    if (substr($alias_value, 0, 1) == '!') {
+      $replacement = array('core-execute', ltrim($alias_value, '!'));
+    }
+    else {
+      $replacement = explode(' ', $alias_value);
+    }
+    $pos = array_search($first, $argv);
+    array_splice($argv, $pos, 1, $replacement);
+    drush_set_context('argv', $argv);
+    drush_parse_args();
+    _drush_bootstrap_global_options();
+  }
+}
\ No newline at end of file
-- 
1.7.4.1

