diff --git a/resources/config/services.yml b/resources/config/services.yml index d4491fac..345499b6 100644 --- a/resources/config/services.yml +++ b/resources/config/services.yml @@ -37,15 +37,15 @@ services: logger: '@grumphp.logger' inherit_environment_variables: true - grumphp.io: - alias: grumphp.io.console + GrumPHP\IO\IOFactory: + arguments: + - '@OndraM\CiDetector\CiDetector' - grumphp.io.console: - class: GrumPHP\IO\ConsoleIO + grumphp.io: + factory: ['@GrumPHP\IO\IOFactory', 'create'] arguments: - - '@console.input' - - '@console.output' - public: true + - '@console.input' + - '@console.output' grumphp.logger: class: Monolog\Logger diff --git a/src/Console/Command/Git/CommitMsgCommand.php b/src/Console/Command/Git/CommitMsgCommand.php index 13a12330..dc55d939 100644 --- a/src/Console/Command/Git/CommitMsgCommand.php +++ b/src/Console/Command/Git/CommitMsgCommand.php @@ -6,7 +6,8 @@ use GrumPHP\Collection\FilesCollection; use GrumPHP\Collection\TestSuiteCollection; -use GrumPHP\IO\ConsoleIO; +use GrumPHP\IO\IOFactory; +use GrumPHP\IO\IOInterface; use GrumPHP\Locator\ChangedFiles; use GrumPHP\Locator\StdInFiles; use GrumPHP\Runner\TaskRunner; @@ -60,13 +61,16 @@ class CommitMsgCommand extends Command */ private $paths; + private IOFactory $IOFactory; + public function __construct( TestSuiteCollection $testSuites, StdInFiles $stdInFilesLocator, ChangedFiles $changedFilesLocator, TaskRunner $taskRunner, Filesystem $filesystem, - Paths $paths + Paths $paths, + IOFactory $IOFactory ) { parent::__construct(); @@ -76,6 +80,7 @@ public function __construct( $this->filesystem = $filesystem; $this->paths = $paths; $this->stdInFilesLocator = $stdInFilesLocator; + $this->IOFactory = $IOFactory; } public static function getDefaultName(): string @@ -93,7 +98,7 @@ protected function configure(): void public function execute(InputInterface $input, OutputInterface $output): int { - $io = new ConsoleIO($input, $output); + $io = $this->IOFactory->create($input, $output); $files = $this->getCommittedFiles($io); /** @var string $gitUser */ @@ -124,7 +129,7 @@ public function execute(InputInterface $input, OutputInterface $output): int return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK; } - protected function getCommittedFiles(ConsoleIO $io): FilesCollection + protected function getCommittedFiles(IOInterface $io): FilesCollection { if ($stdin = $io->readCommandInput(STDIN)) { return $this->stdInFilesLocator->locate($stdin); diff --git a/src/Console/Command/Git/PreCommitCommand.php b/src/Console/Command/Git/PreCommitCommand.php index c831be46..a0a399d3 100644 --- a/src/Console/Command/Git/PreCommitCommand.php +++ b/src/Console/Command/Git/PreCommitCommand.php @@ -6,7 +6,8 @@ use GrumPHP\Collection\FilesCollection; use GrumPHP\Collection\TestSuiteCollection; -use GrumPHP\IO\ConsoleIO; +use GrumPHP\IO\IOFactory; +use GrumPHP\IO\IOInterface; use GrumPHP\Locator\ChangedFiles; use GrumPHP\Locator\StdInFiles; use GrumPHP\Runner\TaskRunner; @@ -46,11 +47,14 @@ class PreCommitCommand extends Command */ private $taskRunner; + private IOFactory $IOFactory; + public function __construct( TestSuiteCollection $testSuites, StdInFiles $stdInFilesLocator, ChangedFiles $changedFilesLocator, - TaskRunner $taskRunner + TaskRunner $taskRunner, + IOFactory $IOFactory ) { parent::__construct(); @@ -58,6 +62,7 @@ public function __construct( $this->stdInFilesLocator = $stdInFilesLocator; $this->changedFilesLocator = $changedFilesLocator; $this->taskRunner = $taskRunner; + $this->IOFactory = $IOFactory; } public static function getDefaultName(): string @@ -78,7 +83,7 @@ protected function configure(): void public function execute(InputInterface $input, OutputInterface $output): int { - $io = new ConsoleIO($input, $output); + $io = $this->IOFactory->create($input, $output); $files = $this->getCommittedFiles($io); $context = ( @@ -95,7 +100,7 @@ public function execute(InputInterface $input, OutputInterface $output): int return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK; } - protected function getCommittedFiles(ConsoleIO $io): FilesCollection + protected function getCommittedFiles(IOInterface $io): FilesCollection { if ($stdin = $io->readCommandInput(STDIN)) { return $this->stdInFilesLocator->locate($stdin); diff --git a/src/Console/Command/RunCommand.php b/src/Console/Command/RunCommand.php index 2a71732e..f718d2e4 100644 --- a/src/Console/Command/RunCommand.php +++ b/src/Console/Command/RunCommand.php @@ -6,7 +6,8 @@ use GrumPHP\Collection\FilesCollection; use GrumPHP\Collection\TestSuiteCollection; -use GrumPHP\IO\ConsoleIO; +use GrumPHP\IO\IOFactory; +use GrumPHP\IO\IOInterface; use GrumPHP\Locator\RegisteredFiles; use GrumPHP\Locator\StdInFiles; use GrumPHP\Runner\TaskRunner; @@ -44,11 +45,14 @@ class RunCommand extends Command */ private $taskRunner; + private IOFactory $IOFactory; + public function __construct( TestSuiteCollection $testSuites, StdInFiles $stdInFileLocator, RegisteredFiles $registeredFilesLocator, - TaskRunner $taskRunner + TaskRunner $taskRunner, + IOFactory $IOFactory ) { parent::__construct(); @@ -56,6 +60,7 @@ public function __construct( $this->stdInFileLocator = $stdInFileLocator; $this->registeredFilesLocator = $registeredFilesLocator; $this->taskRunner = $taskRunner; + $this->IOFactory = $IOFactory; } public static function getDefaultName(): string @@ -83,7 +88,7 @@ protected function configure(): void public function execute(InputInterface $input, OutputInterface $output): int { - $io = new ConsoleIO($input, $output); + $io = $this->IOFactory->create($input, $output); /** @var string $taskNames */ $taskNames = $input->getOption('tasks') ?? ''; @@ -107,7 +112,7 @@ public function execute(InputInterface $input, OutputInterface $output): int return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK; } - private function detectFiles(ConsoleIO $io): FilesCollection + private function detectFiles(IOInterface $io): FilesCollection { if ($stdin = $io->readCommandInput(STDIN)) { return $this->stdInFileLocator->locate($stdin); diff --git a/src/IO/ConsoleIO.php b/src/IO/ConsoleIO.php index 65dcc9a2..ed9618ad 100644 --- a/src/IO/ConsoleIO.php +++ b/src/IO/ConsoleIO.php @@ -139,6 +139,15 @@ static function (string $message) use ($color) : string { ); } + public function startGroup(string $title): void + { + $this->style()->title($title); + } + + public function endGroup(): void + { + } + /** * Serializing this IO will result in an unwritable resource stream. * Therefor we serialize the data end build up a new stream instead. diff --git a/src/IO/GitHubActionsIO.php b/src/IO/GitHubActionsIO.php new file mode 100644 index 00000000..84875d37 --- /dev/null +++ b/src/IO/GitHubActionsIO.php @@ -0,0 +1,20 @@ +write('::group::' . $title); + parent::startGroup($title); + } + + public function endGroup(): void + { + parent::endGroup(); + $this->write('::endgroup::'); + } +} diff --git a/src/IO/IOFactory.php b/src/IO/IOFactory.php new file mode 100644 index 00000000..2c0e5632 --- /dev/null +++ b/src/IO/IOFactory.php @@ -0,0 +1,27 @@ +ciDetector->isCiDetected()) { + $platform = $this->ciDetector->detect(); + + if ($platform instanceof GitHubActions) { + return new GitHubActionsIO($input, $output); + } + } + + return new ConsoleIO($input, $output); + } +} diff --git a/src/IO/IOInterface.php b/src/IO/IOInterface.php index 02b883fb..089ffe08 100644 --- a/src/IO/IOInterface.php +++ b/src/IO/IOInterface.php @@ -34,4 +34,8 @@ public function style(): StyleInterface; public function section(): ConsoleSectionOutput; public function colorize(array $messages, string $color): array; + + public function startGroup(string $title): void; + + public function endGroup(): void; } diff --git a/src/Runner/Middleware/GroupByPriorityMiddleware.php b/src/Runner/Middleware/GroupByPriorityMiddleware.php index ebcfe64f..ad1932ad 100644 --- a/src/Runner/Middleware/GroupByPriorityMiddleware.php +++ b/src/Runner/Middleware/GroupByPriorityMiddleware.php @@ -35,11 +35,12 @@ public function handle(TaskRunnerContext $context, callable $next): TaskResultCo ->groupByPriority(); foreach ($grouped as $priority => $tasks) { - $this->IO->style()->title('Running tasks with priority '.$priority.'!'); + $this->IO->startGroup('Running tasks with priority '.$priority.'!'); $results = new TaskResultCollection(array_merge( $results->toArray(), $next($context->withTasks($tasks))->toArray() )); + $this->IO->endGroup(); // Stop on failure: if ($this->config->stopOnFailure() && $results->isFailed()) { diff --git a/src/Runner/Reporting/RunnerReporter.php b/src/Runner/Reporting/RunnerReporter.php index aac0c81d..dedfc297 100644 --- a/src/Runner/Reporting/RunnerReporter.php +++ b/src/Runner/Reporting/RunnerReporter.php @@ -124,8 +124,9 @@ private function reportWarningMessages(array $warningMessages): void private function reportFailedMessages(array $messages, string $color): void { foreach ($messages as $label => $message) { - $this->IO->style()->title($label); + $this->IO->startGroup($label); $this->IO->writeError($this->IO->colorize([$message], $color)); + $this->IO->endGroup(); } }