Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions config/openapi-client-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
'packageName' => '<paste_your_php_package_name>'
],

/**
* Global Properties from https://openapi-generator.tech/docs/globals
*/
'global_params' => [
'apiTests' => false,
'modelTests' => false,
],

/**
* Directory where you can place templates to override default ones. . Used in -t
*/
Expand All @@ -86,10 +94,5 @@
* Files that will be ignored during repository cleanup
*/
'files_to_ignore_during_cleanup' => ['.git', '.gitignore'],

/**
* Options for disable patch section "require" composer.json
*/
'composer_disable_patch_require' => false,
]
];
33 changes: 24 additions & 9 deletions src/Commands/GenerateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ abstract class GenerateClient extends Command
*/
protected $params;

/**
* @var array
*/
protected $globalParams;

/**
* @var string
*/
Expand All @@ -71,6 +76,7 @@ public function __construct()
$this->gitHost = config('openapi-client-generator.git_host', '');

$this->params = config("openapi-client-generator.{$this->client}_args.params");
$this->globalParams = config("openapi-client-generator.{$this->client}_args.global_params", []);
$this->templateDir = config("openapi-client-generator.{$this->client}_args.template_dir", '');
$this->filesToIgnoreDuringCleanup = config("openapi-client-generator.{$this->client}_args.files_to_ignore_during_cleanup", []);
}
Expand Down Expand Up @@ -129,18 +135,22 @@ private function getGeneratorArguments(): string
$arguments .= " -t " . escapeshellarg($this->templateDir);
}

$additionalParams = $this->getAdditionalParamsArgument();

$additionalParams = $this->getParamsArgument($this->params);
if (Str::length($additionalParams) > 0) {
$arguments .= " -p " . escapeshellarg($additionalParams);
}

$globalParams = $this->getParamsArgument($this->globalParams);
if (Str::length($globalParams) > 0) {
$arguments .= " --global-property=" . escapeshellarg($globalParams);
}

return $arguments;
}

