Skip to content

Commit

Permalink
Add pretty-json command (#26)
Browse files Browse the repository at this point in the history
* Add pretty-json command

* fixup! Add pretty-json command
  • Loading branch information
TomasVotruba committed May 10, 2024
1 parent b2b162e commit 64d9bf8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
69 changes: 69 additions & 0 deletions src/Command/PrettyJsonCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Command;

use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Rector\SwissKnife\Finder\FilesFinder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class PrettyJsonCommand extends Command
{
public function __construct(
private readonly SymfonyStyle $symfonyStyle,
) {
parent::__construct();
}

protected function configure(): void
{
$this->setName('pretty-json');

$this->addArgument(
'sources',
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'File or directory to prettify'
);
$this->setDescription('Turns JSON files from 1-line format to pretty format');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$sources = (array) $input->getArgument('sources');
$jsonFileInfos = FilesFinder::findJsonFiles($sources);

if (count($jsonFileInfos) === 0) {
$this->symfonyStyle->error('No *.json files found');
return self::FAILURE;
}

$message = sprintf('Analysing %d *.json files', count($jsonFileInfos));
$this->symfonyStyle->note($message);

// convert file infos from uggly json to pretty json
foreach ($jsonFileInfos as $jsonFileInfo) {
$jsonContent = FileSystem::read($jsonFileInfo->getRealPath());
$prettyJsonContent = Json::encode(Json::decode($jsonContent), JSON_PRETTY_PRINT);

// nothing to convert
if ($prettyJsonContent === $jsonContent) {
$this->symfonyStyle->writeln(sprintf('The "%s" file is already pretty', $jsonFileInfo->getRealPath()));
continue;
}

// notify the file was changed
$this->symfonyStyle->writeln(
sprintf('The "%s" file was changed to pretty json', $jsonFileInfo->getRealPath())
);
FileSystem::write($jsonFileInfo->getRealPath(), $prettyJsonContent);
}

return self::SUCCESS;
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\SwissKnife\Command\FinalizeClassesCommand;
use Rector\SwissKnife\Command\FindMultiClassesCommand;
use Rector\SwissKnife\Command\NamespaceToPSR4Command;
use Rector\SwissKnife\Command\PrettyJsonCommand;
use Rector\SwissKnife\Command\ValidateFileLengthCommand;
use Rector\SwissKnife\Testing\Command\DetectUnitTestsCommand;
use Symfony\Component\Console\Application;
Expand All @@ -34,6 +35,7 @@ public function create(): Container
$application = new Application('Easy CI toolkit');

$commands = [
$container->make(PrettyJsonCommand::class),
$container->make(CheckCommentedCodeCommand::class),
$container->make(CheckConflictsCommand::class),
$container->make(ValidateFileLengthCommand::class),
Expand Down
30 changes: 30 additions & 0 deletions src/Finder/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,34 @@ public static function findPhpFiles(array $sources): array

return iterator_to_array($finder->getIterator());
}

/**
* @param string[] $sources
* @return SplFileInfo[]
*/
public static function findJsonFiles(array $sources): array
{
$jsonFileInfos = [];
$directories = [];

foreach ($sources as $source) {
if (is_file($source)) {
$jsonFileInfos[] = new SplFileInfo($source, '', $source);
} else {
$directories[] = $source;
}
}

$jsonFileFinder = Finder::create()
->files()
->in($directories)
->name('*.json')
->sortByName();

foreach ($jsonFileFinder->getIterator() as $fileInfo) {
$jsonFileInfos[] = $fileInfo;
}

return $jsonFileInfos;
}
}
4 changes: 1 addition & 3 deletions tests/NeedsFinalizeAnalyzer/NeedsFinalizeAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ protected function setUp(): void
parent::setUp();

$this->needsFinalizeAnalyzer = new NeedsFinalizeAnalyzer(
excludedClasses: [
'Rector\SwissKnife\Tests\NeedsFinalizeAnalyzer\Fixture\ExcludedClass',
],
excludedClasses: ['Rector\SwissKnife\Tests\NeedsFinalizeAnalyzer\Fixture\ExcludedClass'],
cachedPhpParser: $this->make(CachedPhpParser::class),
);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/ParentClassResolver/ParentClassResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ protected function setUp(): void

public function test(): void
{
$parentClasses = $this->parentClassResolver->resolve(PhpFilesFinder::find([__DIR__ . '/Fixture']), static function (): void {
});
$parentClasses = $this->parentClassResolver->resolve(
PhpFilesFinder::find([__DIR__ . '/Fixture']),
static function (): void {
}
);

$this->assertSame(
[
Expand Down

0 comments on commit 64d9bf8

Please sign in to comment.