Skip to content

Commit

Permalink
Merge pull request #180 from localheinz/feature/diff
Browse files Browse the repository at this point in the history
Enhancement: Use StrictUnifiedDiffOutputBuilder when available
  • Loading branch information
localheinz authored Jun 1, 2019
2 parents 01d717e + ad311ae commit 68c0d86
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Unreleased

For a full diff see [`1.1.4...1.x`](https://github.com/localheinz/composer-normalize/compare/1.1.4...1.x).
For a full diff see [`1.2.0...1.x`](https://github.com/localheinz/composer-normalize/compare/1.2.0...1.x).

### [`1.2.0`](https://github.com/localheinz/composer-normalize/releases/tag/1.2.0)

For a full diff see [`1.1.4...1.2.0`](https://github.com/localheinz/composer-normalize/compare/1.1.4...1.2.0).

#### Changed

* Started using the `StrictUnifiedDiffOutputBuilder` when available to create more condensed diffs when using the `--dry-run` option ([#80](https://github.com/localheinz/composer-normalize/pull/180)), by [@localheinz](https://github.com/localheinz)

### [`1.1.4`](https://github.com/localheinz/composer-normalize/releases/tag/1.1.4)

Expand Down
81 changes: 52 additions & 29 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,26 @@ public function __construct(
$this->factory = $factory;
$this->normalizer = $normalizer;
$this->formatter = $formatter ?: new Normalizer\Format\Formatter();
$this->differ = $differ ?: new Diff\Differ();

if (null === $differ) {
$outputBuilder = null;

if (\class_exists(Diff\Output\StrictUnifiedDiffOutputBuilder::class)) {
$outputBuilder = new Diff\Output\StrictUnifiedDiffOutputBuilder([
'fromFile' => 'original',
'toFile' => 'normalized',
]);
} else {
$outputBuilder = new Diff\Output\UnifiedDiffOutputBuilder(\implode("\n", [
'--- original',
'+++ normalized',
]));
}

$differ = new Diff\Differ($outputBuilder);
}

$this->differ = $differ;
}

protected function configure(): void
Expand Down Expand Up @@ -204,22 +223,19 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
));

$io->write([
'',
'<fg=red>--- original </>',
'<fg=green>+++ normalized </>',
'',
'<fg=yellow>---------- begin diff ----------</>',
]);

$io->write(
$this->diff(
$json->encoded(),
$formatted->encoded()
),
false
);
$io->write($this->diff(
$json->encoded(),
$formatted->encoded()
));

$io->write('<fg=yellow>----------- end diff -----------</>');
$io->write([
'<fg=yellow>----------- end diff -----------</>',
'',
]);

return 1;
}
Expand Down Expand Up @@ -314,31 +330,38 @@ private function indentFrom(Console\Input\InputInterface $input): ?Normalizer\Fo
* @param string $before
* @param string $after
*
* @return string[]
* @return string
*/
private function diff(string $before, string $after): array
private function diff(string $before, string $after): string
{
$diff = $this->differ->diffToArray(
$diff = $this->differ->diff(
$before,
$after
);

return \array_map(static function (array $element) {
static $templates = [
0 => ' %s',
1 => '<fg=green>+%s</>',
2 => '<fg=red>-%s</>',
];

[$token, $status] = $element;

$template = $templates[$status];
$lines = \explode(
"\n",
$diff
);

return \sprintf(
$template,
$token
$formatted = \array_map(static function (string $line) {
return \preg_replace(
[
'/^(\+.*)$/',
'/^(-.*)$/',
],
[
'<fg=green>$1</>',
'<fg=red>$1</>',
],
$line
);
}, $diff);
}, $lines);

return \implode(
"\n",
$formatted
);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions test/Integration/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Test\Util\Helper;
use PHPUnit\Framework;
use SebastianBergmann\Diff\Differ;
use Symfony\Component\Console;
use Symfony\Component\Filesystem;

Expand Down Expand Up @@ -283,8 +282,7 @@ public function normalize(Json $json): Json
throw new \RuntimeException($this->exceptionMessage);
}
},
new Formatter(),
new Differ()
new Formatter()
));

$input = new Console\Input\ArrayInput($scenario->consoleParameters());
Expand Down Expand Up @@ -900,8 +898,7 @@ private static function createApplicationWithDefaultNormalizeCommand(): Applicat
return self::createApplication(new NormalizeCommand(
new Factory(),
new ComposerJsonNormalizer(),
new Formatter(),
new Differ()
new Formatter()
));
}

Expand Down

0 comments on commit 68c0d86

Please sign in to comment.