forked from jolicode/castor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SectionOutput.php
131 lines (102 loc) 路 4.24 KB
/
SectionOutput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Castor;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
/** @internal */
class SectionOutput
{
private const COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
private OutputInterface|ConsoleSectionOutput $consoleOutput;
private ?ConsoleOutput $mainOutput;
/** @var \SplObjectStorage<Process, SectionDetails> */
private \SplObjectStorage $sections;
public function __construct(OutputInterface $output)
{
$this->consoleOutput = $output;
$this->mainOutput = null;
$this->sections = new \SplObjectStorage();
if ($output instanceof ConsoleOutput && 'true' === getenv('CASTOR_USE_SECTION') && stream_isatty(\STDOUT)) {
$this->mainOutput = $output;
$this->consoleOutput = $output->section();
}
}
public function getConsoleOutput(): OutputInterface
{
return $this->consoleOutput;
}
public function writeProcessOutput(string $type, string $bytes, Process $process): void
{
if (!$this->mainOutput) {
if (Process::OUT === $type) {
fwrite(\STDOUT, $bytes);
} else {
fwrite(\STDERR, $bytes);
}
return;
}
$this->getSectionDetails($process)->section->write($bytes);
$this->tickProcess($process);
}
public function initProcess(Process $process): void
{
if (!$this->mainOutput) {
return;
}
$this->getSectionDetails($process);
}
public function finishProcess(Process $process): void
{
if (!$this->mainOutput) {
return;
}
$sectionDetails = $this->getSectionDetails($process);
$outputContent = $sectionDetails->section->getContent();
$time = number_format(microtime(true) - $sectionDetails->start, 2);
$fg = 0 === $process->getExitCode() ? 'green' : 'red';
$status = 0 === $process->getExitCode() ? 'success' : 'failure';
$color = self::COLORS[$sectionDetails->index % \count(self::COLORS)];
$this->consoleOutput->writeln("<bg={$color}> </>[{$sectionDetails->index}] <fg={$fg}>{$process->getCommandLine()}</> {$status} after {$time}s");
$this->consoleOutput->write($outputContent);
$sectionDetails->section->clear();
$sectionDetails->progressBarSection->clear();
}
public function tickProcess(Process $process): void
{
if (!$this->mainOutput) {
return;
}
$sectionDetails = $this->getSectionDetails($process);
$time = number_format(microtime(true) - $sectionDetails->start, 2);
$color = self::COLORS[$sectionDetails->index % \count(self::COLORS)];
$sectionDetails->progressBarSection->writeln("<bg={$color}> </>[{$sectionDetails->index}] <fg=yellow>{$process->getCommandLine()}</> running for {$time}s");
}
private function getSectionDetails(Process $process): SectionDetails
{
if (!$this->mainOutput) {
throw new \LogicException('Cannot call getSectionDetails() without a main output.');
}
if (!$this->sections->contains($process) || ('' === $this->sections[$process]->progressBarSection->getContent())) {
$progressBarSection = $this->mainOutput->section();
$section = $this->mainOutput->section();
$index = sprintf('%02d', \count($this->sections) + 1);
$color = self::COLORS[$index % \count(self::COLORS)];
$progressBarSection->writeln("<bg={$color}> </>[{$index}] <fg=yellow>{$process->getCommandLine()}</> starting...");
$progressBarSection->setDecorated(true);
$progressBarSection->setMaxHeight(1);
$this->sections[$process] = new SectionDetails($section, $progressBarSection, microtime(true), $index);
}
return $this->sections[$process];
}
}
class SectionDetails
{
public function __construct(
public ConsoleSectionOutput $section,
public ConsoleSectionOutput $progressBarSection,
public float $start,
public string $index,
) {
}
}