Skip to content

Commit

Permalink
Merge pull request #100 from ampproject/add/amp-validator-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jun 1, 2021
2 parents 178c7ac + 1004f23 commit 0ceee97
Show file tree
Hide file tree
Showing 983 changed files with 63,355 additions and 159 deletions.
26 changes: 15 additions & 11 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
<exclude name="PSR12.Properties.ConstantVisibility.NotFound" />
</rule>

<!-- Include sniffs for PHP cross-version compatibility. -->
<config name="testVersion" value="5.6-"/>
<rule ref="PHPCompatibilityWP">
<exclude-pattern>bin/*</exclude-pattern>
</rule>

<arg value="p"/>
<arg value="s"/>
<arg name="extensions" value="php"/>

Expand All @@ -25,14 +20,23 @@
<arg name="basepath" value="./"/>

<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20"/>
<arg name="parallel" value="1"/>

<!-- Executable scripts are meant to have side-effects. -->
<rule ref="PSR1.Files.SideEffects">
<exclude-pattern>bin/*</exclude-pattern>
</rule>
<!-- Executable scripts are meant to have side-effects. -->
<rule ref="PSR1.Files.SideEffects">
<exclude-pattern>bin/*</exclude-pattern>
</rule>

<!-- Line length can not easily be controlled in generated code. -->
<rule ref="Generic.Files.LineLength.TooLong">
<exclude-pattern>src/Validator/ErrorCode.php</exclude-pattern>
<exclude-pattern>src/Validator/Spec.php</exclude-pattern>
<exclude-pattern>src/Validator/Spec/*</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>

<!-- Class names that conflict with PHP reserved keywords need to be suffixed. -->
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<exclude-pattern>src/Validator/Spec/Tag/*_.php</exclude-pattern>
</rule>
</ruleset>
67 changes: 67 additions & 0 deletions bin/generate-validator-spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env php
<?php

use AmpProject\Tooling\Validator\SpecGenerator;

require dirname(__DIR__) . '/vendor/autoload.php';

if (! class_exists('Nette\PhpGenerator\ClassType')) {
echo "ERROR: The optional package nette/php-generator is needed for code generation.\n";
echo "Install the package via the following Composer command:\n";
echo "composer require --dev nette/php-generator:^3.5\n";
exit -1;
}

$specGenerator = new SpecGenerator();
$destination = !empty($argv[1]) ? $argv[1] : dirname(__DIR__) . '/src/Validator';
$spec_url = 'https://cdn.ampproject.org/v0/validator.json';

function recursivelyRemoveDirectory($directory)
{
if (is_dir($directory)) {
$objects = scandir($directory);

foreach ($objects as $object) {
if ($object !== '.' && $object !== '..') {
$filePath = "{$directory}/{$object}";

if (is_dir($filePath) && ! is_link($filePath)) {
recursivelyRemoveDirectory($filePath);
} else {
unlink($filePath);
}
}
}

rmdir($directory);
}
}

try {
echo "Recursively removing $destination";
recursivelyRemoveDirectory($destination);
echo "\n";

echo "Fetching $spec_url...";
$json = file_get_contents('https://cdn.ampproject.org/v0/validator.json');
echo "\n";

$data = json_decode($json, true);
if (json_last_error()) {
echo " JSON parse error: " . json_last_error_msg() . "\n";
exit -1;
}

echo 'Generating spec';
$specGenerator->generate(
json_decode($json, true),
'AmpProject\Validator',
$destination
);
echo "\n";

echo "Done!\n";
} catch (Exception $exception) {
echo 'ERROR: ' . $exception->getMessage() . "\n";
exit -1;
}
Loading

0 comments on commit 0ceee97

Please sign in to comment.