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 --indent-size and --indent-style options #35

Merged
merged 1 commit into from
Jan 27, 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil
* determine whether a `composer.json` exists
* determine whether a `composer.lock` exists, and if so, whether it is up to date
* use the `ComposerJsonNormalizer` to normalize the content of `composer.json`
* write the normalized content of `composer.json` back to the file
* format the normalized content (either as sniffed, or as specified using the `--indent-size` and `--indent-style` options)
* write the normalized and formatted content of `composer.json` back to the file
* update the hash in `composer.lock` if it exists and if an update is necessary

### Options

* `--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

## Normalizers

The `ComposerJsonNormalizer` composes normalizers provided by [`localheinz/json-normalizer`](https://github.com/localheinz/json-normalizer):

* [`Localheinz\Json\Normalizer\AutoFormatNormalizer`](https://github.com/localheinz/json-normalizer#autoformatnormalizer)
* [`Localheinz\Json\Normalizer\ChainNormalizer`](https://github.com/localheinz/json-normalizer#chainnormalizer)
* [`Localheinz\Json\Normalizer\SchemaNormalizer`](https://github.com/localheinz/json-normalizer#schemanormalizer)

Expand Down
107 changes: 101 additions & 6 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,60 @@

final class NormalizeCommand extends Command\BaseCommand
{
/**
* @var array
*/
private static $indentStyles = [
'space' => ' ',
'tab' => "\t",
];

/**
* @var Normalizer\NormalizerInterface
*/
private $normalizer;

public function __construct(Normalizer\NormalizerInterface $normalizer)
{
/**
* @var Normalizer\Format\SnifferInterface
*/
private $sniffer;

/**
* @var Normalizer\Format\FormatterInterface
*/
private $formatter;

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

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

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(
'indent-size',
null,
Console\Input\InputOption::VALUE_REQUIRED,
'Indent size (an integer greater than 0); should be used with the --indent-style option'
),
new Console\Input\InputOption(
'indent-style',
null,
Console\Input\InputOption::VALUE_REQUIRED,
\sprintf(
'Indent style (one of "%s"); should be used with the --indent-size option',
\implode('", "', \array_keys(self::$indentStyles))
)
),
new Console\Input\InputOption(
'no-update-lock',
null,
Expand All @@ -47,10 +85,56 @@ protected function configure(): void

protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
$file = Factory::getComposerFile();

$io = $this->getIO();

$indent = null;

$indentSize = $input->getOption('indent-size');
$indentStyle = $input->getOption('indent-style');

if (null !== $indentSize || null !== $indentStyle) {
if (null === $indentSize) {
$io->writeError('<error>When using the indent-style option, an indent size needs to be specified using the indent-size option.</error>');

return 1;
}

if (null === $indentStyle) {
$io->writeError(\sprintf(
'<error>When using the indent-size option, an indent style (one of "%s") needs to be specified using the indent-style option.</error>',
\implode('", "', \array_keys(self::$indentStyles))
));

return 1;
}

if ((string) (int) $indentSize !== (string) $indentSize || 1 > $indentSize) {
$io->writeError(\sprintf(
'<error>Indent size needs to be an integer greater than 0, but "%s" is not.</error>',
$indentSize
));

return 1;
}

if (!\array_key_exists($indentStyle, self::$indentStyles)) {
$io->writeError(\sprintf(
'<error>Indent style needs to be one of "%s", but "%s" is not.</error>',
\implode('", "', \array_keys(self::$indentStyles)),
$indentStyle
));

return 1;
}

$indent = \str_repeat(
self::$indentStyles[$indentStyle],
(int) $indentSize
);
}

$file = Factory::getComposerFile();

if (!\file_exists($file)) {
$io->writeError(\sprintf(
'<error>%s not found.</error>',
Expand Down Expand Up @@ -108,7 +192,18 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 1;
}

if ($json === $normalized) {
$format = $this->sniffer->sniff($json);

if (null !== $indent) {
$format = $format->withIndent($indent);
}

$formatted = $this->formatter->format(
$normalized,
$format
);

if ($json === $formatted) {
$io->write(\sprintf(
'<info>%s is already normalized.</info>',
$file
Expand All @@ -117,7 +212,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 0;
}

\file_put_contents($file, $normalized);
\file_put_contents($file, $formatted);

$io->write(\sprintf(
'<info>Successfully normalized %s.</info>',
Expand Down
5 changes: 2 additions & 3 deletions src/Normalizer/ComposerJsonNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Localheinz\Composer\Normalize\Normalizer;

use Localheinz\Json\Normalizer\AutoFormatNormalizer;
use Localheinz\Json\Normalizer\ChainNormalizer;
use Localheinz\Json\Normalizer\NormalizerInterface;
use Localheinz\Json\Normalizer\SchemaNormalizer;
Expand All @@ -27,13 +26,13 @@ final class ComposerJsonNormalizer implements NormalizerInterface

public function __construct(string $schemaUri = 'https://getcomposer.org/schema.json')
{
$this->normalizer = new AutoFormatNormalizer(new ChainNormalizer(
$this->normalizer = new ChainNormalizer(
new SchemaNormalizer($schemaUri),
new BinNormalizer(),
new ConfigHashNormalizer(),
new PackageHashNormalizer(),
new VersionConstraintNormalizer()
));
);
}

public function normalize(string $json): string
Expand Down
Loading