add console-color-diff

This commit is contained in:
TomasVotruba 2020-05-05 16:43:01 +02:00
parent 73991b3153
commit 1be9176f75
11 changed files with 14 additions and 173 deletions

View File

@ -11,8 +11,9 @@
"symfony/process": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symplify/auto-bind-parameter": "^7.2",
"symplify/package-builder": "^7.2"
"symplify/console-color-diff": "^7.3",
"symplify/auto-bind-parameter": "^7.3",
"symplify/package-builder": "^7.3"
},
"autoload": {
"psr-4": {

View File

@ -13,10 +13,4 @@ services:
- '../src/Exception/*'
- '../src/HttpKernel/*'
Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory: null
Symfony\Component\Console\Style\SymfonyStyle:
factory: ['@Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory', 'create']
SebastianBergmann\Diff\Differ: null
Symfony\Component\Filesystem\Filesystem: null

View File

@ -6,8 +6,8 @@ namespace Rector\Compiler\Composer;
use Nette\Utils\FileSystem as NetteFileSystem;
use Nette\Utils\Json;
use Rector\Compiler\Differ\ConsoleDiffer;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer;
final class ComposerJsonManipulator
{

View File

@ -1,43 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Compiler\Differ;
use SebastianBergmann\Diff\Differ;
use Symfony\Component\Console\Style\SymfonyStyle;
final class ConsoleDiffer
{
/**
* @var Differ
*/
private $differ;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
/**
* @var ConsoleDifferFormatter
*/
private $consoleDifferFormatter;
public function __construct(
SymfonyStyle $symfonyStyle,
Differ $differ,
ConsoleDifferFormatter $consoleDifferFormatter
) {
$this->symfonyStyle = $symfonyStyle;
$this->differ = $differ;
$this->consoleDifferFormatter = $consoleDifferFormatter;
}
public function diff(string $old, string $new): void
{
$diff = $this->differ->diff($old, $new);
$consoleFormatted = $this->consoleDifferFormatter->format($diff);
$this->symfonyStyle->writeln($consoleFormatted);
}
}

View File

@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Compiler\Differ;
use Nette\Utils\Strings;
use Symfony\Component\Console\Formatter\OutputFormatter;
/**
* Mimics @see \Rector\ConsoleDiffer\Console\Formatter\DiffConsoleFormatter
*/
final class ConsoleDifferFormatter
{
/**
* @var string
*/
private $template;
public function __construct()
{
$this->template = sprintf(
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
PHP_EOL,
PHP_EOL
);
}
public function format(string $diff): string
{
return $this->formatWithTemplate($diff, $this->template);
}
private function formatWithTemplate(string $diff, string $template): string
{
return sprintf($template, implode(PHP_EOL, array_map(function (string $string): string {
// make "+" lines green
$string = Strings::replace($string, '#^(\+.*)#', '<fg=green>$1</fg=green>');
// make "-" lines red
$string = Strings::replace($string, '#^(\-.*)#', '<fg=red>$1</fg=red>');
// make "@ note" lines cyan
$string = Strings::replace($string, '#^(@.*)#', '<fg=cyan>$1</fg=cyan>');
if ($string === ' ') {
$string = rtrim($string);
}
return $string;
}, Strings::split(OutputFormatter::escape(rtrim($diff)), "#\n\r|\n#"))));
}
}

View File

@ -35,6 +35,7 @@
"symplify/autowire-array-parameter": "^7.3.5",
"symplify/package-builder": "^7.3.5",
"symplify/set-config-resolver": "^7.3.5",
"symplify/console-color-diff": "^7.3.5",
"tracy/tracy": "^2.7"
},
"require-dev": {

View File

@ -1,11 +1,3 @@
services:
Symplify\CodingStandard\Fixer\Order\MethodOrderByTypeFixer:
method_order_by_type:
Rector\Contract\Rector\PhpRectorInterface:
- 'getDefinition'
- 'getNodeTypes'
- 'refactor'
parameters:
paths:
- "bin"

View File

@ -25,7 +25,7 @@ services:
arguments:
- '@diffOutputBuilder'
# makrdown differ
# markdown differ
markdownDiffOutputBuilder:
class: SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder
factory: ['@Rector\ConsoleDiffer\Diff\Output\CompleteUnifiedDiffOutputBuilderFactory', 'create']

View File

@ -1,54 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\ConsoleDiffer\Console\Formatter;
use Nette\Utils\Strings;
use Symfony\Component\Console\Formatter\OutputFormatter;
/**
* Most is copy-pasted from 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.
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
final class DiffConsoleFormatter
{
/**
* @var string
*/
private $template;
public function __construct()
{
$this->template = sprintf(
'<comment> ---------- begin diff ----------</comment>%s%%s%s<comment> ----------- end diff -----------</comment>',
PHP_EOL,
PHP_EOL
);
}
public function format(string $diff): string
{
return $this->formatWithTemplate($diff, $this->template);
}
private function formatWithTemplate(string $diff, string $template): string
{
return sprintf($template, implode(PHP_EOL, array_map(function (string $string): string {
// make "+" lines green
$string = Strings::replace($string, '#^(\+.*)#', '<fg=green>$1</fg=green>');
// make "-" lines red
$string = Strings::replace($string, '#^(\-.*)#', '<fg=red>$1</fg=red>');
// make "@ note" lines cyan
$string = Strings::replace($string, '#^(@.*)#', '<fg=cyan>$1</fg=cyan>');
if ($string === ' ') {
$string = rtrim($string);
}
return $string;
}, Strings::split(OutputFormatter::escape(rtrim($diff)), "#\n\r|\n#"))));
}
}

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Rector\ConsoleDiffer;
use Rector\ConsoleDiffer\Console\Formatter\DiffConsoleFormatter;
use SebastianBergmann\Diff\Differ;
use Symplify\ConsoleColorDiff\Console\Formatter\ColorConsoleDiffFormatter;
final class DifferAndFormatter
{
@ -15,14 +15,14 @@ final class DifferAndFormatter
private $differ;
/**
* @var DiffConsoleFormatter
* @var ColorConsoleDiffFormatter
*/
private $diffConsoleFormatter;
private $colorConsoleDiffFormatter;
public function __construct(Differ $differ, DiffConsoleFormatter $diffConsoleFormatter)
public function __construct(Differ $differ, ColorConsoleDiffFormatter $colorConsoleDiffFormatter)
{
$this->differ = $differ;
$this->diffConsoleFormatter = $diffConsoleFormatter;
$this->colorConsoleDiffFormatter = $colorConsoleDiffFormatter;
}
public function diff(string $old, string $new): string
@ -42,6 +42,6 @@ final class DifferAndFormatter
$diff = $this->diff($old, $new);
return $this->diffConsoleFormatter->format($diff);
return $this->colorConsoleDiffFormatter->format($diff);
}
}

View File

@ -20,6 +20,7 @@ use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\Kernel;
use Symplify\AutoBindParameter\DependencyInjection\CompilerPass\AutoBindParameterCompilerPass;
use Symplify\AutowireArrayParameter\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass;
use Symplify\ConsoleColorDiff\ConsoleColorDiffBundle;
use Symplify\PackageBuilder\Contract\HttpKernel\ExtraConfigAwareKernelInterface;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutoReturnFactoryCompilerPass;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireInterfacesCompilerPass;
@ -77,7 +78,7 @@ final class RectorKernel extends Kernel implements ExtraConfigAwareKernelInterfa
*/
public function registerBundles(): array
{
return [];
return [new ConsoleColorDiffBundle()];
}
protected function build(ContainerBuilder $containerBuilder): void