Skip to content

Commit e2efe44

Browse files
authored
Merge pull request #148 from php-api-clients/replace-output-status-table-in-places-instead-of-clearing-the-screen
Replace output status table in places instead of clearing the screen
2 parents a030c6d + 3831755 commit e2efe44

File tree

7 files changed

+169
-43
lines changed

7 files changed

+169
-43
lines changed

etc/qa/phpcs.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
<arg name="cache" value=".phpcs.cache" /> <!-- cache the results and don't commit them -->
77
<arg value="np" /> <!-- n = ignore warnings, p = show progress -->
88

9-
<file>../../src/Generator/Client.php</file>
10-
<!-- <file>../../src</file>-->
11-
<!-- <file>../../tests</file>-->
9+
<file>../../src</file>
10+
<file>../../tests</file>
1211

1312
<rule ref="WyriHaximus-OSS" />
1413
</ruleset>

src/Gatherer/Schema.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function array_key_exists;
1212
use function in_array;
1313
use function is_array;
14+
use function property_exists;
1415

1516
final class Schema
1617
{
@@ -19,8 +20,8 @@ public static function gather(
1920
baseSchema $schema,
2021
SchemaRegistry $schemaRegistry,
2122
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema {
22-
$className = Utils::fixKeyword($className);
23-
$isArray = $schema->type === 'array';
23+
$className = Utils::fixKeyword($className);
24+
$isArray = $schema->type === 'array';
2425
$properties = [];
2526
$example = [];
2627

@@ -43,9 +44,11 @@ public static function gather(
4344
break;
4445
}
4546

46-
if (property_exists($schema, $examplePropertyName) && is_array($schema->$examplePropertyName) && array_key_exists($gatheredProperty->sourceName, $schema->$examplePropertyName)) {
47-
$example[$gatheredProperty->sourceName] = $schema->$examplePropertyName[$gatheredProperty->sourceName];
47+
if (! property_exists($schema, $examplePropertyName) || ! is_array($schema->$examplePropertyName) || ! array_key_exists($gatheredProperty->sourceName, $schema->$examplePropertyName)) {
48+
continue;
4849
}
50+
51+
$example[$gatheredProperty->sourceName] = $schema->$examplePropertyName[$gatheredProperty->sourceName];
4952
}
5053

5154
$example[$gatheredProperty->sourceName] = $gatheredProperty->exampleData;

src/Generator.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\Step;
2626
use cebe\openapi\Reader;
2727
use cebe\openapi\spec\OpenApi;
28-
use DivineOmega\CliProgressBar\ProgressBar;
2928
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection;
3029
use PhpParser\Node;
3130
use PhpParser\PrettyPrinter\Standard;
32-
use Psr\Log\LoggerInterface;
3331
use RuntimeException;
3432

3533
use function array_filter;
@@ -129,7 +127,7 @@ public function __construct(
129127
$this->state->specHash === $this->currentSpecHash &&
130128
(static function (string $root, Files $files, string ...$additionalFiles): bool {
131129
foreach ($additionalFiles as $additionalFile) {
132-
if ($files->has($additionalFile) && (!file_exists($root . $additionalFile) || $files->get($additionalFile)->hash !== md5(file_get_contents($root . $additionalFile)))) {
130+
if ($files->has($additionalFile) && (! file_exists($root . $additionalFile) || $files->get($additionalFile)->hash !== md5(file_get_contents($root . $additionalFile)))) {
133131
echo $additionalFile, PHP_EOL;
134132

135133
return false;
@@ -255,6 +253,7 @@ private function all(string $namespace, string $namespaceTest, string $configura
255253
$this->statusOutput->advanceStep('gathering_schemas');
256254
}
257255
}
256+
258257
$this->statusOutput->markStepDone('gathering_schemas');
259258

260259
$webHooks = [];
@@ -266,9 +265,11 @@ private function all(string $namespace, string $namespaceTest, string $configura
266265
$webHooks[$webHookje->event] = [];
267266
}
268267

269-
$webHooks[$webHookje->event][] = $webHookje;$this->statusOutput->advanceStep('gathering_webhooks');
268+
$webHooks[$webHookje->event][] = $webHookje;
269+
$this->statusOutput->advanceStep('gathering_webhooks');
270270
}
271271
}
272+
272273
$this->statusOutput->markStepDone('gathering_webhooks');
273274

274275
$paths = [];
@@ -285,10 +286,11 @@ private function all(string $namespace, string $namespaceTest, string $configura
285286
continue;
286287
}
287288

288-
$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $this->configuration->voter);
289+
$paths[] = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Path::gather($pathClassName, $path, $pathItem, $schemaRegistry, $this->configuration->voter);
289290
$webHooks[$webHookje->event][] = $webHookje;
290291
$this->statusOutput->advanceStep('gathering_paths');
291292
}
293+
292294
$this->statusOutput->markStepDone('gathering_paths');
293295
}
294296

@@ -316,8 +318,8 @@ private function all(string $namespace, string $namespaceTest, string $configura
316318

317319
private function oneClient(string $namespace, string $namespaceTest, string $configurationLocation, SchemaRegistry $schemaRegistry, ThrowableSchema $throwableSchemaRegistry, array $schemas, array $paths, array $webHooks)
318320
{
319-
$hydrators = [];
320-
$operations = [];
321+
$hydrators = [];
322+
$operations = [];
321323
$this->statusOutput->itemForStep('generating_operations', count($paths));
322324
foreach ($paths as $path) {
323325
$hydrators[] = $path->hydrator;
@@ -345,6 +347,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
345347

346348
$this->statusOutput->advanceStep('generating_operations');
347349
}
350+
348351
$this->statusOutput->markStepDone('generating_operations');
349352

350353
$unknownSchemaCount = 0;
@@ -357,6 +360,7 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
357360
$this->statusOutput->advanceStep('gathering_unknown_schemas');
358361
}
359362
}
363+
360364
$this->statusOutput->markStepDone('gathering_unknown_schemas');
361365

362366
$this->statusOutput->itemForStep('generating_schemas', count($schemas));
@@ -375,20 +379,24 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
375379
$schema,
376380
);
377381
}
382+
378383
$this->statusOutput->advanceStep('generating_schemas');
379384
}
385+
380386
$this->statusOutput->markStepDone('generating_schemas');
381387

