Skip to content

Commit

Permalink
Merge pull request #553 from opendevshop/component/power-process/must…
Browse files Browse the repository at this point in the history
…-run-bug

Component / Power Process / mustRun Bug
  • Loading branch information
jonpugh committed Mar 24, 2020
2 parents 119c508 + e683d49 commit e70bd0f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
21 changes: 17 additions & 4 deletions src/DevShop/Component/PowerProcess/PowerProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Robo\Common\OutputAwareTrait;
use Robo\Common\TimeKeeper;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process as BaseProcess;
use Symfony\Component\Console\Style\SymfonyStyle;

Expand Down Expand Up @@ -107,12 +108,24 @@ public function run($callback = null, array $env = [])
}

/**
* Provides compatibility with Process 3.x.
* @inheritDoc
* Runs the process.
*
* This is identical to run() except that an exception is thrown if the process
* exits with a non-zero exit code.
*
* @return $this
*
* @throws RuntimeException if PHP was compiled with --enable-sigchild and the enhanced sigchild compatibility mode is not enabled
* @throws ProcessFailedException if the process didn't terminate successfully
*
* @final since version 3.3
*/
public function mustRun(callable $callback = null/*, array $env = []*/) {
$env = 1 < \func_num_args() ? func_get_arg(1) : null;
parent::mustRun($callback, $env);
$env = 1 < \func_num_args() ? func_get_arg(1) : [];
if (0 !== $this->run($callback, $env)) {
throw new ProcessFailedException($this);
}
return $this;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/DevShop/Component/PowerProcess/PowerProcessStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public function customLite($message, $prefix = '*', $style = '', $newLine = fals
$message
);
}
$this->io->text($message);
$this->text($message);
if ($newLine) {
$this->io->newLine();
$this->newLine();
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public function block($messages, $type = null, $style = null, $prefix = ' ', $pa
* @param $message
* @param string $icon
*/
function helpBlock($message, $icon = ProvisionStyle::ICON_HELP) {
function helpBlock($message, $icon = self::ICON_HELP) {
if (is_array($message)) {
$message = implode("\n", $message);
}
Expand Down
28 changes: 24 additions & 4 deletions src/DevShop/Component/PowerProcess/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
<?php

// Include autoloader
include('vendor/autoload.php');
function includeIfExists(string $file): bool
{
return file_exists($file) && include $file;
}

if (
!includeIfExists(__DIR__ . '/../../autoload.php') &&
!includeIfExists(__DIR__ . '/vendor/autoload.php') &&
!includeIfExists(__DIR__ . '/../../../../vendor/autoload.php')
) {
fwrite(STDERR, 'Dependencies not found. Install with Composer.'.PHP_EOL);
exit(1);
}

// PowerProcess needs IO.
$input = new \Symfony\Component\Console\Input\ArgvInput($argv);
Expand All @@ -14,13 +26,21 @@
// Run any command.
$command = 'ls -la';
$process = new DevShop\Component\PowerProcess\PowerProcess($command, $io);
$process->run();
$process->mustRun();

// Output comes back in real-time.
$command = 'ping packagist.org -c 5';
echo "Using PowerProcess::run() method so failing commands won't cause errors:";
$command = 'ping packagist.orgggg -c 2';
$process = new DevShop\Component\PowerProcess\PowerProcess($command, $io);
$process->run();

$command = 'rm -rf /';
$process = new DevShop\Component\PowerProcess\PowerProcess($command, $io);
$process->run();

try {
$process->mustRun();
}
catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
$io->customLite("The 'rm -rf /' command exited, which throws an exception, but demo.php caught it so the script will still return a successful exit code. Here's the Exception message: ", '!');
echo $e->getMessage();
}
16 changes: 15 additions & 1 deletion src/DevShop/Component/PowerProcess/run
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@
*/

// Include autoloader
include('vendor/autoload.php');

function includeIfExists(string $file): bool
{
return file_exists($file) && include $file;
}

if (
!includeIfExists(__DIR__ . '/../../autoload.php') &&
!includeIfExists(__DIR__ . '/vendor/autoload.php') &&
!includeIfExists(__DIR__ . '/../../../../vendor/autoload.php')
) {
fwrite(STDERR, 'Dependencies not found. Install with Composer.'.PHP_EOL);
exit(1);
}

// PowerProcess needs IO.
$input = new \Symfony\Component\Console\Input\ArgvInput($argv);
Expand All @@ -27,3 +40,4 @@ $io = new DevShop\Component\PowerProcess\PowerProcessStyle($input, $output);
$command = array_slice($argv, 1);
$process = new DevShop\Component\PowerProcess\PowerProcess($command, $io);
$process->run();
exit($process->getExitCode());

0 comments on commit e70bd0f

Please sign in to comment.