]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add an "arc alias" command
authorepriestley <git@epriestley.com>
Tue, 21 Feb 2012 21:16:52 +0000 (13:16 -0800)
committerepriestley <git@epriestley.com>
Tue, 21 Feb 2012 21:16:52 +0000 (13:16 -0800)
Summary:
Allow users to easily define aliased arc commands. This is easier than making
them muck around with shell stuff, and lets me do stuff like "arc alias adiff
diff -- --auto" so I can test --auto more easily.

There are some limitations here (for example, you can't put --trace in an alias
because it gets parsed too early) but I think it's a reasonable starting point.

Test Plan: Set, listed, removed and used aliases.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran, epriestley
Maniphest Tasks: T896

Differential Revision: https://secure.phabricator.com/D1653

scripts/arcanist.php
src/__phutil_library_map__.php
src/configuration/ArcanistConfiguration.php
src/workflow/alias/ArcanistAliasWorkflow.php [new file with mode: 0644]
src/workflow/alias/__init__.php [new file with mode: 0644]
src/workflow/base/ArcanistBaseWorkflow.php
src/workflow/base/__init__.php
src/workflow/install-certificate/ArcanistInstallCertificateWorkflow.php
src/workflow/install-certificate/__init__.php
src/workflow/shell-complete/ArcanistShellCompleteWorkflow.php
src/workflow/shell-complete/__init__.php

index 4f772a63b149434cce28bb7dd40041ca43fa9c74..1e84eb3c0e791ddbf10f0d88e0a895c4933cf4c3 100755 (executable)
@@ -164,15 +164,40 @@ try {
   }
 
   $command = strtolower($args[0]);
+  $args = array_slice($args, 1);
   $workflow = $config->buildWorkflow($command);
   if (!$workflow) {
-    throw new ArcanistUsageException(
-      "Unknown command '{$command}'. Try 'arc help'.");
+
+    // If the user has an alias, like 'arc alias dhelp diff help', look it up
+    // and substitute it. We do this only after trying to resolve the workflow
+    // normally to prevent you from doing silly things like aliasing 'alias'
+    // to something else.
+
+    list($new_command, $args) = ArcanistAliasWorkflow::resolveAliases(
+      $command,
+      $config,
+      $args);
+
+    if ($new_command) {
+      $workflow = $config->buildWorkflow($new_command);
+    }
+
+    if (!$workflow) {
+      throw new ArcanistUsageException(
+        "Unknown command '{$command}'. Try 'arc help'.");
+    } else {
+      if ($config_trace_mode) {
+        $aliases = ArcanistAliasWorkflow::getAliases();
+        $target = implode(' ', idx($aliases, $command, array()));
+        echo "[alias: 'arc {$command}' -> 'arc {$target}']\n";
+      }
+      $command = $new_command;
+    }
   }
   $workflow->setArcanistConfiguration($config);
   $workflow->setCommand($command);
   $workflow->setWorkingDirectory($working_directory);
-  $workflow->parseArguments(array_slice($args, 1));
+  $workflow->parseArguments($args);
 
   $need_working_copy    = $workflow->requiresWorkingCopy();
   $need_conduit         = $workflow->requiresConduit();
