Skip to content

Commit 190927a

Browse files
committed
Skip unchanged files
This feature is key for clients that apply a CS fixer after generation. This can easily save minutes per client generation.
1 parent bfded35 commit 190927a

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/Generator.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\PrettyPrinter\Standard;
1414
use Safe\Exceptions\FilesystemException;
1515

16+
use function array_filter;
1617
use function array_map;
1718
use function dirname;
1819
use function file_exists;
@@ -29,6 +30,7 @@
2930
use function sys_get_temp_dir;
3031
use function trim;
3132
use function uniqid;
33+
use function usleep;
3234

3335
use const DIRECTORY_SEPARATOR;
3436

@@ -77,20 +79,36 @@ public static function generate(Configuration $configuration, string $configurat
7779
$fileName .= (is_string($file->contents) && strpos($file->contents, '<?php') === false ? '' : '.php');
7880
$fileContents = $fileStringyfier->toString($file);
7981
$fileContentsHash = md5($fileContents);
80-
try {
81-
/** @phpstan-ignore-next-line */
82-
@mkdir(dirname($fileName), 0744, true);
83-
} catch (FilesystemException) {
84-
// @ignoreException
82+
83+
if (
84+
! $state->generatedFiles->has($fileName) ||
85+
$state->generatedFiles->get($fileName)->hash !== $fileContentsHash
86+
) {
87+
try {
88+
/** @phpstan-ignore-next-line */
89+
@mkdir(dirname($fileName), 0744, true);
90+
} catch (FilesystemException) {
91+
// @ignoreException
92+
}
93+
94+
file_put_contents($fileName, $fileContents);
95+
$state->generatedFiles->upsert($fileName, $fileContentsHash);
96+
97+
while (! file_exists($fileName) || $fileContentsHash !== md5(file_get_contents($fileName))) {
98+
usleep(100);
99+
}
85100
}
86101

87-
file_put_contents($fileName, $fileContents);
88-
$state->generatedFiles->upsert($fileName, $fileContentsHash);
102+
$existingFiles = array_filter(
103+
$existingFiles,
104+
static fn (string $file): bool => $file !== $fileName,
105+
);
89106

90107
if ($file->loadOnWrite === \OpenAPITools\Utils\File::DO_NOT_LOAD_ON_WRITE) {
91108
continue;
92109
}
93110

111+
/** @psalm-suppress UnresolvableInclude */
94112
include_once $fileName;
95113
}
96114
}
@@ -104,7 +122,7 @@ public static function generate(Configuration $configuration, string $configurat
104122
$state->additionalFiles->remove($file->name);
105123
}
106124

107-
foreach ($configuration->state->additionalFiles ?? [] as $additionalFile) {
125+
foreach ($package->state->additionalFiles ?? [] as $additionalFile) {
108126
$state->additionalFiles->upsert(
109127
$additionalFile,
110128
file_exists($configurationLocation . $package->destination->root . DIRECTORY_SEPARATOR . $additionalFile) ? self::hash(file_get_contents($configurationLocation . $package->destination->root . DIRECTORY_SEPARATOR . $additionalFile)) : '',

0 commit comments

Comments
 (0)