From 829da9ee3a46107d5ec7e511cc494bde6d6b56f9 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 19 Aug 2019 13:18:39 +0300 Subject: [PATCH] Improved `bin/grav yamllinter` CLI command by adding an option to find YAML Linting issues from the whole site or custom folder --- CHANGELOG.md | 2 + system/src/Grav/Common/Helpers/YamlLinter.php | 16 ++-- .../Grav/Console/Cli/YamlLinterCommand.php | 88 ++++++++++++------- 3 files changed, 69 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b5696d49..1f8cb74e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ 1. [](#new) * Added experimental support for `Flex Pages` (**Flex Objects** plugin required) +1. [](#improved) + * Improved `bin/grav yamllinter` CLI command by adding an option to find YAML Linting issues from the whole site or custom folder 1. [](#bugfix) * Fixed `$page->summary()` always striping HTML tags if the summary was set by `$page->setSummary()` * Grav 1.7: Fixed enabling PHP Debug Bar causes fatal error in Gantry [#2634](https://github.com/getgrav/grav/issues/2634) diff --git a/system/src/Grav/Common/Helpers/YamlLinter.php b/system/src/Grav/Common/Helpers/YamlLinter.php index b7e608d00..3844859fe 100644 --- a/system/src/Grav/Common/Helpers/YamlLinter.php +++ b/system/src/Grav/Common/Helpers/YamlLinter.php @@ -16,13 +16,15 @@ use Symfony\Component\Yaml\Yaml; class YamlLinter { - public static function lint() + public static function lint(string $folder = null) { - $errors = static::lintConfig(); - $errors = $errors + static::lintPages(); - $errors = $errors + static::lintBlueprints(); - - return $errors; + if (null !== $folder) { + $folder = $folder ?: GRAV_ROOT; + + return static::recurseFolder($folder); + } + + return array_merge(static::lintConfig(), static::lintPages(), static::lintBlueprints()); } public static function lintPages() @@ -47,7 +49,7 @@ class YamlLinter return static::recurseFolder('blueprints://'); } - public static function recurseFolder($path, $extensions = 'md|yaml') + public static function recurseFolder($path, $extensions = '(md|yaml)') { $lint_errors = []; diff --git a/system/src/Grav/Console/Cli/YamlLinterCommand.php b/system/src/Grav/Console/Cli/YamlLinterCommand.php index 894f1c02d..c48b63321 100644 --- a/system/src/Grav/Console/Cli/YamlLinterCommand.php +++ b/system/src/Grav/Console/Cli/YamlLinterCommand.php @@ -10,9 +10,7 @@ namespace Grav\Console\Cli; use Grav\Common\Grav; -use Grav\Common\Helpers\LogViewer; use Grav\Common\Helpers\YamlLinter; -use Grav\Common\Utils; use Grav\Console\ConsoleCommand; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Style\SymfonyStyle; @@ -29,6 +27,18 @@ class YamlLinterCommand extends ConsoleCommand InputOption::VALUE_OPTIONAL, 'The environment to trigger a specific configuration. For example: localhost, mysite.dev, www.mysite.com' ) + ->addOption( + 'all', + 'a', + InputOption::VALUE_NONE, + 'Go through the whole Grav installation' + ) + ->addOption( + 'folder', + 'f', + InputOption::VALUE_OPTIONAL, + 'Go through specific folder' + ) ->setDescription('Checks various files for YAML errors') ->setHelp("Checks various files for YAML errors"); } @@ -42,41 +52,59 @@ class YamlLinterCommand extends ConsoleCommand $io->title('Yaml Linter'); - $io->section('User Configuration'); - $errors = YamlLinter::lintConfig(); + if ($this->input->getOption('all')) { + $io->section('All'); + $errors = YamlLinter::lint(''); - if (empty($errors)) { - $io->success('No YAML Linting issues with configuration'); + if (empty($errors)) { + $io->success('No YAML Linting issues found'); + } else { + $this->displayErrors($errors, $io); + } + } elseif ($folder = $this->input->getOption('folder')) { + $io->section($folder); + $errors = YamlLinter::lint($folder); + + if (empty($errors)) { + $io->success('No YAML Linting issues found'); + } else { + $this->displayErrors($errors, $io); + } } else { - $this->displayErrors($errors, $io); + $io->section('User Configuration'); + $errors = YamlLinter::lintConfig(); + + if (empty($errors)) { + $io->success('No YAML Linting issues with configuration'); + } else { + $this->displayErrors($errors, $io); + } + + $io->section('Pages Frontmatter'); + $errors = YamlLinter::lintPages(); + + if (empty($errors)) { + $io->success('No YAML Linting issues with pages'); + } else { + $this->displayErrors($errors, $io); + } + + $io->section('Page Blueprints'); + $errors = YamlLinter::lintBlueprints(); + + if (empty($errors)) { + $io->success('No YAML Linting issues with blueprints'); + } else { + $this->displayErrors($errors, $io); + } } - - - $io->section('Pages Frontmatter'); - $errors = YamlLinter::lintPages(); - - if (empty($errors)) { - $io->success('No YAML Linting issues with pages'); - } else { - $this->displayErrors($errors, $io); - } - - $io->section('Page Blueprints'); - $errors = YamlLinter::lintBlueprints(); - - if (empty($errors)) { - $io->success('No YAML Linting issues with blueprints'); - } else { - $this->displayErrors($errors, $io); - } - } - protected function displayErrors($errors, $io) + protected function displayErrors($errors, SymfonyStyle $io) { - $io->error("YAML Linting issues found..."); + $io->error('YAML Linting issues found...'); foreach ($errors as $path => $error) { - $io->writeln("$path - $error"); + $io->writeln("{$path} - {$error}"); } } }