/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = new Container();
$verbose = OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity();
$strategy = $container->getChainStrategy();
$executor = $container->getExecutor(!$input->getOption('no-cache'), $verbose, $input->getOption('timeout'));
$serviceManager = $container->getServiceManager($verbose);
$notifier = NotifierFactory::create();
$output->writeln("<info>Creating builds...</info>");
$jobs = $strategy->getJobs($input->getOption("project-path"));
$output->writeln(sprintf("<info>%s builds created</info>", count($jobs)));
$exitCode = 0;
try {
foreach ($jobs as $job) {
$output->writeln(sprintf("\n<info>Running job %s</info>\n", $job->getDescription()));
$serviceManager->start($job);
$strategy->prepareJob($job);
$success = $executor->test($job, $input->getArgument('cmd'));
$exitCode += $success == 0 ? 0 : 1;
if ($input->getOption('notify')) {
$notification = new Notification();
$notification->setBody(sprintf('Test results for %s on %s', $container->getNaming()->getProjectName($input->getOption('project-path')), $job->getDescription()));
$notification->setTitle($success == 0 ? 'Tests passed' : 'Tests failed');
$notifier->send($notification);
}
$serviceManager->stop($job);
}
} catch (\Exception $e) {
// Try stop last builds
if (isset($job)) {
$serviceManager->stop($job);
}
// We do not deal with exception (Console Component do it well),
// we just catch it to allow cleaner to be runned even if one of the build failed hard
// Simulation of a finally for php < 5.6 :-°
}
$container->getVacuum()->clean($input->getOption("project-path"), $input->getOption("keep"));
if (isset($e)) {
throw $e;
}
return $exitCode;
}