Skip to content

Commit

Permalink
Remove generated code before new generation (#198)
Browse files Browse the repository at this point in the history
Remove generated code before new generation
  • Loading branch information
Korbeil committed Dec 9, 2019
2 parents af91fb1 + 6f8a939 commit 1f6bfb9
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 31 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"symfony/console": "^3.1 || ^4.0 || ^5.0",
"symfony/filesystem": "^3.1 || ^4.0 || ^5.0",
"symfony/options-resolver": "^3.1 || ^4.0 || ^5.0",
"symfony/property-access": "^3.1 || ^4.0 || ^5.0",
"symfony/property-info": "^3.4 || ^4.0 || ^5.0",
Expand Down
1 change: 1 addition & 0 deletions documentation/JsonSchema/generate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Other options are available to customize the generated code:
* ``date-format``: A date format to specify how the generated code should encode and decode ``\DateTime`` object to string
* ``use-fixer``: A boolean which indicate if we make a first cs-fix after code generation, is disabled by default.
* ``fixer-config-file``: A string to specify where to find the custom configuration for the cs-fixer after code generation, will remove all Jane default cs-fixer default configuration.
* ``clean-generated``: A boolean which indicate if we clean generated output before generating new files, is enabled by default.
* ``use-cacheable-supports-method``: A boolean which indicate if we use ``CacheableSupportsMethodInterface`` interface to improve caching performances when used with Symfony Serializer.

Multi schemas
Expand Down
1 change: 1 addition & 0 deletions documentation/OpenAPI/generate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Other options are available to customize the generated code:
not respecting some standards (nullable field as an example) client.
* ``use-fixer``: A boolean which indicate if we make a first cs-fix after code generation, is disabled by default.
* ``fixer-config-file``: A string to specify where to find the custom configuration for the cs-fixer after code generation, will remove all Jane default cs-fixer default configuration.
* ``clean-generated``: A boolean which indicate if we clean generated output before generating new files, is enabled by default.
* ``use-cacheable-supports-method``: A boolean which indicate if we use ``CacheableSupportsMethodInterface`` interface to improve caching performances when used with Symfony Serializer.

.. _`JSON Reference`: https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html
12 changes: 9 additions & 3 deletions src/JsonSchema/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jane\JsonSchema\Command;

use Jane\JsonSchema\Jane;
use Jane\JsonSchema\Printer;
use Jane\JsonSchema\Registry;
use Jane\JsonSchema\Schema;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}

$jane = \Jane\JsonSchema\Jane::build($options);
$jane = Jane::build($options);
$fixerConfigFile = '';

if (\array_key_exists('fixer-config-file', $options) && null !== $options['fixer-config-file']) {
Expand All @@ -63,8 +64,11 @@ public function execute(InputInterface $input, OutputInterface $output)

$printer = new Printer(new Standard(), $fixerConfigFile);

if (\array_key_exists('use-fixer', $options) && false === $options['use-fixer']) {
$printer->setUseFixer(false);
if (\array_key_exists('use-fixer', $options) && \is_bool($options['use-fixer'])) {
$printer->setUseFixer($options['use-fixer']);
}
if (\array_key_exists('clean-generated', $options) && \is_bool($options['clean-generated'])) {
$printer->setCleanGenerated($options['clean-generated']);
}

$jane->generate($registry);
Expand All @@ -82,6 +86,7 @@ protected function resolveConfiguration(array $options = [])
'date-format' => \DateTime::RFC3339,
'use-fixer' => false,
'fixer-config-file' => null,
'clean-generated' => true,
'use-cacheable-supports-method' => null,
]);

Expand Down Expand Up @@ -113,6 +118,7 @@ protected function resolveSchema($schema, array $options = [])
'strict',
'use-fixer',
'fixer-config-file',
'clean-generated',
'use-cacheable-supports-method',
]);

Expand Down
21 changes: 18 additions & 3 deletions src/JsonSchema/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,44 @@
use PhpParser\PrettyPrinterAbstract;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;

