Improved bin/grav yamllinter CLI command by adding an option to find YAML Linting issues from the whole site or custom folder

This commit is contained in:
Matias Griese
2019-08-19 13:18:39 +03:00
parent 033b54104e
commit 829da9ee3a
3 changed files with 69 additions and 37 deletions

View File

@@ -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)

View File

@@ -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 = [];

View File

@@ -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("<yellow>$path</yellow> - $error");
$io->writeln("<yellow>{$path}</yellow> - {$error}");
}
}
}