Skip to content

Commit

Permalink
Merge pull request consolidation#301 from Codegyre/annotation-command
Browse files Browse the repository at this point in the history
Factor out consolidation/annotation-command
  • Loading branch information
greg-1-anderson committed Mar 24, 2016
2 parents faf89aa + 772f564 commit f213930
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 633 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=5.5.0",
"league/container": "~2.2",
"consolidation/log": "~1",
"phpdocumentor/reflection-docblock": "~2",
"consolidation/annotation-command": "~0",
"symfony/finder": "~2.5|~3.0",
"symfony/console": "~2.5|~3.0",
"symfony/process": "~2.5|~3.0",
Expand Down
239 changes: 112 additions & 127 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 0 additions & 92 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,6 @@ public function __construct($name, $version)
);
}

public function addCommandsFromClass($className, $passThrough = null)
{
$container = $this->getContainer();
$roboTasks = new $className;
if ($roboTasks instanceof ContainerAwareInterface) {
$roboTasks->setContainer($container);
}

// Ignore special functions, such as __construct() and __call(), and
// accessor methods such as getFoo() and setFoo(), while allowing
// set or setup.
$commandNames = array_filter(get_class_methods($className), function ($m) {
return !preg_match('#^(_|get[A-Z]|set[A-Z])#', $m);
});

foreach ($commandNames as $commandName) {
$command = $this->createCommand(new TaskInfo($className, $commandName));
$command->setCode(function (InputInterface $input) use ($roboTasks, $commandName, $passThrough, $container) {
// get passthru args
$args = $input->getArguments();
array_shift($args);
if ($passThrough) {
$args[key(array_slice($args, -1, 1, true))] = $passThrough;
}
$args[] = $input->getOptions();
// Need a better way to handle global options
// Also, this is not necessarily the best place to do this
Config::setGlobalOptions($input);
$container->setSimulated(Config::isSimulated());

$res = call_user_func_array([$roboTasks, $commandName], $args);
if (is_int($res)) {
exit($res);
}
if (is_bool($res)) {
exit($res ? 0 : 1);
}
if ($res instanceof Result) {
exit($res->getExitCode());
}
});
$this->add($command);
}
}

public function createCommand(TaskInfo $taskInfo)
{
$task = new Command($taskInfo->getName());
$task->setDescription($taskInfo->getDescription());
$task->setHelp($taskInfo->getHelp());

$args = $taskInfo->getArguments();
foreach ($args as $name => $val) {
$description = $taskInfo->getArgumentDescription($name);
if ($val === TaskInfo::PARAM_IS_REQUIRED) {
$task->addArgument($name, InputArgument::REQUIRED, $description);
} elseif (is_array($val)) {
$task->addArgument($name, InputArgument::IS_ARRAY, $description, $val);
} else {
$task->addArgument($name, InputArgument::OPTIONAL, $description, $val);
}
}
$opts = $taskInfo->getOptions();
foreach ($opts as $name => $val) {
$description = $taskInfo->getOptionDescription($name);

$fullName = $name;
$shortcut = '';
if (strpos($name, '|')) {
list($fullName, $shortcut) = explode('|', $name, 2);
}

if (is_bool($val)) {
$task->addOption($fullName, $shortcut, InputOption::VALUE_NONE, $description);
} else {
$task->addOption($fullName, $shortcut, InputOption::VALUE_OPTIONAL, $description, $val);
}
}

// TODO: Symfony Console commands by default put aliases and example
// usages together in a list, one per line, in the "Usage" section.
// There is no way to attach a description to a sample usage. We
// need to figure out how to replace the built-in help command with
// our own version that has additional help sections (e.g. topics).
$task->setAliases($taskInfo->getAliases());
foreach ($taskInfo->getExampleUsages() as $usage => $description) {
$task->addUsage($usage);
}

return $task;
}

public function addInitRoboFileCommand($roboFile, $roboClass)
{
$createRoboFile = new Command('init');
Expand Down
Loading

0 comments on commit f213930

Please sign in to comment.