Skip to content

Commit 0447c01

Browse files
committed
Change dirs in config and allow {version} placeholder in destination_dir
1 parent 809ed06 commit 0447c01

File tree

7 files changed

+82
-29
lines changed

7 files changed

+82
-29
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
/.idea/
55
*.komodoproject
66
.vscode
7+
.phpunit.result.cache
78

9+
build
10+
generated
811
vendor
912
node_modules

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
## Установка:
1010
1. `composer require --dev greensight/laravel-openapi-server-generator`
1111
2. `php artisan vendor:publish --provider="Greensight\LaravelOpenapiServerGenerator\OpenapiServerGeneratorServiceProvider"` - копирует конфиг генератора в конфиги приложения
12-
3. Так же можно обновить кэш конфигов: `php artisan config:cache`
1312

1413
## Запуск:
1514
Перед запуском убедиться, что структура описания апи соответствует [этим требованиям](https://github.com/greensight/laravel-openapi-server-generator/blob/master/docs/api_schema_requirements.md).

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"Greensight\\LaravelOpenapiServerGenerator\\OpenapiServerGeneratorServiceProvider"
2929
]
3030
}
31-
}
31+
},
32+
"scripts": {
33+
"test": "phpunit --colors=always"
34+
}
3235
}

config/openapi-server-generator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
'apidoc_dir' => public_path('api-docs'),
99

1010
/**
11-
* Path to the directory where dto model files are generated
11+
* Path to the directory where DTO files are temporary generated.
1212
* Matches the -o option in openapi generator
13+
* Old name: `output_dir`
1314
*/
14-
'output_dir' => base_path('generated'),
15+
'temp_dir' => base_path('generated'),
1516

1617
/**
17-
* Path relative to the app directory where dto models will be located
18+
* Path relative to the app/ directory where DTO files will be located.
19+
* Old name: `app_dir`
1820
*/
19-
'app_dir' => 'OpenApiGenerated'
21+
'destination_dir' => 'Http/Api{version}/OpenApiGenerated'
2022
];

phpunit.xml.dist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
verbose="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">src/</directory>
17+
</include>
18+
<report>
19+
<clover outputFile="build/logs/clover.xml"/>
20+
<html outputDirectory="build/coverage"/>
21+
<text outputFile="build/coverage.txt"/>
22+
</report>
23+
</coverage>
24+
<testsuites>
25+
<testsuite name="Package Test Suite">
26+
<directory>tests</directory>
27+
</testsuite>
28+
</testsuites>
29+
<logging>
30+
<junit outputFile="build/report.junit.xml"/>
31+
</logging>
32+
</phpunit>