382388
$client = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Client::gather($this->spec, ...$paths);
383389

384390
$this->statusOutput->markStepDone('generating_clientinterface');
391+
385392
yield from ClientInterface::generate(
386393
$this->configuration->destination->source . DIRECTORY_SEPARATOR,
387394
$namespace,
388395
$operations,
389396
);
390397

391398
$this->statusOutput->markStepDone('generating_client');
399+
392400
yield from Client::generate(
393401
$this->configuration->destination->source . DIRECTORY_SEPARATOR,
394402
$namespace,
@@ -410,11 +418,14 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
410418
$schemaRegistry,
411419
...$webHook,
412420
);
421+
413422
$this->statusOutput->advanceStep('generating_webhooks');
414423
}
424+
415425
$this->statusOutput->markStepDone('generating_webhooks');
416426

417427
$this->statusOutput->markStepDone('generating_webhooks_entry_point');
428+
418429
yield from WebHooks::generate($this->configuration->destination->source . DIRECTORY_SEPARATOR, $namespace, $webHooksHydrators, $webHooks);
419430

420431
$this->statusOutput->itemForStep('generating_hydrators', count($hydrators));
@@ -423,9 +434,11 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
423434

424435
$this->statusOutput->advanceStep('generating_hydrators');
425436
}
437+
426438
$this->statusOutput->markStepDone('generating_hydrators');
427439

428440
$this->statusOutput->markStepDone('generating_hydrators_entry_point');
441+
429442
yield from Hydrators::generate($this->configuration->destination->source . DIRECTORY_SEPARATOR, $namespace, ...$hydrators);
430443

431444
$this->statusOutput->markStepDone('generating_templated_files');
@@ -447,9 +460,9 @@ private function oneClient(string $namespace, string $namespaceTest, string $con
447460
*/
448461
private function subSplitClient(string $namespace, string $namespaceTest, string $configurationLocation, SchemaRegistry $schemaRegistry, ThrowableSchema $throwableSchemaRegistry, array $schemas, array $paths, array $webHooks)
449462
{
450-
$splits = [];
451-
$hydrators = [];
452-
$operations = [];
463+
$splits = [];
464+
$hydrators = [];
465+
$operations = [];
453466
$this->statusOutput->itemForStep('generating_operations', count($paths));
454467
foreach ($paths as $path) {
455468
foreach ($this->configuration->subSplit->sectionGenerator as $generator) {
@@ -482,8 +495,10 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
482495
$this->configuration,
483496
);
484497
}
498+
485499
$this->statusOutput->advanceStep('generating_operations');
486500
}
501+
487502
$this->statusOutput->markStepDone('generating_operations');
488503

489504
$webHooksHydrators = [];
@@ -512,6 +527,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
512527
$this->statusOutput->advanceStep('gathering_unknown_schemas');
513528
}
514529
}
530+
515531
$this->statusOutput->markStepDone('gathering_unknown_schemas');
516532

517533
$sortedSchemas = [];
@@ -575,20 +591,24 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
575591
$aliases,
576592
);
577593
}
594+
578595
$this->statusOutput->advanceStep('generating_schemas');
579596
}
597+
580598
$this->statusOutput->markStepDone('generating_schemas');
581599

582600
$client = \ApiClients\Tools\OpenApiClientGenerator\Gatherer\Client::gather($this->spec, ...$paths);
583601

584602
$this->statusOutput->markStepDone('generating_clientinterface');
603+
585604
yield from ClientInterface::generate(
586605
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
587606
$namespace,
588607
$operations,
589608
);
590609

591610
$this->statusOutput->markStepDone('generating_client');
611+
592612
yield from Client::generate(
593613
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
594614
$namespace,
@@ -613,11 +633,14 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
613633
$schemaRegistry,
614634
...$webHook,
615635
);
636+
616637
$this->statusOutput->advanceStep('generating_webhooks');
617638
}
639+
618640
$this->statusOutput->markStepDone('generating_webhooks');
619641

