-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from siketyan/feat/console-reporter
feat: Introduce console reporter, enabled by default
- Loading branch information
Showing
14 changed files
with
404 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Siketyan\Loxcan\Exception; | ||
|
||
class InvalidReporterException extends RuntimeException | ||
{ | ||
public function __construct(string $name, int $code = 0, ?\Throwable $previous = null) | ||
{ | ||
parent::__construct( | ||
sprintf('The reporter "%s" is not valid.', $name), | ||
$code, | ||
$previous, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Siketyan\Loxcan\Reporter\Console; | ||
|
||
use JetBrains\PhpStorm\Pure; | ||
use Siketyan\Loxcan\Reporter\MarkdownBuilder; | ||
use Siketyan\Loxcan\Reporter\ReporterInterface; | ||
use Siketyan\Loxcan\Versioning\VersionDiff; | ||
use Symfony\Component\Console\Color; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class ConsoleReporter implements ReporterInterface | ||
{ | ||
public const CONTEXT_SYMFONY_IO = 'console.symfony-style'; | ||
public const CONTEXT_FLAVOR = 'console.flavor'; | ||
|
||
public const FLAVOR_MARKDOWN = 'markdown'; | ||
|
||
public function __construct( | ||
private readonly MarkdownBuilder $markdownBuilder, | ||
) { | ||
} | ||
|
||
public function report(array $diffs, array $context = []): void | ||
{ | ||
$io = $context[self::CONTEXT_SYMFONY_IO] ?? null; | ||
\assert($io instanceof SymfonyStyle); | ||
|
||
if ($diffs === []) { | ||
$io->writeln( | ||
'✨ No lock file changes found, looks shine!', | ||
); | ||
|
||
return; | ||
} | ||
|
||
if (($context[self::CONTEXT_FLAVOR] ?? null) === self::FLAVOR_MARKDOWN) { | ||
echo $this->markdownBuilder->build($diffs) . \PHP_EOL; | ||
|
||
return; | ||
} | ||
|
||
foreach ($diffs as $file => $diff) { | ||
$io->section($file); | ||
|
||
if ($diff->count() === 0) { | ||
$io->writeln( | ||
'🔄 The file was updated, but no dependency changes found.', | ||
); | ||
|
||
continue; | ||
} | ||
|
||
$rows = []; | ||
|
||
foreach ($diff->getAdded() as $dependency) { | ||
$rows[] = [ | ||
'➕', | ||
$dependency->getPackage()->getName(), | ||
'', | ||
$dependency->getVersion(), | ||
]; | ||
} | ||
|
||
foreach ($diff->getUpdated() as $dependencyDiff) { | ||
$versionDiff = $dependencyDiff->getVersionDiff(); | ||
$rows[] = [ | ||
$this->getVersionDiffTypeEmoji($versionDiff), | ||
$this->emphasizeBreakingChanges($versionDiff, $dependencyDiff->getPackage()->getName()), | ||
$this->emphasizeBreakingChanges($versionDiff, (string) $versionDiff->getBefore()), | ||
$this->emphasizeBreakingChanges($versionDiff, (string) $versionDiff->getAfter()), | ||
]; | ||
} | ||
|
||
foreach ($diff->getRemoved() as $dependency) { | ||
$rows[] = [ | ||
'➖', | ||
$dependency->getPackage()->getName(), | ||
$dependency->getVersion(), | ||
'', | ||
]; | ||
} | ||
|
||
$io->table( | ||
['', 'Package', 'Before', 'After'], | ||
$rows, | ||
); | ||
} | ||
} | ||
|
||
#[Pure] | ||
private function getVersionDiffTypeEmoji(VersionDiff $diff): string | ||
{ | ||
switch ($diff->getType()) { | ||
case VersionDiff::UPGRADED: | ||
return '⬆️'; | ||
|
||
case VersionDiff::DOWNGRADED: | ||
return '⬇️'; | ||
|
||
case VersionDiff::CHANGED: | ||
return '💥'; | ||
|
||
default: | ||
case VersionDiff::UNKNOWN: | ||
return '🔄'; | ||
} | ||
} | ||
|
||
private function emphasizeBreakingChanges(VersionDiff $diff, string $str): string | ||
{ | ||
$emphasize = new Color('bright-white', '', ['bold']); | ||
|
||
if (!$diff->isCompatible()) { | ||
return $emphasize->apply($str); | ||
} | ||
|
||
return $str; | ||
} | ||
|
||
public function supports(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return 'console'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.