src/Commands/GenerateServerVersion.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ class GenerateServerVersion extends Command {
3232
/**
3333
* @var string
3434
*/
35-
private $outputDir;
35+
private $tempDir;
3636

3737
/**
3838
* @var string
3939
*/
40-
private $appDir;
40+
private $destinationDir;
4141

4242
public function __construct()
4343
{
4444
parent::__construct();
4545

46-
$this->outputDir = config('openapi-server-generator.output_dir');
47-
$this->appDir = config('openapi-server-generator.app_dir');
46+
$this->tempDir = config('openapi-server-generator.temp_dir', config('openapi-server-generator.output_dir'));
47+
$this->destinationDir = config('openapi-server-generator.destination_dir', config('openapi-server-generator.app_dir'));
4848
}
4949

5050
/**
@@ -57,7 +57,7 @@ public function handle()
5757
$file = $this->getFile();
5858
$version = $this->getVersion();
5959

60-
$this->info("Generate Dto for file: $file, version: $version");
60+
$this->info("Generating DTOs for file: $file, version: $version");
6161

6262
$this->generateDto();
6363
$this->copyGeneratedDtoToApp();
@@ -73,7 +73,7 @@ private function generateDto(): void
7373
$inputFile = $this->getFile();
7474
$invokerPackage = $this->getInvokerPackage();
7575

76-
$command = "$bin generate -i $inputFile -g php -p 'invokerPackage=$invokerPackage,modelPackage=$modelPackage' -o $this->outputDir";
76+
$command = "$bin generate -i $inputFile -g php -p 'invokerPackage=$invokerPackage,modelPackage=$modelPackage' -o $this->tempDir";
7777

7878
$this->info("Execute command: $command");
7979

@@ -82,17 +82,17 @@ private function generateDto(): void
8282

8383
private function copyGeneratedDtoToApp(): void
8484
{
85+
$this->info("Clearing destination dir: " . $this->getAppPathToDto());
86+
8587
$this->clearAppDir();
8688

87-
$this->info("Clear app dir: " . $this->getAppPathToDto());
88-
89+
$this->info("Copying generated DTO files to destination dir: " . $this->getAppPathToDto());
90+
8991
$this->copyDto();
9092

91-
$this->info("Copy generated dto files to app dir: " . $this->getAppPathToDto());
93+
$this->info("Removing temporary generated dir: " . $this->tempDir);
9294

9395
$this->removeGeneratedDto();
94-
95-
$this->info("Remove generated dto dir: " . $this->outputDir);
9696
}
9797

9898
private function clearAppDir(): void {
@@ -102,14 +102,14 @@ private function clearAppDir(): void {
102102

103103
private function copyDto(): void
104104
{
105-
shell_exec("cp -rf $this->outputDir/lib/Dto " . $this->getAppPathToDto());
106-
shell_exec("cp -f $this->outputDir/lib/Configuration.php " . $this->getAppPathToDto());
107-
shell_exec("cp -n $this->outputDir/lib/ObjectSerializer.php " . $this->getAppPathToDto());
105+
shell_exec("cp -rf $this->tempDir/lib/Dto " . $this->getAppPathToDto());
106+
shell_exec("cp -f $this->tempDir/lib/Configuration.php " . $this->getAppPathToDto());
107+
shell_exec("cp -n $this->tempDir/lib/ObjectSerializer.php " . $this->getAppPathToDto());
108108
}
109109

110110
private function removeGeneratedDto(): void
111111
{
112-
shell_exec("rm -rf $this->outputDir");
112+
shell_exec("rm -rf $this->tempDir");
113113
}
114114

115115
private function patchModels(): void
@@ -153,20 +153,34 @@ private function getFile() {
153153
return $this->argument('file');
154154
}
155155

156-
private function getAppPathToDto() {
157-
return app_path($this->appDir . DIRECTORY_SEPARATOR . Str::upper($this->getVersion()));
156+
private function replaceVersionPlaceHolderInPath(string $pathWithPossiblePlaceholder): string
157+
{
158+
$version = Str::upper($this->getVersion());
159+
160+
$count = 0;
161+
$path = str_replace("{version}", $version, $pathWithPossiblePlaceholder, $count);
162+
if ($count) {
163+
return $path;
164+
}
165+
166+
return rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $version;
167+
}
168+
169+
private function getAppPathToDto()
170+
{
171+
return app_path($this->replaceVersionPlaceHolderInPath($this->destinationDir));
158172
}
159173

160174
private function getAppPathToModels()
161175
{
162176
return $this->getAppPathToDto() . DIRECTORY_SEPARATOR . self::MODEL_PACKAGE;
163177
}
164178

165-
private function getInvokerPackage() {
179+
private function getInvokerPackage()
180+
{
166181
return collect([
167182
'App',
168-
str_replace(DIRECTORY_SEPARATOR, '\\\\', $this->appDir),
169-
Str::upper($this->getVersion())
183+
str_replace(DIRECTORY_SEPARATOR, '\\\\', $this->replaceVersionPlaceHolderInPath($this->destinationDir))
170184
])->join('\\\\');
171185
}
172186
}

tests/Commands/GenerateServerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public function setUp(): void
1212
}
1313

1414
protected function getEnvironmentSetUp($app): void {
15-
$app['config']->set('openapi-server-generator.apidoc_dir', ('./tests/api-docs'));
16-
$app['config']->set('openapi-server-generator.output_dir', './generated');
17-
$app['config']->set('openapi-server-generator.app_dir', 'OpenApiGenerated');
15+
$app['config']->set('openapi-server-generator.apidoc_dir', './tests/api-docs');
16+
$app['config']->set('openapi-server-generator.temp_dir', './generated');
17+
$app['config']->set('openapi-server-generator.destination_dir', 'OpenApiGenerated');
1818
}
1919

2020
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)