Skip to content

Commit 3043e7b

Browse files
committed
[generator] better error handling
- reset docs to master before building DTD entities from them - use symfony Finder to remove all generated files, instead of glob to remove some of them - less spammy log output (unless --verbose is passed) - more detailed error messages
1 parent bab89f5 commit 3043e7b

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

generator/src/Commands/GenerateCommand.php

+30-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Safe\Generator\FileCreator;
1010
use Safe\Generator\ComposerJsonEditor;
1111

12+
use Symfony\Component\Finder\Finder;
1213
use Symfony\Component\Console\Command\Command;
1314
use Symfony\Component\Console\Input\InputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
@@ -30,6 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3031
$this->rmGenerated();
3132

3233
// Let's build the DTD necessary to load the XML files.
34+
$this->checkout(DocPage::findReferenceDir(), "master");
3335
DocPage::buildEntities();
3436

3537
// PHP documentation is a living document, which broadly reflects
@@ -64,7 +66,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6466
$this->checkout(DocPage::findReferenceDir(), $commit);
6567
$scanner = new Scanner(DocPage::findReferenceDir());
6668
$res = $scanner->getMethods($scanner->getFunctionsPaths(), $output);
67-
$output->writeln('These functions have been ignored and must be dealt with manually: '.\implode(', ', $res->overloadedFunctions));
69+
$output->writeln(
70+
'Functions have been ignored and must be dealt with manually: ' .
71+
($output->isVerbose() ?
72+
implode(', ', $res->overloadedFunctions) :
73+
count($res->overloadedFunctions) . ' functions'
74+
)
75+
);
6876

6977
$currentFunctionsByName = [];
7078
foreach ($res->methods as $function) {
@@ -83,8 +91,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8391
$missingMethods[] = $oldFunction;
8492
}
8593
}
86-
$output->writeln('Methods no longer need safe wrappers in ' . $version . ': ' .
87-
\implode(', ', \array_map(fn($m) => $m->getFunctionName(), $missingMethods)));
94+
$output->writeln(
95+
'Functions no longer need safe wrappers in ' . $version . ': ' .
96+
($output->isVerbose() ?
97+
\implode(', ', \array_map(fn($m) => $m->getFunctionName(), $missingMethods)) :
98+
count($missingMethods) . ' functions'
99+
)
100+
);
88101

89102
$genDir = FileCreator::getSafeRootDir() . "/generated/$version";
90103
$fileCreator = new FileCreator();
@@ -111,32 +124,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
111124

112125
private function checkout(string $dir, string $commit): void
113126
{
127+
$process = new Process(['git', 'clean', '-fdx'], $dir);
128+
$process->setTimeout(10);
129+
$code = $process->run();
130+
if ($code !== 0) {
131+
throw new \RuntimeException("Failed to git-clean in $dir (exit $code):\n{$process->getErrorOutput()}");
132+
}
133+
114134
$process = new Process(['git', 'checkout', $commit], $dir);
115135
$process->setTimeout(10);
116136
$code = $process->run();
117137
if ($code !== 0) {
118-
throw new \RuntimeException("Failed to checkout $commit in $dir");
138+
throw new \RuntimeException("Failed to checkout $commit in $dir (exit $code):\n{$process->getErrorOutput()}");
119139
}
120140
}
121141

122142
private function rmGenerated(): void
123143
{
124-
$exceptions = \glob(FileCreator::getSafeRootDir() . '/generated/Exceptions/*.php');
125-
if ($exceptions === false) {
126-
throw new \RuntimeException('Failed to require the generated exception files');
127-
}
128-
129-
foreach ($exceptions as $exception) {
130-
\unlink($exception);
131-
}
132-
133-
$files = \glob(FileCreator::getSafeRootDir() . '/generated/*.php');
134-
if ($files === false) {
135-
throw new \RuntimeException('Failed to require the generated files');
136-
}
137-
138-
foreach ($files as $file) {
139-
\unlink($file);
144+
$finder = new Finder();
145+
$finder->in(FileCreator::getSafeRootDir() . "/generated");
146+
foreach ($finder as $file) {
147+
if ($file->isFile()) {
148+
\unlink($file->getPathname());
149+
}
140150
}
141151

142152
if (\file_exists(DocPage::findDocDir() . '/entities/generated.ent')) {

0 commit comments

Comments
 (0)