Skip to content

Commit

Permalink
[console] Add allow_failure flag. (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas committed Apr 10, 2017
1 parent 3895bd3 commit 8986c38
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
33 changes: 25 additions & 8 deletions src/Command/Chain/ChainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

/**
* Class ChainCommand
*
* @package Drupal\Console\Core\Command\Chain
*/
class ChainCommand extends Command
Expand All @@ -43,6 +44,7 @@ class ChainCommand extends Command

/**
* ChainCommand constructor.
*
* @param ChainQueue $chainQueue
* @param ChainDiscovery $chainDiscovery
*/
Expand Down Expand Up @@ -292,6 +294,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$commands = $configData['commands'];
}

$parameterOptions = $input->getOptions();
unset($parameterOptions['file']);

foreach ($commands as $command) {
$moduleInputs = [];
$arguments = !empty($command['arguments']) ? $command['arguments'] : [];
Expand All @@ -305,11 +310,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$moduleInputs['--'.$key] = is_null($value) ? '' : $value;
}

$parameterOptions = $input->getOptions();
unset($parameterOptions['file']);
foreach ($parameterOptions as $key => $value) {
if ($value===true) {
$moduleInputs['--' . $key] = true;
foreach ($this->getApplication()->getDefinition()->getOptions() as $option) {
$optionName = $option->getName();
if (array_key_exists($optionName, $parameterOptions)) {
$optionValue = $parameterOptions[$optionName];
if ($optionValue) {
$moduleInputs['--' . $optionName] = $optionValue;
}
}
}

Expand All @@ -320,13 +327,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
continue;
}

$io->text($command['command']);
$io->newLine();

$input = new ArrayInput($moduleInputs);
if (!is_null($interactive)) {
$input->setInteractive($interactive);
$input->setInteractive($interactive);
}

$io->text($command['command']);
$callCommand->run($input, $io);
$allowFailure = array_key_exists('allow_failure', $command)?$command['allow_failure']:false;
try {
$callCommand->run($input, $io);
} catch (\Exception $e) {
if (!$allowFailure) {
$io->error($e->getMessage());
return 1;
}
}
}

return 0;
Expand Down
16 changes: 13 additions & 3 deletions src/EventSubscriber/CallCommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

/**
* Class CallCommandListener
*
* @package Drupal\Console\Core\EventSubscriber
*/
class CallCommandListener implements EventSubscriberInterface
Expand All @@ -28,6 +29,7 @@ class CallCommandListener implements EventSubscriberInterface

/**
* CallCommandListener constructor.
*
* @param ChainQueue $chainQueue
*/
public function __construct(ChainQueue $chainQueue)
Expand All @@ -46,14 +48,14 @@ public function callCommands(ConsoleTerminateEvent $event)
$io = new DrupalStyle($event->getInput(), $event->getOutput());

if (!$command instanceof Command) {
return;
return 0;
}

$application = $command->getApplication();
$commands = $this->chainQueue->getCommands();

if (!$commands) {
return;
return 0;
}

foreach ($commands as $chainedCommand) {
Expand All @@ -69,7 +71,15 @@ public function callCommands(ConsoleTerminateEvent $event)
}

$io->text($chainedCommand['name']);
$callCommand->run($input, $io);
$allowFailure = array_key_exists('allow_failure', $chainedCommand)?$chainedCommand['allow_failure']:false;
try {
$callCommand->run($input, $io);
} catch (\Exception $e) {
if (!$allowFailure) {
$io->error($e->getMessage());
return 1;
}
}
}
}

Expand Down

0 comments on commit 8986c38

Please sign in to comment.