Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add --dry-run option #38

Merged
merged 2 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil

### Options

* `--dry-run`: Show the results of normalizing, but do not modify any files
* `--indent-size`: Indent size (an integer greater than 0); should be used with the `--indent-style` option
* `--indent-style`: Indent style (one of "space", "tab"); should be used with the `--indent-size` option
* `--no-update-lock`: Do not update lock file if it exists
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "^7.1",
"composer-plugin-api": "^1.1.0",
"localheinz/json-normalizer": "0.4.0"
"localheinz/json-normalizer": "0.4.0",
"sebastian/diff": "^2.0.1"
},
"require-dev": {
"composer/composer": "^1.1.0",
Expand Down
106 changes: 53 additions & 53 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 72 additions & 1 deletion src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Composer\Command;
use Composer\Factory;
use Localheinz\Json\Normalizer;
use SebastianBergmann\Diff;
use Symfony\Component\Console;

final class NormalizeCommand extends Command\BaseCommand
Expand Down Expand Up @@ -43,22 +44,35 @@ final class NormalizeCommand extends Command\BaseCommand
*/
private $formatter;

/**
* @var Diff\Differ
*/
private $differ;

public function __construct(
Normalizer\NormalizerInterface $normalizer,
Normalizer\Format\SnifferInterface $sniffer = null,
Normalizer\Format\FormatterInterface $formatter = null
Normalizer\Format\FormatterInterface $formatter = null,
Diff\Differ $differ = null
) {
parent::__construct('normalize');

$this->normalizer = $normalizer;
$this->sniffer = $sniffer ?: new Normalizer\Format\Sniffer();
$this->formatter = $formatter ?: new Normalizer\Format\Formatter();
$this->differ = $differ ?: new Diff\Differ();
}

protected function configure(): void
{
$this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).');
$this->setDefinition([
new Console\Input\InputOption(
'dry-run',
null,
Console\Input\InputOption::VALUE_NONE,
'Show the results of normalizing, but do not modify any files'
),
new Console\Input\InputOption(
'indent-size',
null,
Expand Down Expand Up @@ -87,6 +101,8 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
{
$io = $this->getIO();

$dryRun = $input->getOption('dry-run');

try {
$indent = $this->indentFrom($input);
} catch (\RuntimeException $exception) {
Expand Down Expand Up @@ -157,6 +173,30 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 0;
}

if ($dryRun) {
$io->writeError(\sprintf(
'<error>%s is not normalized.</error>',
$composerFile
));

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

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

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

return 1;
}

\file_put_contents($composerFile, $formatted);

$io->write(\sprintf(
Expand Down Expand Up @@ -256,6 +296,37 @@ private function composerFile(): string
return $composerFile;
}

/**
* @param string $before
* @param string $after
*
* @return string[]
*/
private function diff(string $before, string $after): array
{
$diff = $this->differ->diffToArray(
$before,
$after
);

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

[$token, $status] = $element;

$template = $templates[$status];

return \sprintf(
$template,
$token
);
}, $diff);
}

/**
* @see https://getcomposer.org/doc/03-cli.md#validate
*
Expand Down
Loading