index d94b89b1b896f4d103787f67343d39c1b9c7dfdf..c6f3b428748dfcfea7fc8addb10ca1424f68879e 100644 (file)
@@ -8,6 +8,7 @@
 phutil_register_library_map(array(
   'class' =>
   array(
+    'ArcanistAliasWorkflow' => 'workflow/alias',
     'ArcanistAmendWorkflow' => 'workflow/amend',
     'ArcanistApacheLicenseLinter' => 'lint/linter/apachelicense',
     'ArcanistApacheLicenseLinterTestCase' => 'lint/linter/apachelicense/__tests__',
@@ -116,6 +117,7 @@ phutil_register_library_map(array(
   ),
   'requires_class' =>
   array(
+    'ArcanistAliasWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistAmendWorkflow' => 'ArcanistBaseWorkflow',
     'ArcanistApacheLicenseLinter' => 'ArcanistLicenseLinter',
     'ArcanistApacheLicenseLinterTestCase' => 'ArcanistLinterTestCase',
index 1a6eadda0c2b8d85acc28db7aea8eeca23b12e2e..e89e7f0c69dbc67fedd8298622b1b2cd2a12c77a 100644 (file)
@@ -100,6 +100,10 @@ class ArcanistConfiguration {
     return $workflows;
   }
 
+  final public function isValidWorkflow($workflow) {
+    return (bool)$this->buildWorkflow($workflow);
+  }
+
   public function willRunWorkflow($command, ArcanistBaseWorkflow $workflow) {
     // This is a hook.
   }
diff --git a/src/workflow/alias/ArcanistAliasWorkflow.php b/src/workflow/alias/ArcanistAliasWorkflow.php
new file mode 100644 (file)
index 0000000..2afa45b
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+
+/*
+ * Copyright 2012 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Show which revision or revisions are in the working copy.
+ *
+ * @group workflow
+ */
+final class ArcanistAliasWorkflow extends ArcanistBaseWorkflow {
+
+  public function getCommandHelp() {
+    return phutil_console_format(<<<EOTEXT
+      **alias**
+      **alias** __command__
+      **alias** __command__ __target__ -- [__options__]
+          Supports: cli
+          Create an alias from __command__ to __target__ (optionally, with
+          __options__). For example:
+
+            arc alias fpatch patch -- --force
+
+          ...will create a new 'arc' command, 'arc fpatch', which invokes
+          'arc patch --force ...' when run. NOTE: use "--" before specifying
+          options!
+
+          You can not overwrite builtins, including 'alias' itself. The builtin
+          will always execute, even if it was added after your alias.
+
+          To remove an alias, run:
+
+            arc alias fpatch
+
+          Without any arguments, 'arc alias' will list aliases.
+
+EOTEXT
+      );
+  }
+
+  public function getArguments() {
+    return array(
+      '*' => 'argv',
+    );
+  }
+
+  public static function getAliases() {
+    $config = self::readUserConfigurationFile();
+    return idx($config, 'aliases');
+  }
+
+  private function writeAliases(array $aliases) {
+    $config = self::readUserConfigurationFile();
+    $config['aliases'] = $aliases;
+    self::writeUserConfigurationFile($config);
+  }
+
+  public function run() {
+
+    $aliases = self::getAliases();
+
+    $argv = $this->getArgument('argv');
+    if (count($argv) == 0) {
+      if ($aliases) {
+        foreach ($aliases as $alias => $binding) {
+          echo phutil_console_format(
+            "**%s** %s\n",
+            $alias,
+            implode(' ' , $binding));
+        }
+      } else {
+        echo "You haven't defined any aliases yet.\n";
+      }
+    } else if (count($argv) == 1) {
+      if (empty($aliases[$argv[0]])) {
+        echo "No alias '{$argv[0]}' to remove.\n";
+      } else {
+        echo phutil_console_format(
+          "'**arc %s**' is currently aliased to '**arc %s**'.",
+          $argv[0],
+          implode(' ', $aliases[$argv[0]]));
+        $ok = phutil_console_confirm('Delete this alias?');
+        if ($ok) {
+          $was = implode(' ', $aliases[$argv[0]]);
+          unset($aliases[$argv[0]]);
+          $this->writeAliases($aliases);
+          echo "Unaliased '{$argv[0]}' (was '{$was}').\n";
+        } else {
+          throw new ArcanistUserAbortException();
+        }
+      }
+    } else {
+      $arc_config = $this->getArcanistConfiguration();
+
+      if ($arc_config->buildWorkflow($argv[0])) {
+        throw new ArcanistUsageException(
+          "You can not create an alias for '{$argv[0]}' because it is a ".
+          "builtin command. 'arc alias' can only create new commands.");
+      }
+
+      $aliases[$argv[0]] = array_slice($argv, 1);
+
+      echo phutil_console_format(
+        "Aliased '**arc %s**' to '**arc %s**'.\n",
+        $argv[0],
+        implode(' ', $aliases[$argv[0]]));
+
+      $this->writeAliases($aliases);
+    }
+
+    return 0;
+  }
+
+  public static function resolveAliases(
+    $command,
+    ArcanistConfiguration $config,
+    array $argv) {
+
+    $aliases = ArcanistAliasWorkflow::getAliases();
+    if (!isset($aliases[$command])) {
+      return array(null, $argv);
+    }
+
+    $new_command = head($aliases[$command]);
+    $workflow = $config->buildWorkflow($new_command);
+    if (!$workflow) {
+      return array(null, $argv);
+    }
+
+    $alias_argv = array_slice($aliases[$command], 1);
+    foreach ($alias_argv as $alias_arg) {
+      if (!in_array($alias_arg, $argv)) {
+        array_unshift($argv, $alias_arg);
+      }
+    }
+
+    return array($new_command, $argv);
+  }
+
+}
diff --git a/src/workflow/alias/__init__.php b/src/workflow/alias/__init__.php
new file mode 100644 (file)
index 0000000..e5111ce
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'exception/usage');
+phutil_require_module('arcanist', 'exception/usage/userabort');
+phutil_require_module('arcanist', 'workflow/base');
+
+phutil_require_module('phutil', 'console');
+phutil_require_module('phutil', 'utils');
+
+
+phutil_require_source('ArcanistAliasWorkflow.php');
index 199f959d569e9fff3770171aa01c1d264e93d316..5e3dd820a36b9998093a0ae4bf54dca0f7c0f137 100644 (file)
@@ -952,6 +952,17 @@ abstract class ArcanistBaseWorkflow {
     return $user_config;
   }
 
+
+  public static function writeUserConfigurationFile($config) {
+    $json_encoder = new PhutilJSON();
+    $json = $json_encoder->encodeFormatted($config);
+
+    $path = self::getUserConfigurationFileLocation();
+    Filesystem::writeFile($path, $json);
+    execx('chmod 600 %s', $path);
+  }
+
+
   /**
    * Write a message to stderr so that '--json' flags or stdout which is meant
    * to be piped somewhere aren't disrupted.
index f23596cf2c2e20da51cb44212ea5f7f7f2dfd55f..49420f4d2fca53ff0634ba685323ed1606b620d3 100644 (file)
@@ -21,6 +21,7 @@ phutil_require_module('phutil', 'console');
 phutil_require_module('phutil', 'filesystem');
 phutil_require_module('phutil', 'future/exec');
 phutil_require_module('phutil', 'moduleutils');
+phutil_require_module('phutil', 'parser/json');
 phutil_require_module('phutil', 'utils');
 
 
index 36aadc93b46d5a3fd459df7513c31b483620095f..38160630e8040cb62077ff30325480cf6c23e766 100644 (file)
@@ -110,14 +110,8 @@ EOTEXT
       'cert' => $info['certificate'],
     );
 
-    $json_encoder = new PhutilJSON();
-    $json = $json_encoder->encodeFormatted($config);
-
     echo "Writing ~/.arcrc...\n";
-
-    $path = self::getUserConfigurationFileLocation();
-    Filesystem::writeFile($path, $json);
-    execx('chmod 600 %s', $path);
+    self::writeUserConfigurationFile($config);
 
     echo phutil_console_format(
       "<bg:green>** SUCCESS! **</bg> Certificate installed.\n");
index ad6ca17ef9e7823e8c3f6829a12d24a2c444ce5f..d5f910fe6cf8bc1579202f66550806316ab1a860 100644 (file)
@@ -11,9 +11,6 @@ phutil_require_module('arcanist', 'workflow/base');
 
 phutil_require_module('phutil', 'conduit/client');
 phutil_require_module('phutil', 'console');
-phutil_require_module('phutil', 'filesystem');
-phutil_require_module('phutil', 'future/exec');
-phutil_require_module('phutil', 'parser/json');
 phutil_require_module('phutil', 'parser/uri');
 
 
index c297ac0b9ce4cb6deafd878170829967bcd6b6e3..17358089ed1814fead0a89a31bf90f87aa917ffb 100644 (file)
@@ -93,13 +93,33 @@ EOTEXT
         $complete[] = $name;
       }
 
+      // Also permit autocompletion of "arc alias" commands.
+      foreach (ArcanistAliasWorkflow::getAliases() as $key => $value) {
+        $complete[] = $key;
+      }
+
       echo implode(' ', $complete)."\n";
       return 0;
     } else {
       $workflow = $arc_config->buildWorkflow($argv[1]);
       if (!$workflow) {
-        return 1;
+        list($new_command, $new_args) = ArcanistAliasWorkflow::resolveAliases(
+          $argv[1],
+          $arc_config,
+          array_slice($argv, 2));
+        if ($new_command) {
+          $workflow = $arc_config->buildWorkflow($new_command);
+        }
+        if (!$workflow) {
+          return 1;
+        } else {
+          $argv = array_merge(
+            array($argv[0]),
+            array($new_command),
+            $new_args);
+        }
       }
+
       $arguments = $workflow->getArguments();
 
       $prev = idx($argv, $pos - 1, null);
index dbb4ac027a1e28493fc9024338bb54d433b16f6d..405ce60e13861bbc33045b8086f2ac685ba04023 100644 (file)
@@ -7,6 +7,7 @@
 
 
 phutil_require_module('arcanist', 'repository/api/base');
+phutil_require_module('arcanist', 'workflow/alias');
 phutil_require_module('arcanist', 'workflow/base');
 phutil_require_module('arcanist', 'workingcopyidentity');