'argv', ); } public static function getAliases( ArcanistConfigurationManager $configuration_manager) { $working_copy_config_aliases = $configuration_manager->getProjectConfig('aliases'); if (!$working_copy_config_aliases) { $working_copy_config_aliases = array(); } $user_config_aliases = idx( $configuration_manager->readUserConfigurationFile(), 'aliases', array()); return $user_config_aliases + $working_copy_config_aliases; } private function writeAliases(array $aliases) { $config = $this->getConfigurationManager()->readUserConfigurationFile(); $config['aliases'] = $aliases; $this->getConfigurationManager()->writeUserConfigurationFile($config); } public function run() { $aliases = self::getAliases($this->getConfigurationManager()); $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 pht("You haven't defined any aliases yet.")."\n"; } } else if (count($argv) == 1) { if (empty($aliases[$argv[0]])) { echo pht("No alias '%s' to remove.", $argv[0])."\n"; } else { echo pht( "'%s' is currently aliased to '%s'.", phutil_console_format('**arc %s**', $argv[0]), phutil_console_format( '**arc %s**', implode(' ', $aliases[$argv[0]]))); $ok = phutil_console_confirm(pht('Delete this alias?')); if ($ok) { $was = implode(' ', $aliases[$argv[0]]); unset($aliases[$argv[0]]); $this->writeAliases($aliases); echo pht("Unaliased '%s' (was '%s').", $argv[0], $was)."\n"; } else { throw new ArcanistUserAbortException(); } } } else { $arc_config = $this->getArcanistConfiguration(); if ($arc_config->buildWorkflow($argv[0])) { throw new ArcanistUsageException( pht( "You can not create an alias for '%s' because it is a ". "builtin command. '%s' can only create new commands.", $argv[0], 'arc alias')); } $aliases[$argv[0]] = array_slice($argv, 1); echo pht( "Aliased '%s' to '%s'.\n", phutil_console_format('**arc %s**', $argv[0]), phutil_console_format('**arc %s**', implode(' ', $aliases[$argv[0]]))); $this->writeAliases($aliases); } return 0; } public static function isShellCommandAlias($command) { return preg_match('/^!/', $command); } public static function resolveAliases( $command, ArcanistConfiguration $config, array $argv, ArcanistConfigurationManager $configuration_manager) { $aliases = self::getAliases($configuration_manager); if (!isset($aliases[$command])) { return array(null, $argv); } $new_command = head($aliases[$command]); if (self::isShellCommandAlias($new_command)) { return array($new_command, $argv); } $workflow = $config->buildWorkflow($new_command); if (!$workflow) { return array(null, $argv); } $alias_argv = array_slice($aliases[$command], 1); foreach (array_reverse($alias_argv) as $alias_arg) { if (!in_array($alias_arg, $argv)) { array_unshift($argv, $alias_arg); } } return array($new_command, $argv); } }