5 use Robo\Common\ExecCommand;
6 use Robo\Contract\PrintedInterface;
8 use Robo\Contract\CommandInterface;
9 use Robo\Exception\TaskException;
11 abstract class CommandStack extends BaseTask implements CommandInterface, PrintedInterface
18 protected $executable;
30 protected $stopOnFail = false;
35 public function getCommand()
37 return implode(' && ', $this->exec);
41 * @param string $executable
45 public function executable($executable)
47 $this->executable = $executable;
52 * @param string|string[] $command
56 public function exec($command)
58 if (is_array($command)) {
59 $command = implode(' ', array_filter($command));
62 $command = $this->executable . ' ' . $this->stripExecutableFromCommand($command);
63 $this->exec[] = trim($command);
68 * @param bool $stopOnFail
72 public function stopOnFail($stopOnFail = true)
74 $this->stopOnFail = $stopOnFail;
78 public function result($result)
80 $this->result = $result;
85 * @param string $command
89 protected function stripExecutableFromCommand($command)
91 $command = trim($command);
92 $executable = $this->executable . ' ';
93 if (strpos($command, $executable) === 0) {
94 $command = substr($command, strlen($executable));
102 public function run()
104 if (empty($this->exec)) {
105 throw new TaskException($this, 'You must add at least one command');
107 // If 'stopOnFail' is not set, or if there is only one command to run,
108 // then execute the single command to run.
109 if (!$this->stopOnFail || (count($this->exec) == 1)) {
110 $this->printTaskInfo('{command}', ['command' => $this->getCommand()]);
111 return $this->executeCommand($this->getCommand());
114 // When executing multiple commands in 'stopOnFail' mode, run them
115 // one at a time so that the result will have the exact command
116 // that failed available to the caller. This is at the expense of
117 // losing the output from all successful commands.
121 foreach ($this->exec as $command) {
122 $this->printTaskInfo("Executing {command}", ['command' => $command]);
123 $result = $this->executeCommand($command);
124 $result->accumulateExecutionTime($data);
125 $message = $result->accumulateMessage($message);
126 $data = $result->mergeData($data);
127 if (!$result->wasSuccessful()) {