1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer\Executor; |
||
9 | |||
10 | use Deployer\Exception\Exception; |
||
11 | use Deployer\Exception\RunException; |
||
12 | use Deployer\Host\Host; |
||
13 | use Deployer\Task\Task; |
||
14 | use Symfony\Component\Console\Input\Input; |
||
15 | use Symfony\Component\Console\Output\Output; |
||
16 | use Throwable; |
||
17 | |||
18 | class Messenger |
||
19 | { |
||
20 | private $input; |
||
21 | private $output; |
||
22 | |||
23 | /** |
||
24 | * @var int|double |
||
25 | */ |
||
26 | private $startTime; |
||
27 | |||
28 | 12 | public function __construct(Input $input, Output $output) |
|
29 | { |
||
30 | 12 | $this->input = $input; |
|
31 | 12 | $this->output = $output; |
|
32 | 12 | } |
|
33 | |||
34 | 12 | public function startTask(Task $task) |
|
35 | { |
||
36 | 12 | $this->startTime = round(microtime(true) * 1000); |
|
37 | 12 | if (!$task->isShallow()) { |
|
38 | 12 | $this->output->writeln("<fg=cyan;options=bold>task</> {$task->getName()}"); |
|
39 | } |
||
40 | 12 | } |
|
41 | |||
42 | /* |
||
43 | * Print task was ok. |
||
44 | */ |
||
45 | 12 | public function endTask(Task $task) |
|
46 | { |
||
47 | 12 | if ($task->isShallow()) { |
|
48 | 4 | return; |
|
49 | } |
||
50 | 12 | if (empty($this->startTime)) { |
|
51 | $this->startTime = round(microtime(true) * 1000); |
||
52 | } |
||
53 | |||
54 | 12 | $endTime = round(microtime(true) * 1000); |
|
55 | 12 | $millis = $endTime - $this->startTime; |
|
56 | 12 | $seconds = floor($millis / 1000); |
|
57 | 12 | $millis = $millis - $seconds * 1000; |
|
58 | 12 | $taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms"; |
|
59 | |||
60 | 12 | if ($this->output->isVeryVerbose()) { |
|
61 | 1 | $this->output->writeln("<fg=yellow;options=bold>done</> {$task->getName()} $taskTime"); |
|
62 | } |
||
63 | |||
64 | 12 | if (!empty($this->input->getOption('profile'))) { |
|
65 | $line = sprintf("%s\t%s\n", $task->getName(), $taskTime); |
||
66 | file_put_contents($this->input->getOption('profile'), $line, FILE_APPEND); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
67 | |||
68 | } |
||
69 | 12 | } |
|
70 | |||
71 | 8 | public function endOnHost(Host $host) |
|
72 | { |
||
73 | 8 | if ($this->output->isVeryVerbose()) { |
|
74 | $this->output->writeln("[{$host->getTag()}] <info>ok</info>"); |
||
75 | } |
||
76 | 8 | } |
|
77 | |||
78 | 2 | public function renderException(Throwable $exception, Host $host) |
|
79 | { |
||
80 | 2 | if ($exception instanceof RunException) { |
|
81 | |||
82 | 1 | $message = ""; |
|
83 | 1 | $message .= "[{$host->getTag()}] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n"; |
|
84 | 1 | if ($this->output->getVerbosity() === Output::VERBOSITY_NORMAL) { |
|
85 | $message .= "[{$host->getTag()}] <fg=green;options=bold>run</> {$exception->getCommand()}\n"; |
||
86 | foreach (explode("\n", $exception->getErrorOutput()) as $line) { |
||
87 | $line = trim($line); |
||
88 | if ($line !== "") { |
||
89 | $message .= "[{$host->getTag()}] <fg=red>err</> $line\n"; |
||
90 | } |
||
91 | } |
||
92 | foreach (explode("\n", $exception->getOutput()) as $line) { |
||
93 | $line = trim($line); |
||
94 | if ($line !== "") { |
||
95 | $message .= "[{$host->getTag()}] $line\n"; |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | 1 | $message .= "[{$host->getTag()}] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n"; |
|
100 | 1 | $this->output->write($message); |
|
101 | |||
102 | } else { |
||
103 | 1 | $message = ""; |
|
104 | 1 | $class = get_class($exception); |
|
105 | 1 | $file = basename($exception->getFile()); |
|
106 | 1 | $line = $exception->getLine(); |
|
107 | 1 | if ($exception instanceof Exception) { |
|
108 | 1 | $file = $exception->getTaskFilename(); |
|
109 | 1 | $line = $exception->getTaskLineNumber(); |
|
110 | } |
||
111 | 1 | $message .= "[{$host->getTag()}] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n"; |
|
112 | 1 | $message .= "[{$host->getTag()}]\n"; |
|
113 | 1 | foreach (explode("\n", $exception->getMessage()) as $line) { |
|
114 | 1 | $line = trim($line); |
|
115 | 1 | if ($line !== "") { |
|
116 | $message .= "[{$host->getTag()}] <comment>$line</comment>\n"; |
||
117 | } |
||
118 | } |
||
119 | 1 | $message .= "[{$host->getTag()}]\n"; |
|
120 | 1 | if ($this->output->isDebug()) { |
|
121 | foreach (explode("\n", $exception->getTraceAsString()) as $line) { |
||
122 | $line = trim($line); |
||
123 | if ($line !== "") { |
||
124 | $message .= "[{$host->getTag()}] $line\n"; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | 1 | $this->output->write($message); |
|
129 | } |
||
130 | |||
131 | 2 | if ($exception->getPrevious()) { |
|
132 | $this->renderException($exception->getPrevious(), $host); |
||
133 | } |
||
134 | 2 | } |
|
135 | } |
||
136 |