private function getAdditionalParamsArgument(): string
private function getParamsArgument(array $params): string
{
return collect($this->params)
return collect($params)
->map(function ($value, $name) {
$escapedValue = PHP_OS_FAMILY !== 'Windows' ? str_replace("\\", "\\\\", $value) : $value;
return "$name=$escapedValue";
Expand Down Expand Up @@ -179,31 +189,36 @@ protected function externalTemplatePath(string $path): ?string
return $resultPath;
}

private function recursiveClearDirectory(string $dir, int $level = 0)
private function recursiveClearDirectory(string $dir, int $level = 0, string $baseDir = ''): bool
{
if (!$dir) {
return;
return true;
}

$disableDeleteDir = false;
foreach (scandir($dir) as $fileWithoutDir) {
if (in_array($fileWithoutDir, ['..', '.'])) {
continue;
}
$file = $dir . "/" . $fileWithoutDir;
$pathFromBase = $baseDir ? $baseDir . '/' . $fileWithoutDir : $fileWithoutDir;

if ($level === 0 && in_array($fileWithoutDir, $this->filesToIgnoreDuringCleanup)) {
if (in_array($pathFromBase, $this->filesToIgnoreDuringCleanup)) {
$disableDeleteDir = true;
continue;
}

if (is_dir($file)) {
$this->recursiveClearDirectory($file, $level + 1);
$disableDeleteDir = $this->recursiveClearDirectory($file, $level + 1, $pathFromBase) || $disableDeleteDir;
} else {
unlink($file);
}
}

if ($level > 0) {
if ($level > 0 && !$disableDeleteDir) {
rmdir($dir);
}

return $disableDeleteDir;
}
}
3 changes: 2 additions & 1 deletion src/Commands/GenerateNodeJSClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Ensi\LaravelOpenapiClientGenerator\Core\Generators\NestModuleGenerator;
use Ensi\LaravelOpenapiClientGenerator\Core\Generators\NodeJSUtilsGenerator;

class GenerateNodeJSClient extends GenerateClient {
class GenerateNodeJSClient extends GenerateClient
{
/**
* @var string
*/
Expand Down
12 changes: 3 additions & 9 deletions src/Commands/GeneratePhpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Ensi\LaravelOpenapiClientGenerator\Core\Patchers\ComposerPackagePatcher;
use Ensi\LaravelOpenapiClientGenerator\Core\Generators\PhpProviderGenerator;

class GeneratePhpClient extends GenerateClient {
class GeneratePhpClient extends GenerateClient
{
/**
* @var string
*/
Expand Down Expand Up @@ -42,16 +43,11 @@ class GeneratePhpClient extends GenerateClient {
*/
protected $laravelPackageConfigKey;

/** @var bool */
private $disableComposerPatchRequire;

public function __construct()
{
parent::__construct();
$this->composerName = config('openapi-client-generator.php_args.composer_name');
$this->laravelPackageConfigKey = config("openapi-client-generator.{$this->client}_args.laravel_package_config_key", '');

$this->disableComposerPatchRequire = (bool) config('openapi-client-generator.php_args.composer_disable_patch_require', false);
}

protected function patchClientPackage(): void
Expand Down Expand Up @@ -85,9 +81,7 @@ private function patchEnums(): void
private function patchComposerPackage(): void
{
$patcher = new ComposerPackagePatcher($this->outputDir, $this->composerName);
$patcher
->setDisableRequirePatching($this->disableComposerPatchRequire)
->patch();
$patcher->patch();
}

private function generateProvider(): void {
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Generators/NestModuleConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Illuminate\Support\Str;

class NestModuleConfigGenerator {
class NestModuleConfigGenerator
{
CONST MODULE_DIRNAME = 'module';
CONST MODULE_FILENAME = 'module.ts';
CONST CONFIG_FILENAME = 'config.ts';
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Generators/NestModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use FilesystemIterator;
use Illuminate\Support\Str;

class NestModuleGenerator {
class NestModuleGenerator
{
CONST MODULE_DIRNAME = 'module';
CONST MODULE_FILENAME = 'module.ts';
CONST CONFIG_FILENAME = 'config.ts';
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Generators/NodeJSUtilsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Generators;

class NodeJSUtilsGenerator {
class NodeJSUtilsGenerator
{
CONST UTILS_DIRNAME = 'utils';
CONST QUERY_STRING_FILTER_WRAPPER_FILENAME = 'querystringFilterWrapper.ts';
CONST INDEX_FILENAME = 'index.ts';
Expand Down
35 changes: 5 additions & 30 deletions src/Core/Patchers/ComposerPackagePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,21 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

class ComposerPackagePatcher extends PackageManifestPatcher {
class ComposerPackagePatcher extends PackageManifestPatcher
{

/** @var string */
protected $manifestName = 'composer.json';
/** @var string */
protected $packageName;
/** @var bool */
private $disableRequirePatching = false;
protected string $manifestName = 'composer.json';
protected string $packageName;

public function __construct(string $packageRootDir, string $packageName)
{
parent::__construct($packageRootDir);
$this->packageName = $packageName;
}

public function setDisableRequirePatching(bool $disableRequirePatching): self
{
$this->disableRequirePatching = $disableRequirePatching;

return $this;
}

protected function applyPatchers($manifest): array
{
$manifest = $this->patchPackageName($manifest);

if (false === $this->disableRequirePatching) {
$manifest = $this->patchRequire($manifest);
}

return $manifest;
return $this->patchPackageName($manifest);
}

protected function patchPackageName($manifest)
Expand All @@ -41,13 +25,4 @@ protected function patchPackageName($manifest)

return $manifest;
}

protected function patchRequire($manifest)
{
$manifest['require']['php'] = '^7.1 || ^8.0';
$manifest['require']['guzzlehttp/guzzle'] = '^6.2 || ^7.0';
$manifest['require']['guzzlehttp/psr7'] = '^1.6.1';

return $manifest;
}
}
3 changes: 2 additions & 1 deletion src/Core/Patchers/EnumPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

abstract class EnumPatcher {
abstract class EnumPatcher
{

/**
* @var string
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Patchers/NodeJSEnumPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Illuminate\Support\Str;

class NodeJSEnumPatcher extends EnumPatcher {
class NodeJSEnumPatcher extends EnumPatcher
{

protected function getSpecificationName(): string
{
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Patchers/NodeJSIndexFilePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

class NodeJSIndexFilePatcher {
class NodeJSIndexFilePatcher
{
/**
* @var string
*/
Expand Down
8 changes: 3 additions & 5 deletions src/Core/Patchers/NpmPackagePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

class NpmPackagePatcher extends PackageManifestPatcher {
class NpmPackagePatcher extends PackageManifestPatcher
{
CONST NESTJS_COMMON_PACKAGE_VERSION = '7.0.0';
CONST NESTJS_CONFIG_PACKAGE_VERSION = '0.5.0';
CONST RXJS_PACKAGE_VERSION = '6.5.4';
CONST NODE_FETCH_PACKAGE_VERSION = '2.6.1';

/**
* @var string
*/
protected $manifestName = 'package.json';
protected string $manifestName = 'package.json';

/**
* @var boolean
Expand Down
11 changes: 5 additions & 6 deletions src/Core/Patchers/PackageManifestPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

abstract class PackageManifestPatcher {
abstract class PackageManifestPatcher
{

/**
* @var string
*/
protected $manifestName;
protected string $manifestName;

/**
* @var string
Expand All @@ -19,7 +17,8 @@ public function __construct(string $packageRootDir)
$this->packageRootDir = $packageRootDir;
}

public function patch(): void {
public function patch(): void
{
$manifest = $this->getManifest();

$manifest = $this->applyPatchers($manifest);
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Patchers/PhpEnumPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Illuminate\Support\Str;

class PhpEnumPatcher extends EnumPatcher {
class PhpEnumPatcher extends EnumPatcher
{

protected function getSpecificationName(): string
{
Expand Down
8 changes: 3 additions & 5 deletions src/Core/Patchers/TypeScriptConfigPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;

class TypeScriptConfigPatcher extends PackageManifestPatcher {
class TypeScriptConfigPatcher extends PackageManifestPatcher
{

/**
* @var string
*/
protected $manifestName = 'tsconfig.json';
protected string $manifestName = 'tsconfig.json';

protected function applyPatchers($tsConfig)
{
Expand Down