From 64d9bf8aba357f14c86b5d16f9a5a55d46fcb76b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 10 May 2024 14:14:12 +0900 Subject: [PATCH] Add pretty-json command (#26) * Add pretty-json command * fixup! Add pretty-json command --- src/Command/PrettyJsonCommand.php | 69 +++++++++++++++++++ src/DependencyInjection/ContainerFactory.php | 2 + src/Finder/FilesFinder.php | 30 ++++++++ .../NeedsFinalizeAnalyzerTest.php | 4 +- .../ParentClassResolverTest.php | 7 +- 5 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 src/Command/PrettyJsonCommand.php diff --git a/src/Command/PrettyJsonCommand.php b/src/Command/PrettyJsonCommand.php new file mode 100644 index 0000000000..a4b11e0d48 --- /dev/null +++ b/src/Command/PrettyJsonCommand.php @@ -0,0 +1,69 @@ +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; + } +} diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 2e66791ca7..f5fe539c33 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -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; @@ -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), diff --git a/src/Finder/FilesFinder.php b/src/Finder/FilesFinder.php index 33090868a4..b203638777 100644 --- a/src/Finder/FilesFinder.php +++ b/src/Finder/FilesFinder.php @@ -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; + } } diff --git a/tests/NeedsFinalizeAnalyzer/NeedsFinalizeAnalyzerTest.php b/tests/NeedsFinalizeAnalyzer/NeedsFinalizeAnalyzerTest.php index 9cba9bd700..503125b335 100644 --- a/tests/NeedsFinalizeAnalyzer/NeedsFinalizeAnalyzerTest.php +++ b/tests/NeedsFinalizeAnalyzer/NeedsFinalizeAnalyzerTest.php @@ -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), ); } diff --git a/tests/ParentClassResolver/ParentClassResolverTest.php b/tests/ParentClassResolver/ParentClassResolverTest.php index d86c09d6f7..c612d5aaf9 100644 --- a/tests/ParentClassResolver/ParentClassResolverTest.php +++ b/tests/ParentClassResolver/ParentClassResolverTest.php @@ -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( [