Changeset View
Changeset View
Standalone View
Standalone View
src/toolset/ArcanistAlias.php
- This file was added.
<?php | |||||
final class ArcanistAlias extends Phobject { | |||||
private $toolset; | |||||
private $trigger; | |||||
private $command; | |||||
private $exception; | |||||
private $configurationSource; | |||||
public static function newFromConfig($key, $value) { | |||||
$alias = new self(); | |||||
// Parse older style aliases which were always for the "arc" toolset. | |||||
// When we next write these back into the config file, we'll update them | |||||
// to the modern format. | |||||
// The old format looked like this: | |||||
// | |||||
// { | |||||
// "draft": ["diff", "--draft"] | |||||
// } | |||||
// | |||||
// The new format looks like this: | |||||
// | |||||
// { | |||||
// [ | |||||
// "toolset": "arc", | |||||
// "trigger": "draft", | |||||
// "command": ["diff", "--draft"] | |||||
// ] | |||||
// } | |||||
// | |||||
// For now, we parse the older format and fill in the toolset as "arc". | |||||
$is_list = false; | |||||
$is_dict = false; | |||||
if ($value && is_array($value)) { | |||||
if (array_keys($value) === range(0, count($value) - 1)) { | |||||
amckinley: Kinda surprised that this isn't a `libphutil` function. I had to think about this for a minute. | |||||
Done Inline ActionsIt probably should be, I bet we've got at least a few callsites by now and the construction isn't obvious to look at. Got any good name ideas? phutil_array_has_natural_keys()? phutil_is_deserialized_json_list_not_object()? epriestley: It probably should be, I bet we've got at least a few callsites by now and the construction… | |||||
Not Done Inline Actionsphutil_is_like_a_regular_non_php_array_thing? amckinley: `phutil_is_like_a_regular_non_php_array_thing`? | |||||
$is_list = true; | |||||
} else { | |||||
$is_dict = true; | |||||
} | |||||
} | |||||
if ($is_list) { | |||||
$alias->trigger = $key; | |||||
$alias->toolset = 'arc'; | |||||
$alias->command = $value; | |||||
} else if ($is_dict) { | |||||
try { | |||||
PhutilTypeSpec::checkMap( | |||||
$value, | |||||
array( | |||||
'trigger' => 'string', | |||||
'toolset' => 'string', | |||||
'command' => 'list<string>', | |||||
)); | |||||
$alias->trigger = idx($value, 'trigger'); | |||||
$alias->toolset = idx($value, 'toolset'); | |||||
$alias->command = idx($value, 'command'); | |||||
} catch (PhutilTypeCheckException $ex) { | |||||
$alias->exception = new PhutilProxyException( | |||||
pht( | |||||
'Found invalid alias definition (with key "%s").', | |||||
$key), | |||||
$ex); | |||||
} | |||||
} else { | |||||
$alias->exception = new Exception( | |||||
pht( | |||||
'Expected alias definition (with key "%s") to be a dictionary.', | |||||
$key)); | |||||
} | |||||
return $alias; | |||||
} | |||||
public function setToolset($toolset) { | |||||
$this->toolset = $toolset; | |||||
return $this; | |||||
} | |||||
public function getToolset() { | |||||
return $this->toolset; | |||||
} | |||||
public function setTrigger($trigger) { | |||||
$this->trigger = $trigger; | |||||
return $this; | |||||
} | |||||
public function getTrigger() { | |||||
return $this->trigger; | |||||
} | |||||
public function setCommand(array $command) { | |||||
$this->command = $command; | |||||
return $this; | |||||
} | |||||
public function getCommand() { | |||||
return $this->command; | |||||
} | |||||
public function setException(Exception $exception) { | |||||
$this->exception = $exception; | |||||
return $this; | |||||
} | |||||
public function getException() { | |||||
return $this->exception; | |||||
} | |||||
public function isShellCommandAlias() { | |||||
$command = $this->getCommand(); | |||||
if (!$command) { | |||||
return false; | |||||
} | |||||
$head = head($command); | |||||
return preg_match('/^!/', $head); | |||||
} | |||||
public function getStorageDictionary() { | |||||
return array( | |||||
'trigger' => $this->getTrigger(), | |||||
'toolset' => $this->getToolset(), | |||||
'command' => $this->getCommand(), | |||||
); | |||||
} | |||||
public function setConfigurationSource( | |||||
ArcanistConfigurationSource $configuration_source) { | |||||
$this->configurationSource = $configuration_source; | |||||
return $this; | |||||
} | |||||
public function getConfigurationSource() { | |||||
return $this->configurationSource; | |||||
} | |||||
} | |||||
Kinda surprised that this isn't a libphutil function. I had to think about this for a minute.