diff --git a/src-symfony-compatibility/v6/Style/DrushStyle.php b/src-symfony-compatibility/v6/Style/DrushStyle.php index 32b8ad9d44..9ca5c48db4 100644 --- a/src-symfony-compatibility/v6/Style/DrushStyle.php +++ b/src-symfony-compatibility/v6/Style/DrushStyle.php @@ -2,6 +2,7 @@ namespace Drush\Style; +use Consolidation\Log\LogOutputStyler; use Drush\Drush; use JetBrains\PhpStorm\Deprecated; use Laravel\Prompts\MultiSearchPrompt; @@ -13,6 +14,9 @@ use Laravel\Prompts\Spinner; use Laravel\Prompts\SuggestPrompt; use Laravel\Prompts\TextPrompt; +use Psr\Log\LogLevel; +use Robo\Log\RoboLogLevel; +use Robo\Log\RoboLogStyle; use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; @@ -22,11 +26,6 @@ class DrushStyle extends SymfonyStyle { - public function success(array|string $message): void { - // Force output to stderr so as to not interfere with formatted output. - $this->getErrorStyle()->success($message); - } - public function confirm(string $question, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, ?\Closure $validate = null, string $hint = ''): bool { // Automatically accept confirmations if the --yes argument was supplied. @@ -175,19 +174,43 @@ public function progress(string $label, iterable|int $steps, ?\Closure $callback return $progress; } + public function comment(string|array $message): void + { + $this->message('', $message, '// '); + } + + public function success(string|array $message): void + { + $messages = \is_array($message) ? array_values($message) : [$message]; + foreach ($messages as $message) { + // Force output to stderr to not interfere with formatted output. + $this->getErrorOutput()->writeln($this->formatMessage(RoboLogLevel::SUCCESS, $message, RoboLogStyle::TASK_STYLE_SUCCESS)); + } + } + + public function error(string|array $message): void + { + $this->message(LogLevel::ERROR, $message, '', LogOutputStyler::TASK_STYLE_ERROR); + } + public function warning(string|array $message): void { - $this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ! ', true); + $this->message(LogLevel::WARNING, $message, '', LogOutputStyler::TASK_STYLE_WARNING); } public function note(string|array $message): void { - $this->block($message, 'NOTE', 'fg=black;bg=yellow', ' ! '); + $this->message(LogLevel::NOTICE, $message, '', LogOutputStyler::TASK_STYLE_INFO); + } + + public function info(string|array $message): void + { + $this->message(LogLevel::INFO, $message, '', LogOutputStyler::TASK_STYLE_INFO); } public function caution(string|array $message): void { - $this->block($message, 'CAUTION', 'fg=black;bg=yellow', ' ! ', true); + $this->message('caution', $message, '! ', LogOutputStyler::TASK_STYLE_ERROR); } /** @@ -208,4 +231,41 @@ public function askRequired($question) return $this->askQuestion($question); } + + /** + * Log a message with the provided label and message styles. + */ + protected function message(string $label, string|array $message, string $prefix, string $labelStyle = '', string $messageStyle = ''): void + { + $messages = \is_array($message) ? array_values($message) : [$message]; + foreach ($messages as $message) { + $this->writeln($this->formatMessage($label, $prefix . $message, $labelStyle, $messageStyle)); + } + } + + /** + * Apply styling with the provided label and message styles. + */ + protected function formatMessage(string $label, string|array $message, string $labelStyle = '', string $messageStyle = ''): string + { + if (!empty($messageStyle)) { + $message = $this->wrapFormatString(" $message ", $messageStyle); + } + if (!empty($label)) { + $message = ' ' . $this->wrapFormatString("[$label]", $labelStyle) . ' ' . $message; + } + + return $message; + } + + /** + * Wrap a string in a format element. + */ + protected function wrapFormatString(string $string, string $style): string + { + if ($style) { + return "<{$style}>$string"; + } + return $string; + } } diff --git a/tests/functional/DeployHookTest.php b/tests/functional/DeployHookTest.php index 297ab54471..e0f1824d4f 100644 --- a/tests/functional/DeployHookTest.php +++ b/tests/functional/DeployHookTest.php @@ -91,7 +91,7 @@ public function testSkipDeployHooks(): void // Mark them all as having run. $this->drush(DeployHookMarkCompleteCommand::NAME, [], [], null, null, self::EXIT_SUCCESS); - $this->assertStringContainsString('[OK] Marked 3 pending deploy hooks as complete.', $this->getErrorOutput()); + $this->assertStringContainsString('[success] Marked 3 pending deploy hooks as complete.', $this->getErrorOutput()); // Check again to see no pending hooks. $this->drush(DeployHookStatusCommand::NAME, [], $options, null, null, self::EXIT_SUCCESS); diff --git a/tests/functional/UpdateDBTest.php b/tests/functional/UpdateDBTest.php index bbe4be370e..aa82a09c61 100644 --- a/tests/functional/UpdateDBTest.php +++ b/tests/functional/UpdateDBTest.php @@ -27,7 +27,7 @@ public function testUpdateDBStatus(): void $this->drush(PmCommands::INSTALL, ['drush_empty_module']); $this->drush(UpdateDBStatusCommand::NAME); $err = $this->getErrorOutput(); - $this->assertStringContainsString('[OK] No database updates required.', $err); + $this->assertStringContainsString('[success] No database updates required.', $err); // Force a pending update. $this->drush(PhpCommands::SCRIPT, ['updatedb_script'], ['script-path' => __DIR__ . '/resources']); @@ -43,7 +43,7 @@ public function testUpdateDBStatus(): void // Assert that we ran hook_update_n properly $this->drush(UpdateDbStatusCommand::NAME); $err = $this->getErrorOutput(); - $this->assertStringContainsString('[OK] No database updates required.', $err); + $this->assertStringContainsString('[success] No database updates required.', $err); // Assure that a pending post-update is reported. $this->pathPostUpdate = Path::join($this->webroot(), 'modules/unish/drush_empty_module/drush_empty_module.post_update.php'); @@ -230,7 +230,7 @@ class: Drupal\woot\DependingService // Assert that the updates were run correctly. $this->drush(UpdateDBStatusCommand::NAME); $err = $this->getErrorOutput(); - $this->assertStringContainsString('[OK] No database updates required.', $err); + $this->assertStringContainsString('[success] No database updates required.', $err); } /**