Skip to content

Commit 17bee04

Browse files
committed
First commit; branch from main repo
0 parents  commit 17bee04

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
/tests/reports/

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "task/process",
3+
"description": "Process plugin for Task",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Mike Fisher",
8+
"email": "mike@mbfisher.com"
9+
}
10+
],
11+
"require": {
12+
"task/plugin": "~1.0",
13+
"symfony/process": "~2.4"
14+
},
15+
"require-dev": {
16+
"phpspec/phpspec": "2.0.*@dev"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Task\\Plugin\\": "src/"
21+
}
22+
}
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Task\Plugin\Process;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ProcessBuilderSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Task\Plugin\Process\ProcessBuilder');
13+
}
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace spec\Task\Plugin\Process;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Task\Plugin\Stream\WritableInterface;
8+
9+
class ProcessSpec extends ObjectBehavior
10+
{
11+
function let()
12+
{
13+
$this->beConstructedWith('whoami');
14+
}
15+
16+
function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Task\Plugin\Process\Process');
19+
}
20+
21+
function it_should_throw_on_run_it_non_zero_exit_code()
22+
{
23+
$this->setCommandLine('exit 1');
24+
$this->shouldThrow('RuntimeException')->duringRun();
25+
}
26+
27+
function it_should_return_exit_code_on_run()
28+
{
29+
$this->setCommandLine('exit 0');
30+
$this->run()->shouldEqual(0);
31+
}
32+
33+
function it_should_run_and_get_output_on_read()
34+
{
35+
$this->read()->shouldEqual(`whoami`);
36+
}
37+
38+
function it_should_write_read_data_on_pipe(WritableInterface $to)
39+
{
40+
$to->write(`whoami`)->willReturn('me');
41+
$this->pipe($to)->shouldEqual('me');
42+
}
43+
44+
function it_should_set_stdin_on_write_and_return_this()
45+
{
46+
$this->write('test')->shouldReturn($this);
47+
$this->getStdin()->shouldReturn('test');
48+
}
49+
}

src/Process/AbstractCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Task\Plugin\Process;
4+
5+
class AbstractCommand extends ProcessBuilder
6+
{
7+
public function addArgument($arg)
8+
{
9+
$this->args[] = $arg;
10+
11+
return $this;
12+
}
13+
14+
public function addOption($option, $value = null)
15+
{
16+
$this->options[] = $option;
17+
if ($value !== null) {
18+
$this->options[] = $value;
19+
}
20+
21+
return $this;
22+
}
23+
24+
public function getProcess()
25+
{
26+
$this->setArguments(array_merge($this->options, $this->args));
27+
return parent::getProcess();
28+
}
29+
}

src/Process/Exception.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Task\Plugin\Process;
4+
5+
class Exception extends \Exception {}

src/Process/Process.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Task\Plugin\Process;
4+
5+
use Symfony\Component\Process\Process as BaseProcess;
6+
use Task\Plugin\Stream\WritableInterface;
7+
use Task\Plugin\Stream\ReadableInterface;
8+
9+
class Process extends BaseProcess implements WritableInterface, ReadableInterface
10+
{
11+
protected $stream;
12+
13+
public static function extend(BaseProcess $proc)
14+
{
15+
return new static(
16+
$proc->getCommandLine(),
17+
$proc->getWorkingDirectory(),
18+
$proc->getEnv(),
19+
$proc->getStdin(),
20+
$proc->getTimeout(),
21+
$proc->getOptions()
22+
);
23+
}
24+
25+
public function run($callback = null)
26+
{
27+
if ($this->stream) {
28+
$stream = $this->stream;
29+
$callback = function ($type, $data) use ($stream) {
30+
$stream->write($data);
31+
};
32+
}
33+
34+
$exitcode = parent::run($callback);
35+
36+
if ($this->isSuccessful()) {
37+
return $exitcode;
38+
} else {
39+
throw new \RuntimeException(
40+
sprintf(
41+
"%s returned %d\n%s",
42+
$this->getCommandLine(),
43+
$this->getExitCode(),
44+
$this->getErrorOutput()
45+
)
46+
);
47+
}
48+
49+
return $this;
50+
}
51+
52+
public function read()
53+
{
54+
$this->run();
55+
return $this->getOutput();
56+
}
57+
58+
public function write($data)
59+
{
60+
return $this->setStdin($data);
61+
}
62+
63+
public function pipe(WritableInterface $to)
64+
{
65+
$this->stream = $to;
66+
return $to->write($this->read());
67+
}
68+
}

src/Process/ProcessBuilder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Task\Plugin\Process;
4+
5+
use Symfony\Component\Process\ProcessBuilder as BaseProcessBuilder;
6+
use Task\Plugin\Stream\WritableInterface;
7+
8+
class ProcessBuilder extends BaseProcessBuilder implements WritableInterface
9+
{
10+
public function getProcess()
11+
{
12+
return Process::extend(parent::getProcess());
13+
}
14+
15+
public function run()
16+
{
17+
return $this->getProcess()->run();
18+
}
19+
20+
public function read()
21+
{
22+
return $this->run()->getOutput();
23+
}
24+
25+
public function write($data)
26+
{
27+
return $this->setInput($data)->getProcess();
28+
}
29+
30+
public function pipe(WritableInterface $to)
31+
{
32+
return $to->write($this->read());
33+
}
34+
}

src/ProcessPlugin.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Task\Plugin;
4+
5+
use Task\Plugin\Process\ProcessBuilder;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class ProcessPlugin implements PluginInterface, WritableInterface
9+
{
10+
public function build($command, array $args = [])
11+
{
12+
return ProcessBuilder::create()
13+
->setPrefix($command)
14+
->setArguments($args);
15+
}
16+
17+
public function run($command, array $args = [], $cwd = null, array $env = [], OutputInterface $output = null)
18+
{
19+
$proc = $this->build($command, $args)
20+
->setWorkingDirectory($cwd)
21+
->addEnvironmentVariables($env)
22+
->getProcess();
23+
24+
$callback = null;
25+
if (isset($output)) {
26+
$callback = function ($type, $buffer) use ($output) {
27+
$output->writeln($buffer);
28+
};
29+
}
30+
31+
$proc->run($callback);
32+
33+
return $proc;
34+
}
35+
}

0 commit comments

Comments
 (0)