Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 68 additions & 8 deletions src-symfony-compatibility/v6/Style/DrushStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drush\Style;

use Consolidation\Log\LogOutputStyler;
use Drush\Drush;
use JetBrains\PhpStorm\Deprecated;
use Laravel\Prompts\MultiSearchPrompt;
Expand All @@ -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;

Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/functional/DeployHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/UpdateDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
}

/**
Expand Down