1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-10-24 17:48:50 +02:00
phorge-arcanist/src/workflow/ArcanistAliasWorkflow.php
vrana 66d204be81 Delete license headers from files
Summary:
This commit doesn't change license of any file. It just makes the license implicit (inherited from LICENSE file in the root directory).

We are removing the headers for these reasons:

- It wastes space in editors, less code is visible in editor upon opening a file.
- It brings noise to diff of the first change of any file every year.
- It confuses Git file copy detection when creating small files.
- We don't have an explicit license header in other files (JS, CSS, images, documentation).
- Using license header in every file is not obligatory: http://www.apache.org/dev/apply-license.html#new.

This change is approved by Alma Chao (Lead Open Source and IP Counsel at Facebook).

Test Plan: Verified that the license survived only in unit tests and LICENSE file.

Reviewers: epriestley, btrahan, edward

Reviewed By: epriestley

CC: aran, Korvin, davidrecordon

Maniphest Tasks: T2035

Differential Revision: https://secure.phabricator.com/D3881
2012-11-05 11:16:24 -08:00

171 lines
4.8 KiB
PHP

<?php
/**
* Manages aliases for commands with options.
*
* @group workflow
*/
final class ArcanistAliasWorkflow extends ArcanistBaseWorkflow {
public function getWorkflowName() {
return 'alias';
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**alias**
**alias** __command__
**alias** __command__ __target__ -- [__options__]
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
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!
If you start an alias with "!", the remainder of the alias will be
invoked as a shell command. For example, if you want to implement
'arc ls', you can do so like this:
arc alias ls '!ls'
You can now run "arc ls" and it will behave like "ls". Of course, this
example is silly and would make your life worse.
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($working_copy) {
$working_copy_config_aliases = $working_copy->getConfig('aliases');
if (!$working_copy_config_aliases) {
$working_copy_config_aliases = array();
}
$user_config_aliases =
idx(self::readUserConfigurationFile(), 'aliases', array());
return $user_config_aliases + $working_copy_config_aliases;
}
private function writeAliases(array $aliases) {
$config = self::readUserConfigurationFile();
$config['aliases'] = $aliases;
self::writeUserConfigurationFile($config);
}
public function run() {
// We might not be in a working directory, so we don't want to require a
// working copy identity here.
$working_copy = ArcanistWorkingCopyIdentity::newFromPath(getcwd());
$aliases = self::getAliases($working_copy);
$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 isShellCommandAlias($command) {
return preg_match('/^!/', $command);
}
public static function resolveAliases(
$command,
ArcanistConfiguration $config,
array $argv,
ArcanistWorkingCopyIdentity $working_copy) {
$aliases = ArcanistAliasWorkflow::getAliases($working_copy);
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);
}
}