Skip to content

Commit f1dfd10

Browse files
committed
Merge branch 'check-generated-code'
2 parents 8f51c08 + d4d10d5 commit f1dfd10

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ concurrency:
1212
cancel-in-progress: true
1313

1414
jobs:
15-
format:
16-
name: Check formatting
15+
check:
16+
name: Check generated files and formatting
1717
runs-on: ubuntu-latest
1818

1919
steps:
@@ -26,15 +26,22 @@ jobs:
2626
php-version: "8.2"
2727
coverage: none
2828

29+
- name: Install dependencies
30+
run: composer install --no-interaction --no-progress
31+
32+
- name: Check generated files
33+
run: scripts/generate.php --check
34+
2935
- name: Install PrettyPHP
30-
run: composer create-project --no-interaction --no-progress --no-dev lkrms/pretty-php=0.4.25 build/pretty-php
36+
run: composer create-project --no-interaction --no-progress --no-dev lkrms/pretty-php=0.4.28 build/pretty-php
3137

3238
- name: Run PrettyPHP
3339
run: build/pretty-php/bin/pretty-php --diff
3440

3541
phpstan:
3642
name: PHPStan
3743
runs-on: ubuntu-latest
44+
needs: check
3845

3946
strategy:
4047
fail-fast: false
@@ -61,6 +68,7 @@ jobs:
6168

6269
unit-tests:
6370
name: PHPUnit tests
71+
needs: check
6472

6573
strategy:
6674
fail-fast: false

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
composer.phar
44

55
.env
6-
*.generated.php
76
*.log
87
/var
98
!/tests/.env

