mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
add DifferAndFormatter
This commit is contained in:
parent
c54dad164f
commit
221042afed
@ -8,6 +8,7 @@
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.1",
|
||||
"gecko-packages/gecko-diff-output-builder": "^1.0",
|
||||
"klausi/yaml_comments": "0.x-dev",
|
||||
"nette/utils": "^2.4",
|
||||
"nikic/php-parser": "4.0.x-dev",
|
||||
@ -18,8 +19,7 @@
|
||||
"symfony/dependency-injection": "^4.0",
|
||||
"symfony/finder": "^4.0",
|
||||
"symplify/easy-coding-standard": "^3.0",
|
||||
"webmozart/assert": "^1.2",
|
||||
"friendsofphp/php-cs-fixer": "^2.9"
|
||||
"webmozart/assert": "^1.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"latte/latte": "^2.4",
|
||||
@ -38,6 +38,7 @@
|
||||
"psr-4": {
|
||||
"Rector\\": "src",
|
||||
"Rector\\BetterReflection\\": "packages/BetterReflection/src",
|
||||
"Rector\\ConsoleDiffer\\": "packages/ConsoleDiffer/src",
|
||||
"Rector\\ReflectionDocBlock\\": "packages/ReflectionDocBlock/src",
|
||||
"Rector\\NodeTypeResolver\\": "packages/NodeTypeResolver/src",
|
||||
"Rector\\NodeTraverserQueue\\": "packages/NodeTraverserQueue/src",
|
||||
|
@ -0,0 +1,55 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ConsoleDiffer\Console\Formatter;
|
||||
|
||||
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 $lineTemplate = '%s'): string
|
||||
{
|
||||
return sprintf(
|
||||
$this->template,
|
||||
implode(PHP_EOL, array_map(
|
||||
static function ($string) use ($lineTemplate) {
|
||||
$string = preg_replace(
|
||||
['/^(\+.*)/', '/^(\-.*)/', '/^(@.*)/'],
|
||||
['<fg=green>\1</fg=green>', '<fg=red>\1</fg=red>', '<fg=cyan>\1</fg=cyan>'],
|
||||
$string
|
||||
);
|
||||
|
||||
$templated = sprintf($lineTemplate, $string);
|
||||
|
||||
if ($string === ' ') {
|
||||
$templated = rtrim($templated);
|
||||
}
|
||||
|
||||
return $templated;
|
||||
},
|
||||
preg_split("#\n\r|\n#", OutputFormatter::escape(rtrim($diff)))
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
32
packages/ConsoleDiffer/src/DifferAndFormatter.php
Normal file
32
packages/ConsoleDiffer/src/DifferAndFormatter.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ConsoleDiffer;
|
||||
|
||||
use Rector\ConsoleDiffer\Console\Formatter\DiffConsoleFormatter;
|
||||
use SebastianBergmann\Diff\Differ;
|
||||
|
||||
final class DifferAndFormatter
|
||||
{
|
||||
/**
|
||||
* @var Differ
|
||||
*/
|
||||
private $differ;
|
||||
|
||||
/**
|
||||
* @var DiffConsoleFormatter
|
||||
*/
|
||||
private $diffConsoleFormatter;
|
||||
|
||||
public function __construct(Differ $differ, DiffConsoleFormatter $diffConsoleFormatter)
|
||||
{
|
||||
$this->differ = $differ;
|
||||
$this->diffConsoleFormatter = $diffConsoleFormatter;
|
||||
}
|
||||
|
||||
public function diffAndFormat(string $old, string $new): string
|
||||
{
|
||||
$diff = $this->differ->diff($old, $new);
|
||||
|
||||
return $this->diffConsoleFormatter->format($diff);
|
||||
}
|
||||
}
|
16
packages/ConsoleDiffer/src/config/services.yml
Normal file
16
packages/ConsoleDiffer/src/config/services.yml
Normal file
@ -0,0 +1,16 @@
|
||||
services:
|
||||
_defaults:
|
||||
autowire: true
|
||||
|
||||
Rector\ConsoleDiffer\:
|
||||
resource: ..
|
||||
|
||||
GeckoPackages\DiffOutputBuilder\UnifiedDiffOutputBuilder:
|
||||
arguments:
|
||||
$options:
|
||||
'fromFile': 'Original'
|
||||
'toFile': 'New'
|
||||
|
||||
SebastianBergmann\Diff\Differ:
|
||||
arguments:
|
||||
- '@GeckoPackages\DiffOutputBuilder\UnifiedDiffOutputBuilder'
|
@ -2,10 +2,10 @@
|
||||
|
||||
namespace Rector\Console\Command;
|
||||
|
||||
use PhpCsFixer\Differ\DiffConsoleFormatter;
|
||||
use PhpCsFixer\Differ\UnifiedDiffer;
|
||||
use Rector\Application\FileProcessor;
|
||||
use Rector\Console\Output\ProcessCommandReporter;
|
||||
use Rector\ConsoleDiffer\DifferAndFormatter;
|
||||
use Rector\Exception\NoRectorsLoadedException;
|
||||
use Rector\FileSystem\PhpFilesFinder;
|
||||
use Rector\Naming\CommandNaming;
|
||||
@ -61,6 +61,11 @@ final class ProcessCommand extends Command
|
||||
*/
|
||||
private $unifiedDiffer;
|
||||
|
||||
/**
|
||||
* @var DifferAndFormatter
|
||||
*/
|
||||
private $differAndFormatter;
|
||||
|
||||
public function __construct(
|
||||
FileProcessor $fileProcessor,
|
||||
RectorCollector $rectorCollector,
|
||||
@ -68,7 +73,7 @@ final class ProcessCommand extends Command
|
||||
PhpFilesFinder $phpFilesFinder,
|
||||
ProcessCommandReporter $processCommandReporter,
|
||||
ParameterProvider $parameterProvider,
|
||||
UnifiedDiffer $unifiedDiffer
|
||||
DifferAndFormatter $differAndFormatter
|
||||
) {
|
||||
$this->fileProcessor = $fileProcessor;
|
||||
$this->rectorCollector = $rectorCollector;
|
||||
@ -78,7 +83,7 @@ final class ProcessCommand extends Command
|
||||
|
||||
parent::__construct();
|
||||
$this->parameterProvider = $parameterProvider;
|
||||
$this->unifiedDiffer = $unifiedDiffer;
|
||||
$this->differAndFormatter = $differAndFormatter;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
@ -139,18 +144,8 @@ final class ProcessCommand extends Command
|
||||
$oldContent = $fileInfo->getContents();
|
||||
$newContent = $this->fileProcessor->processFileToString($fileInfo);
|
||||
|
||||
// @todo service?
|
||||
$diffConsoleFormatter = new DiffConsoleFormatter(true, sprintf(
|
||||
'<comment> ---------- begin diff ----------</comment>' .
|
||||
'%s%%s%s' .
|
||||
'<comment> ----------- end diff -----------</comment>',
|
||||
PHP_EOL,
|
||||
PHP_EOL
|
||||
));
|
||||
|
||||
if ($newContent !== $oldContent) {
|
||||
$diff = $this->unifiedDiffer->diff($oldContent, $newContent);
|
||||
$this->symfonyStyle->writeln($diffConsoleFormatter->format($diff));
|
||||
$this->symfonyStyle->writeln($this->differAndFormatter->diffAndFormat($oldContent, $newContent));
|
||||
}
|
||||
} else {
|
||||
$this->fileProcessor->processFile($fileInfo);
|
||||
|
Loading…
x
Reference in New Issue
Block a user