1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Conveyor package. |
5
|
|
|
* |
6
|
|
|
* (c) Jeroen Fiege <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webcreate\Conveyor\Task; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
15
|
|
|
use Symfony\Component\Process\Process; |
16
|
|
|
|
17
|
|
|
use Webcreate\Conveyor\Task\Result\ExecuteResult; |
18
|
|
|
use Webcreate\Util\Cli; |
19
|
|
|
use Webcreate\Conveyor\IO\IOInterface; |
20
|
|
|
use Webcreate\Conveyor\Repository\Version; |
21
|
|
|
|
22
|
|
|
class ShellTask extends Task |
23
|
|
|
{ |
24
|
|
|
protected $cwd; |
25
|
|
|
protected $cli; |
26
|
|
|
protected $io; |
|
|
|
|
27
|
|
|
protected $commandline; |
28
|
|
|
|
29
|
2 |
|
public function __construct($cwd, Cli $cli, IOInterface $io = null) |
|
|
|
|
30
|
|
|
{ |
31
|
2 |
|
$this->cli = $cli; |
32
|
2 |
|
$this->io = $io; |
33
|
2 |
|
$this->cwd = $cwd; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @todo improve output (also @see SshTask) |
38
|
|
|
* |
39
|
|
|
* @param $target |
40
|
|
|
* @param Version $version |
41
|
|
|
* @return ExecuteResult |
42
|
|
|
* @throws \Symfony\Component\Process\Exception\ProcessFailedException |
43
|
|
|
*/ |
44
|
2 |
|
public function execute($target, Version $version = null) |
45
|
|
|
{ |
46
|
2 |
|
$this->commandline = $this->options['command']; |
47
|
2 |
|
foreach ($this->commandline as $command) { |
48
|
2 |
|
$this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
49
|
|
|
|
50
|
2 |
|
$hasOutput = false; |
51
|
|
|
|
52
|
2 |
|
$self = $this; |
53
|
|
View Code Duplication |
$outputter = function ($type, $buffer) use ($self, &$hasOutput) { |
|
|
|
|
54
|
|
|
if (false === $self->io->isVerbose()) return; |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (1 || 'out' === $type) { |
57
|
|
|
if (!$hasOutput) { |
58
|
|
|
$this->io->write(''); |
59
|
|
|
$this->io->write(''); |
60
|
|
|
$hasOutput = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$lines = explode("\n", $buffer); |
64
|
|
|
foreach ($lines as $line) { |
65
|
|
|
if ($output = trim($line, "\r\n")) { |
66
|
|
|
$self->io->write(sprintf('> %s', $output)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
2 |
|
}; |
71
|
|
|
|
72
|
2 |
|
if ($this->cli->execute($command, $outputter, $this->cwd) <> 0) { |
|
|
|
|
73
|
2 |
|
throw new ProcessFailedException($this->cli->getProcess()); |
74
|
|
|
} |
75
|
|
|
// elseif ($message = $this->cli->getErrorOutput()) { |
76
|
|
|
// $messages = explode("\n", $message); |
77
|
|
|
// |
78
|
|
|
// $self->io->write(sprintf('<comment>%s</comment>', $messages)); |
79
|
|
|
// } |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return new ExecuteResult( |
83
|
2 |
|
$this->getDerivedFiles(), |
84
|
2 |
|
$this->getRemovedFiles() |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
// return $this->cli->getOutput(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function simulate($target, Version $version) |
91
|
|
|
{ |
92
|
|
|
$this->commandline = $this->options['command']; |
93
|
|
|
foreach ($this->commandline as $command) { |
94
|
|
|
$this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
protected function getDerivedFiles() |
99
|
|
|
{ |
100
|
2 |
|
if (isset($this->options['creates'])) { |
101
|
|
|
return (array) $this->options['creates']; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
return array(); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
protected function getRemovedFiles() |
108
|
|
|
{ |
109
|
2 |
|
if (isset($this->options['removes'])) { |
110
|
|
|
return (array) $this->options['removes']; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return array(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.