.prettyphp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"src": [
33
"bin",
44
"lib",
5+
"scripts",
56
"src",
67
"tests",
78
"bootstrap.php"

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"phpstan/extension-installer": "^1",
2828
"phpstan/phpstan": "^1",
2929
"phpstan/phpstan-deprecation-rules": "^1",
30-
"phpunit/phpunit": "^9"
30+
"phpunit/phpunit": "^9",
31+
"sebastian/diff": "^4 || ^5"
3132
},
3233
"autoload": {
3334
"psr-4": {

lib/lk-util/Command/Generate/Concept/GenerateCommand.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
77
use Lkrms\Cli\CliOption;
88
use Lkrms\Cli\CliOptionBuilder;
9+
use Lkrms\Console\Catalog\ConsoleLevel as Level;
910
use Lkrms\Facade\Composer;
1011
use Lkrms\Facade\Console;
1112
use Lkrms\Facade\File;
@@ -20,6 +21,8 @@
2021
use Lkrms\Support\TokenExtractor;
2122
use Lkrms\Utility\Convert;
2223
use Lkrms\Utility\Test;
24+
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
25+
use SebastianBergmann\Diff\Differ;
2326
use ReflectionClass;
2427
use ReflectionException;
2528
use ReflectionParameter;
@@ -40,6 +43,7 @@ abstract class GenerateCommand extends Command
4043
protected ?string $OutputDescription;
4144
protected ?bool $ToStdout;
4245
protected ?bool $ReplaceIfExists;
46+
protected ?bool $Check;
4347
protected ?bool $NoMetaTags;
4448

4549
/**
@@ -141,6 +145,10 @@ protected function getOutputOptionList(string $outputType): array
141145
->short('s')
142146
->description('Write to standard output')
143147
->bindTo($this->ToStdout),
148+
CliOption::build()
149+
->long('check')
150+
->description('Fail if the output file should be replaced')
151+
->bindTo($this->Check),
144152
CliOption::build()
145153
->long('force')
146154
->short('f')
@@ -556,25 +564,46 @@ protected function handleOutput($lines): void
556564
$verb = null;
557565
} else {
558566
$file = sprintf('%s.php', $this->OutputClass);
567+
$dir = Composer::getNamespacePath($this->OutputNamespace);
559568

560-
if ($dir = Composer::getNamespacePath($this->OutputNamespace)) {
561-
File::maybeCreateDirectory($dir);
569+
if ($dir !== null) {
570+
if (!$this->Check) {
571+
File::maybeCreateDirectory($dir);
572+
}
562573
$file = $dir . '/' . $file;
563574
}
564575

565576
if (file_exists($file)) {
566-
if (rtrim(file_get_contents($file)) == rtrim($output)) {
567-
Console::log('Unchanged:', $file);
568-
577+
$input = file_get_contents($file);
578+
if ($input === $output) {
579+
Console::log('Nothing to do:', $file);
569580
return;
570581
}
571-
if (!$this->ReplaceIfExists) {
572-
Console::warn('File already exists:', $file);
573-
$file = substr($file, 0, -4) . '.generated.php';
582+
if ($this->Check) {
583+
Console::info('Would replace', $file);
584+
Console::count(Level::ERROR);
585+
$this->setExitStatus(1);
586+
return;
574587
}
575-
if (file_exists($file)) {
576-
$verb = 'Replacing';
588+
if (!$this->ReplaceIfExists) {
589+
$basePath = Composer::getRootPackagePath();
590+
$relative = File::realpath($file);
591+
if (strpos($relative, $basePath) === 0) {
592+
$relative = substr($relative, strlen($basePath) + 1);
593+
}
594+
print (new Differ(new StrictUnifiedDiffOutputBuilder([
595+
'fromFile' => "a/$relative",
596+
'toFile' => "b/$relative",
597+
])))->diff($input, $output);
598+
Console::info('Out of date:', $file);
599+
return;
577600
}
601+
$verb = 'Replacing';
602+
} elseif ($this->Check) {
603+
Console::info('Would create', $file);
604+
$this->setExitStatus(1);
605+
Console::count(Level::ERROR);
606+
return;
578607
}
579608
}
580609

scripts/generate.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use Lkrms\Console\ConsoleWriter;
77
use Lkrms\Container\Application;
88
use Lkrms\Container\Container;
9-
use Lkrms\Curler\Curler;
109
use Lkrms\Curler\Support\CurlerPage;
10+
use Lkrms\Curler\Curler;
11+
use Lkrms\Facade\Console;
1112
use Lkrms\LkUtil\Catalog\EnvVar;
1213
use Lkrms\LkUtil\Command\Generate\GenerateBuilder;
1314
use Lkrms\LkUtil\Command\Generate\GenerateFacade;
@@ -73,10 +74,22 @@
7374
$app->env()->unset($constant->getValue());
7475
}
7576

77+
$args = [
78+
'--force',
79+
'--no-meta',
80+
...array_slice($argv, 1),
81+
];
82+
83+
$status = 0;
84+
7685
foreach ($facades as $class => $facade) {
77-
$generateFacade('-f', '-m', $class, $facade);
86+
$status |= $generateFacade(...[...$args, $class, $facade]);
7887
}
7988

8089
foreach ($builders as $class => $builder) {
81-
$generateBuilder('-f', '-m', $class, $builder);
90+
$status |= $generateBuilder(...[...$args, $class, $builder]);
8291
}
92+
93+
Console::summary('Code generation completed');
94+
95+
exit ($status);

src/Facade/Sync.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Lkrms\Concept\Facade;
66
use Lkrms\Store\Concept\SqliteStore;
77
use Lkrms\Sync\Contract\ISyncClassResolver;
8+
use Lkrms\Sync\Contract\ISyncContext;
89
use Lkrms\Sync\Contract\ISyncEntity;
910
use Lkrms\Sync\Contract\ISyncProvider;
1011
use Lkrms\Sync\Support\DeferredSyncEntity;
@@ -26,7 +27,7 @@
2627
* @method static SyncStore entity(int $providerId, class-string<ISyncEntity> $entityType, int|string $entityId, ISyncEntity $entity) Register a sync entity (see {@see SyncStore::entity()})
2728
* @method static SyncStore entityType(class-string<ISyncEntity> $entity) Register a sync entity type and set its ID (unless already registered) (see {@see SyncStore::entityType()})
2829
* @method static SyncStore error(SyncError|ErrorBuilder $error, bool $deduplicate = false, bool $toConsole = false) Report an error that occurred during a sync operation
29-
* @method static ISyncEntity|null getEntity(int $providerId, class-string<ISyncEntity> $entityType, int|string $entityId) Get a previously registered sync entity
30+
* @method static ISyncEntity|DeferredSyncEntity|null getEntity(int $providerId, class-string<ISyncEntity> $entityType, int|string $entityId, ?ISyncContext $context = null) Get a previously registered sync entity
3031
* @method static string|null getEntityTypeNamespace(class-string<ISyncEntity> $entity) Get the namespace of a sync entity type (see {@see SyncStore::getEntityTypeNamespace()})
3132
* @method static string|null getEntityTypeUri(class-string<ISyncEntity> $entity, bool $compact = true) Get the canonical URI of a sync entity type (see {@see SyncStore::getEntityTypeUri()})
3233
* @method static SyncErrorCollection getErrors() Get sync operation errors recorded so far
@@ -38,6 +39,7 @@
3839
* @method static bool isOpen() Check if a database is open
3940
* @method static SyncStore namespace(string $prefix, string $uri, string $namespace, class-string<ISyncClassResolver>|null $resolver = null) Register a sync entity namespace (see {@see SyncStore::namespace()})
4041
* @method static SyncStore provider(ISyncProvider $provider) Register a sync provider and set its provider ID (see {@see SyncStore::provider()})
42+
* @method static SyncStore reserveEntity(int $providerId, class-string<ISyncEntity> $entityType, int|string $entityId) Pre-register a sync entity (see {@see SyncStore::reserveEntity()})
4143
*
4244
* @uses SyncStore
4345
*

0 commit comments

Comments
 (0)