Skip to content

Commit

Permalink
Add option to only validate the schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jan 18, 2023
1 parent 2956011 commit d7f2a53
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Hyde\Publications\Commands;

use Hyde\Hyde;
use function basename;
use function collect;
use Exception;
use function filled;
Expand All @@ -15,6 +17,7 @@
use Illuminate\Support\Collection;
use InvalidArgumentException;
use LaravelZero\Framework\Commands\Command;
use function glob;
use function microtime;
use function str_repeat;
use function strlen;
Expand All @@ -35,7 +38,8 @@ class ValidatePublicationsCommand extends ValidatingCommand
/** @var string */
protected $signature = 'validate:publications
{publicationType? : The name of the publication type to validate.}
{--json : Display results as JSON.}';
{--json : Display results as JSON.}
{--schemas : Only validate the publication schema files.}';

/** @var string */
protected $description = 'Validate all or the specified publication type(s)';
Expand All @@ -61,6 +65,17 @@ public function safeHandle(): int
$this->title('Validating publications!');
}

if ($this->option('schemas')) {
$this->validateSchemaFiles();

$this->newLine();
$this->info(sprintf('All done in %sms using %sMB peak memory!',
round((microtime(true) - $timeStart) * 1000),
round(memory_get_peak_usage() / 1024 / 1024)
));
return Command::SUCCESS;
}

$publicationTypesToValidate = $this->getPublicationTypesToValidate();

foreach ($publicationTypesToValidate as $publicationType) {
Expand Down Expand Up @@ -249,4 +264,30 @@ protected function outputJson(): void
{
$this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT));
}

protected function validateSchemaFiles(): void
{
/** @see PublicationService::getSchemaFiles() */
$schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json');

foreach ($schemaFiles as $number => $schemaFile) {
$name = basename(dirname($schemaFile));
$this->infoComment('Validating schema file for', $name);

$errors = PublicationService::validateSchemaFile($schemaFile, false);

if (empty($errors['schema'])) {
$this->line('<info> No top-level schema errors found</info>');
} else {
$this->line(sprintf(" <fg=red>Found %s errors:</>", count($errors['schema'])));
foreach ($errors['schema'] as $error) {
$this->line(sprintf(" <fg=red>%s</> <comment>%s</comment>", self::CROSS_MARK, implode(' ', $error)));
}
}

if ($number !== count($schemaFiles) - 1) {
$this->newLine();
}
}
}
}

0 comments on commit d7f2a53

Please sign in to comment.