Skip to content

Commit

Permalink
Feature: scan dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rokas Mikalkėnas committed Sep 10, 2020
1 parent 31ff47d commit 2ce3ce0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require": {
"php": "~7.2",
"symfony/console": "~3.4|~4.3|~5.0",
"symfony/finder": "~3.4|~4.3|~5.0",
"symfony/yaml": "~3.4|~4.3|~5.0"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions docs/symfony-config/yaml-sort-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ files:
- host
- username
- password
excludedSections:
0: imports
1: parameters
excludedSections:
0: imports
1: parameters

app/config/config_dev.yml:
excludedKeys:
Expand Down
91 changes: 70 additions & 21 deletions src/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,49 @@

namespace Mhujer\YamlSortChecker;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;

class CheckCommand extends \Symfony\Component\Console\Command\Command
{

protected function configure(): void
{
$this->setName('yaml-check-sort');
$this
->setName('yaml-check-sort')
->addArgument('config', InputArgument::OPTIONAL, 'Configuration filename', 'yaml-sort-checker.yml');
}

protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$output->writeln('#### YAML Sort Checker ####');

$configFilePath = realpath('yaml-sort-checker.yml');
$config = $input->getArgument('config');
$configFilePath = is_string($config) ? realpath($config) : false;
if ($configFilePath === false) {
$output->writeln(sprintf('Config file "%s" not found!', 'yaml-sort-checker.yml'));
exit(1);
return 1;
}
$output->writeln(sprintf('Using config file "%s"', $configFilePath));

$configFileContents = file_get_contents($configFilePath);
if ($configFileContents === false) {
throw new \Exception(sprintf('File "%s" could not be loaded', $configFilePath));
$output->writeln(sprintf('File "%s" could not be loaded', $configFilePath));
return 1;
}
$config = Yaml::parse($configFileContents);

if (!array_key_exists('files', $config)) {
$output->writeln('There must be a key "files" in config');
exit(1);
return 1;
}

if (count($config['files']) === 0) {
$output->writeln('There must be some files in the config');
exit(1);
return 1;
}

$output->writeln('');
Expand All @@ -56,32 +62,75 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$excludedKeys = array_key_exists('excludedKeys', $options) ? $options['excludedKeys'] : [];
$excludedSections = array_key_exists('excludedSections', $options) ? $options['excludedSections'] : [];

$output->write(sprintf('Checking %s: ', $filename));
if (realpath($filename) === false || !is_readable(realpath($filename))) {
$output->writeln('NOT READABLE!');
exit(1);
try {
$files = $this->resolveYmlFiles($filename);
} catch (\InvalidArgumentException $e) {
$output->writeln($e->getMessage());
return 1;
}

$sortCheckResult = $sortChecker->isSorted($filename, $depth, $excludedKeys, $excludedSections);
foreach ($files as $file) {
$output->write(sprintf('Checking %s: ', $file));

if ($sortCheckResult->isOk()) {
$output->writeln('OK');
} else {
$output->writeln('ERROR');
foreach ($sortCheckResult->getMessages() as $message) {
$output->writeln(' ' . $message);
$sortCheckResult = $sortChecker->isSorted($file, $depth, $excludedKeys, $excludedSections);

if ($sortCheckResult->isOk()) {
$output->writeln('OK');
} else {
$output->writeln('ERROR');
foreach ($sortCheckResult->getMessages() as $message) {
$output->writeln(' ' . $message);
}
$isOk = false;
}
$isOk = false;
}
}

$output->writeln('');
if (!$isOk) {
$output->writeln('Fix the YAMLs or exclude the keys in the config.');
return 1;
} else {
$output->writeln('All YAMLs are properly sorted.');
return 0;
}

$output->writeln('All YAMLs are properly sorted.');
return 0;
}

/**
* @param string $filename
* @return string[]
*/
private function resolveYmlFiles(string $filename): array
{
// Check if its file
$filepath = realpath($filename);
if ($filepath !== false && is_file($filepath) && is_readable($filepath)) {
return [$filename];
}

try {
// If not try to parse it as directory
$filesIterator = (new Finder())
->in($filename)
->name(['*.yml', '*.yaml'])
->ignoreUnreadableDirs(true)
->ignoreDotFiles(true)
->ignoreVCS(true)
->getIterator();

$files = [];
foreach ($filesIterator as $fileInfo) {
$fileRealPath = $fileInfo->getRealPath();
if ($fileRealPath === false || !is_readable($fileRealPath)) {
throw new \InvalidArgumentException(sprintf('File %s is not readable', $fileRealPath));
}

$files[] = $fileRealPath;
}

return $files;
} catch (\Throwable $e) {
throw new \InvalidArgumentException(sprintf('Unable to find files in %s', $filename));
}
}

Expand Down

0 comments on commit 2ce3ce0

Please sign in to comment.