Skip to content

Commit 7a9220f

Browse files
authored
Add option to keep exit code in build command (#18)
1 parent ba39415 commit 7a9220f

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Build/BuilderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function getDependencies();
2525

2626
/**
2727
* Execute the build logic
28+
*
29+
* @return int|null
2830
*/
2931
public function build();
3032

Command/BuildCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function configure()
8282

8383
$this->addArgument('target', InputArgument::OPTIONAL, 'Target to build', null);
8484
$this->addOption('nodeps', 'D', InputOption::VALUE_NONE, 'Ignore dependencies');
85+
$this->addOption('keep-exit-code', '-k', InputOption::VALUE_NONE, 'Keep the exit code of a job if it fails');
8586
}
8687

8788
/**
@@ -114,17 +115,20 @@ public function execute(InputInterface $input, OutputInterface $output)
114115
if (!$this->question->ask($input, $output, $question)) {
115116
$this->output->writeln('Bye!');
116117

117-
return;
118+
return 0;
118119
}
119120
}
120121

121122
$this->output->writeln('');
122-
$this->runBuilders($builders);
123+
$exitCode = $this->runBuilders($builders);
123124

124125
$end = microtime(true);
125126

126127
$this->output->writeln(sprintf('<info>Done (%ss)</info>', number_format($end - $start, 2)));
127128

129+
if($exitCode !== 0 && $input->getOption('keep-exit-code')) {
130+
return $exitCode;
131+
}
128132
return 0;
129133
}
130134

@@ -172,6 +176,7 @@ protected function runBuilders($builders)
172176

173177
$builderContext = new BuilderContext($this->input, $this->output, $this->getApplication());
174178

179+
$combinedExitCode = 0;
175180
foreach ($builders as $builder) {
176181
$this->output->getFormatter()->setIndentLevel(0);
177182

@@ -187,8 +192,13 @@ protected function runBuilders($builders)
187192
$this->output->writeln('');
188193

189194
$this->output->getFormatter()->setIndentLevel(1);
190-
$builder->build();
195+
$exitCode = $builder->build();
196+
if($exitCode !== null && $exitCode !== 0) {
197+
$combinedExitCode = $exitCode;
198+
}
191199
}
200+
201+
return $combinedExitCode;
192202
}
193203

194204
protected function writeTitle($title)

Tests/Command/BuildCommandTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,26 @@ public function testBuildTarget()
7575
$this->assertEquals(0, $res);
7676
}
7777

78+
public function testBuildTargetThatFails()
79+
{
80+
$this->buildRegistry->getBuilders('Builder 1')->willReturn(array(
81+
$this->builder1->reveal(),
82+
$this->builder2->reveal()
83+
));
84+
85+
$this->builder2->setContainer($this->container)->shouldBeCalled();
86+
$this->builder1->setContext(Argument::type('Massive\Bundle\BuildBundle\Build\BuilderContext'))
87+
->shouldBeCalled();
88+
$this->builder2->setContext(Argument::type('Massive\Bundle\BuildBundle\Build\BuilderContext'))
89+
->shouldBeCalled();
90+
91+
$this->builder1->build()->shouldBeCalled()->willReturn(1);
92+
$this->builder2->build()->shouldBeCalled();
93+
94+
$exitCode = $this->execute(array('target' => 'Builder 1', '--keep-exit-code' => true), array());
95+
$this->assertEquals(1, $exitCode);
96+
}
97+
7898
public function testBuildNotTarget()
7999
{
80100
$this->buildRegistry->getBuilders(null)->willReturn(array(

0 commit comments

Comments
 (0)