620642
$this->statusOutput->markStepDone('generating_webhooks_entry_point');
643+
621644
yield from WebHooks::generate(
622645
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
623646
$namespace,
@@ -634,11 +657,14 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
634657
$hydrator
635658
);
636659
}
660+
637661
$this->statusOutput->advanceStep('generating_hydrators');
638662
}
663+
639664
$this->statusOutput->markStepDone('generating_hydrators');
640665

641666
$this->statusOutput->markStepDone('generating_hydrators_entry_point');
667+
642668
yield from Hydrators::generate(
643669
$this->configuration->subSplit->subSplitsDestination . DIRECTORY_SEPARATOR . $this->splitPathPrefix($this->configuration->subSplit->rootPackage, '') . $this->configuration->destination->source,
644670
$namespace,
@@ -739,6 +765,7 @@ private function subSplitClient(string $namespace, string $namespaceTest, string
739765
);
740766
$this->statusOutput->advanceStep('generating_templates_files_subsplit_package');
741767
}
768+
742769
$this->statusOutput->markStepDone('generating_templates_files_subsplit_package');
743770

744771
$this->statusOutput->markStepDone('generating_subsplit_configuration');

src/Registry/ThrowableSchema.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
final class ThrowableSchema
1010
{
11-
/**
12-
* @var array<string>
13-
*/
11+
/** @var array<string> */
1412
private array $throwables = [];
1513

1614
public function add(string $class): void

src/StatusOutput.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace ApiClients\Tools\OpenApiClientGenerator;
46

7+
use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\OverWritingOutPut;
58
use ApiClients\Tools\OpenApiClientGenerator\StatusOutput\Step;
6-
use Termwind\Terminal;
9+
use Symfony\Component\Console\Output\ConsoleOutput;
710

8-
use function Termwind\terminal;
911
use function Termwind\render;
12+
use function Termwind\renderUsing;
13+
use function time;
1014

1115
final class StatusOutput
1216
{
13-
/**
14-
* @var array<Step>
15-
*/
17+
/** @var array<Step> */
1618
private readonly array $steps;
1719

18-
/**
19-
* @var array<bool>
20-
*/
20+
/** @var array<bool> */
2121
private array $stepsStatus = [];
2222

23-
/**
24-
* @var array<int>
25-
*/
23+
/** @var array<int> */
2624
private array $itemsCountForStep = [];
2725

28-
/**
29-
* @var array<int>
30-
*/
26+
/** @var array<int> */
3127
private array $stepProgress = [];
3228

33-
private readonly Terminal $terminal;
34-
3529
private int $lastPaint = 0;
30+
3631
public function __construct(Step ...$steps)
3732
{
3833
$this->steps = $steps;
3934
foreach ($this->steps as $step) {
4035
$this->stepsStatus[$step->key] = '🌀';
41-
if ($step->progressBer) {
42-
$this->itemsCountForStep[$step->key] = 0;
43-
$this->stepProgress[$step->key] = 0;
36+
if (! $step->progressBer) {
37+
continue;
4438
}
39+
40+
$this->itemsCountForStep[$step->key] = 0;
41+
$this->stepProgress[$step->key] = 0;
4542
}
46-
$this->terminal = terminal();
43+
44+
renderUsing(new OverWritingOutPut(new ConsoleOutput()));
4745
}
4846

4947
public function render(): void
5048
{
51-
$html = '<table>';
49+
$html = '<table>';
5250
$html .= '<thead>';
5351
$html .= '<tr>';
5452
$html .= '<th>Status</th>';
@@ -61,15 +59,16 @@ public function render(): void
6159
if ($step->progressBer && $this->itemsCountForStep[$step->key] > 0) {
6260
$progress = $this->stepProgress[$step->key] . '/' . $this->itemsCountForStep[$step->key];
6361
}
62+
6463
$html .= '<tr>';
6564
$html .= '<td>' . $this->stepsStatus[$step->key] . '</td>';
6665
$html .= '<td>' . $step->name . '</td>';
6766
$html .= '<td>' . $progress . '</td>';
6867
$html .= '</tr>';
6968
}
69+
7070
$html .= '</table>';
7171

72-
$this->terminal->clear();
7372
render($html);
7473
$this->lastPaint = time();
7574
}
@@ -94,6 +93,7 @@ public function markStepWontDo(string ...$keys): void
9493
foreach ($keys as $key) {
9594
$this->stepsStatus[$key] = '🚫';
9695
}
96+
9797
$this->render();
9898
}
9999

@@ -106,7 +106,7 @@ public function itemForStep(string $key, int $count): void
106106
public function advanceStep(string $key): void
107107
{
108108
$this->stepProgress[$key]++;
109-
$percentage = (100 / $this->itemsCountForStep[$key]) * $this->stepProgress[$key];
109+
$percentage = 100 / $this->itemsCountForStep[$key] * $this->stepProgress[$key];
110110
switch (true) {
111111
case $percentage <= 12.5:
112112
$this->stepsStatus[$key] = '🌑';

0 commit comments

Comments
 (0)