Skip to content

Commit

Permalink
Enhancement: Implement NormalizeCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 13, 2018
1 parent 85466b5 commit 1c6f7a2
Show file tree
Hide file tree
Showing 3 changed files with 965 additions and 0 deletions.
Empty file removed src/.gitkeep
Empty file.
138 changes: 138 additions & 0 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2018 Andreas Möller.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/localheinz/composer-normalize
*/

namespace Localheinz\Composer\Normalize\Command;

use Composer\Command;
use Composer\Factory;
use Localheinz\Json\Normalizer;
use Symfony\Component\Console;

final class NormalizeCommand extends Command\BaseCommand
{
/**
* @var Normalizer\NormalizerInterface
*/
private $normalizer;

public function __construct(Normalizer\NormalizerInterface $normalizer)
{
parent::__construct('normalize');

$this->normalizer = $normalizer;
}

protected function configure()
{
$this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).');
}

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

$io = $this->getIO();

if (!\file_exists($file)) {
$io->writeError(\sprintf(
'<error>%s not found.</error>',
$file
));

return 1;
}

if (!\is_readable($file)) {
$io->writeError(\sprintf(
'<error>%s is not readable.</error>',
$file
));

return 1;
}

if (!\is_writable($file)) {
$io->writeError(\sprintf(
'<error>%s is not writable.</error>',
$file
));

return 1;
}

$composer = $this->getComposer();

$locker = $composer->getLocker();

if ($locker->isLocked() && !$locker->isFresh()) {
$io->writeError('<error>The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update`.</error>');

return 1;
}

$json = \file_get_contents($file);

try {
$normalized = $this->normalizer->normalize($json);
} catch (\InvalidArgumentException $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
$exception->getMessage()
));

return 1;
} catch (\RuntimeException $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
$exception->getMessage()
));

return 1;
}

if ($json === $normalized) {
$io->write(\sprintf(
'<info>%s is already normalized.</info>',
$file
));

return 0;
}

\file_put_contents($file, $normalized);

if ($locker->isLocked() && 0 !== $this->updateLocker()) {
$io->writeError(\sprintf(
'<error>Successfully normalized %s, but could not update lock file.</error>',
$file
));

return 1;
}

$io->write(\sprintf(
'<info>Successfully normalized %s.</info>',
$file
));

return 0;
}

private function updateLocker(): int
{
return $this->getApplication()->run(
new Console\Input\StringInput('update --lock'),
new Console\Output\NullOutput()
);
}
}
Loading

0 comments on commit 1c6f7a2

Please sign in to comment.