-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] Move color differ here (#2887)
- Loading branch information
1 parent
5a28970
commit 88b75c6
Showing
14 changed files
with
222 additions
and
9 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
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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Console\Formatter; | ||
|
||
use Nette\Utils\Strings; | ||
use Symfony\Component\Console\Formatter\OutputFormatter; | ||
|
||
/** | ||
* Inspired by @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/src/Differ/DiffConsoleFormatter.php to be | ||
* used as standalone class, without need to require whole package by Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* @see \Rector\Core\Tests\Console\Formatter\ColorConsoleDiffFormatterTest | ||
*/ | ||
final class ColorConsoleDiffFormatter | ||
{ | ||
/** | ||
* @var string | ||
* @see https://regex101.com/r/ovLMDF/1 | ||
*/ | ||
private const PLUS_START_REGEX = '#^(\+.*)#'; | ||
|
||
/** | ||
* @var string | ||
* @see https://regex101.com/r/xwywpa/1 | ||
*/ | ||
private const MINUT_START_REGEX = '#^(\-.*)#'; | ||
|
||
/** | ||
* @var string | ||
* @see https://regex101.com/r/CMlwa8/1 | ||
*/ | ||
private const AT_START_REGEX = '#^(@.*)#'; | ||
|
||
/** | ||
* @var string | ||
* @see https://regex101.com/r/qduj2O/1 | ||
*/ | ||
private const NEWLINES_REGEX = "#\n\r|\n#"; | ||
|
||
private readonly string $template; | ||
|
||
public function __construct() | ||
{ | ||
$this->template = sprintf( | ||
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>' . PHP_EOL, | ||
PHP_EOL, | ||
PHP_EOL | ||
); | ||
} | ||
|
||
public function format(string $diff): string | ||
{ | ||
return $this->formatWithTemplate($diff, $this->template); | ||
} | ||
|
||
private function formatWithTemplate(string $diff, string $template): string | ||
{ | ||
$escapedDiff = OutputFormatter::escape(rtrim($diff)); | ||
|
||
$escapedDiffLines = Strings::split($escapedDiff, self::NEWLINES_REGEX); | ||
|
||
// remove description of added + remove; obvious on diffs | ||
foreach ($escapedDiffLines as $key => $escapedDiffLine) { | ||
if ($escapedDiffLine === '--- Original') { | ||
unset($escapedDiffLines[$key]); | ||
} | ||
|
||
if ($escapedDiffLine === '+++ New') { | ||
unset($escapedDiffLines[$key]); | ||
} | ||
} | ||
|
||
$coloredLines = array_map(function (string $string): string { | ||
$string = $this->makePlusLinesGreen($string); | ||
$string = $this->makeMinusLinesRed($string); | ||
$string = $this->makeAtNoteCyan($string); | ||
|
||
if ($string === ' ') { | ||
return ''; | ||
} | ||
|
||
return $string; | ||
}, $escapedDiffLines); | ||
|
||
return sprintf($template, implode(PHP_EOL, $coloredLines)); | ||
} | ||
|
||
private function makePlusLinesGreen(string $string): string | ||
{ | ||
return Strings::replace($string, self::PLUS_START_REGEX, '<fg=green>$1</fg=green>'); | ||
} | ||
|
||
private function makeMinusLinesRed(string $string): string | ||
{ | ||
return Strings::replace($string, self::MINUT_START_REGEX, '<fg=red>$1</fg=red>'); | ||
} | ||
|
||
private function makeAtNoteCyan(string $string): string | ||
{ | ||
return Strings::replace($string, self::AT_START_REGEX, '<fg=cyan>$1</fg=cyan>'); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Console/Formatter/CompleteUnifiedDiffOutputBuilderFactory.php
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Console\Formatter; | ||
|
||
use Rector\Core\Util\Reflection\PrivatesAccessor; | ||
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; | ||
|
||
/** | ||
* @api | ||
* Creates @see UnifiedDiffOutputBuilder with "$contextLines = 1000;" | ||
*/ | ||
final class CompleteUnifiedDiffOutputBuilderFactory | ||
{ | ||
public function __construct( | ||
private readonly PrivatesAccessor $privatesAccessor | ||
) { | ||
} | ||
|
||
/** | ||
* @api | ||
*/ | ||
public function create(): UnifiedDiffOutputBuilder | ||
{ | ||
$unifiedDiffOutputBuilder = new UnifiedDiffOutputBuilder(''); | ||
$this->privatesAccessor->setPrivateProperty($unifiedDiffOutputBuilder, 'contextLines', 10000); | ||
return $unifiedDiffOutputBuilder; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Console\Formatter; | ||
|
||
use SebastianBergmann\Diff\Differ; | ||
|
||
final class ConsoleDiffer | ||
{ | ||
public function __construct( | ||
private readonly Differ $differ, | ||
private readonly ColorConsoleDiffFormatter $colorConsoleDiffFormatter | ||
) { | ||
} | ||
|
||
public function diff(string $old, string $new): string | ||
{ | ||
$diff = $this->differ->diff($old, $new); | ||
return $this->colorConsoleDiffFormatter->format($diff); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Tests\Console\Formatter; | ||
|
||
use Iterator; | ||
use Nette\Utils\FileSystem; | ||
use PHPUnit\Framework\TestCase; | ||
use Rector\Core\Console\Formatter\ColorConsoleDiffFormatter; | ||
|
||
final class ColorConsoleDiffFormatterTest extends TestCase | ||
{ | ||
private ColorConsoleDiffFormatter $colorConsoleDiffFormatter; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->colorConsoleDiffFormatter = new ColorConsoleDiffFormatter(); | ||
} | ||
|
||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $content, string $expectedFormatedFileContent): void | ||
{ | ||
$formattedContent = $this->colorConsoleDiffFormatter->format($content); | ||
|
||
$this->assertStringEqualsFile($expectedFormatedFileContent, $formattedContent); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
yield ['...', __DIR__ . '/Source/expected/expected.txt']; | ||
yield ["-old\n+new", __DIR__ . '/Source/expected/expected_old_new.txt']; | ||
|
||
yield [ | ||
FileSystem::read(__DIR__ . '/Fixture/with_full_diff_by_phpunit.diff'), | ||
__DIR__ . '/Fixture/expected_with_full_diff_by_phpunit.diff', | ||
]; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/Console/Formatter/Fixture/expected_with_full_diff_by_phpunit.diff
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,3 @@ | ||
<comment> ---------- begin diff ----------</comment> | ||
... | ||
<comment> ----------- end diff -----------</comment> |
3 changes: 3 additions & 0 deletions
3
tests/Console/Formatter/Fixture/with_full_diff_by_phpunit.diff
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,3 @@ | ||
--- Original | ||
+++ New | ||
... |
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,3 @@ | ||
<comment> ---------- begin diff ----------</comment> | ||
... | ||
<comment> ----------- end diff -----------</comment> |
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,4 @@ | ||
<comment> ---------- begin diff ----------</comment> | ||
<fg=red>-old</fg=red> | ||
<fg=green>+new</fg=green> | ||
<comment> ----------- end diff -----------</comment> |
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