class Printer
{
private $prettyPrinter;

private $fixerConfig;

private $useFixer;
private $useFixer = false;

public function __construct(PrettyPrinterAbstract $prettyPrinter, string $fixerConfig = '', bool $useFixer = false)
private $cleanGenerated = true;

public function __construct(PrettyPrinterAbstract $prettyPrinter, string $fixerConfig = '')
{
$this->prettyPrinter = $prettyPrinter;
$this->fixerConfig = $fixerConfig;
$this->useFixer = $useFixer;
}

public function setUseFixer(bool $useFixer): void
{
$this->useFixer = $useFixer;
}

public function setCleanGenerated(bool $cleanGenerated): void
{
$this->cleanGenerated = $cleanGenerated;
}

public function output(Registry $registry): void
{
if ($this->cleanGenerated) {
$fs = new Filesystem();
foreach ($registry->getOutputDirectories() as $directory) {
$fs->remove($directory);
$fs->mkdir($directory);
}
}

foreach ($registry->getSchemas() as $schema) {
foreach ($schema->getFiles() as $file) {
if (!file_exists(\dirname($file->getFilename()))) {
Expand Down
14 changes: 2 additions & 12 deletions src/JsonSchema/Tests/JaneBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

Expand All @@ -17,24 +16,15 @@ class JaneBaseTest extends TestCase
*/
public function testRessources($name, SplFileInfo $testDirectory)
{
// 1. Cleanup generated
$filesystem = new Filesystem();

if ($filesystem->exists($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated')) {
$filesystem->remove($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated');
}

$filesystem->mkdir($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated');

// 2. Generate
// 1. Generate
$command = new GenerateCommand();
$inputArray = new ArrayInput([
'--config-file' => $testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . '.jane',
], $command->getDefinition());

$command->execute($inputArray, new NullOutput());

// 3. Compare
// 2. Compare
$expectedFinder = new Finder();
$expectedFinder->in($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'expected');
$generatedFinder = new Finder();
Expand Down
4 changes: 3 additions & 1 deletion src/JsonSchema/Tests/LibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function setUp()
'reference' => true,
'strict' => false,
]);
$this->printer = new Printer(new Standard(), '', true);
$this->printer = new Printer(new Standard(), '');
$this->printer->setUseFixer(true);
$this->printer->setCleanGenerated(false);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"jane-php/json-schema-runtime": "^5.0",
"nikic/php-parser": "^4.0",
"symfony/console": "^3.1 || ^4.0 || ^5.0",
"symfony/filesystem": "^3.1 || ^4.0 || ^5.0",
"symfony/options-resolver": "^3.1 || ^4.0 || ^5.0",
"symfony/property-access": "^3.1 || ^4.0 || ^5.0",
"symfony/yaml": "^3.1 || ^4.0 || ^5.0"
Expand Down
5 changes: 5 additions & 0 deletions src/OpenApi/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function execute(InputInterface $input, OutputInterface $output)
if (\array_key_exists('use-fixer', $options) && \is_bool($options['use-fixer'])) {
$printer->setUseFixer($options['use-fixer']);
}
if (\array_key_exists('clean-generated', $options) && \is_bool($options['clean-generated'])) {
$printer->setCleanGenerated($options['clean-generated']);
}

$janeOpenApi->generate($registry);
$printer->output($registry);
Expand All @@ -84,6 +87,7 @@ protected function resolveConfiguration(array $options = [])
'strict' => true,
'use-fixer' => false,
'fixer-config-file' => null,
'clean-generated' => true,
'use-cacheable-supports-method' => null,
'client' => JaneOpenApi::CLIENT_HTTPLUG,
]);
Expand Down Expand Up @@ -123,6 +127,7 @@ protected function resolveSchema($schema, array $options = [])
'strict',
'use-fixer',
'fixer-config-file',
'clean-generated',
'use-cacheable-supports-method',
'client',
]);
Expand Down
14 changes: 2 additions & 12 deletions src/OpenApi/Tests/JaneOpenApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

Expand All @@ -17,21 +16,12 @@ class JaneOpenApiResourceTest extends TestCase
*/
public function testRessources($name, SplFileInfo $testDirectory)
{
// 1. Cleanup generated
$filesystem = new Filesystem();

if ($filesystem->exists($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated')) {
$filesystem->remove($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated');
}

$filesystem->mkdir($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'generated');

// 2. Generate
// 1. Generate
$command = new GenerateCommand();
$input = new ArrayInput(['--config-file' => $testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . '.jane-openapi'], $command->getDefinition());
$command->execute($input, new NullOutput());

// 3. Compare
// 2. Compare
$expectedFinder = new Finder();
$expectedFinder->in($testDirectory->getRealPath() . \DIRECTORY_SEPARATOR . 'expected');

Expand Down

0 comments on commit 1f6bfb9

Please sign in to comment.