Skip to content

Commit

Permalink
PHAR - rename file extensions of PhpStorm stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 25, 2020
1 parent 4f51382 commit e18b3b4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"nette/neon": "^3.0.0",
"symfony/console": "^4.1",
"symfony/process": "^4.1",
"symfony/filesystem": "^4.1"
"symfony/filesystem": "^4.1",
"symfony/finder": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
38 changes: 38 additions & 0 deletions compiler/src/Console/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->processFactory->setOutput($output);

$this->fixComposerJson($this->buildDir);
$this->renamePhpStormStubs();
$this->processFactory->create(['composer', 'update', '--no-dev', '--classmap-authoritative'], $this->buildDir);

$this->processFactory->create(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir);
Expand Down Expand Up @@ -76,4 +77,41 @@ private function fixComposerJson(string $buildDir): void
$this->filesystem->write($buildDir . '/composer.json', $encodedJson);
}

private function renamePhpStormStubs(): void
{
$directory = $this->buildDir . '/vendor/jetbrains/phpstorm-stubs';
if (!is_dir($directory)) {
return;
}

$stubFinder = \Symfony\Component\Finder\Finder::create();
$stubsMapPath = $directory . '/PhpStormStubsMap.php';
foreach ($stubFinder->files()->name('*.php')->in($directory) as $stubFile) {
$path = $stubFile->getPathname();
if ($path === $stubsMapPath) {
continue;
}

$renameSuccess = rename(
$path,
dirname($path) . '/' . $stubFile->getBasename('.php') . '.stub'
);
if ($renameSuccess === false) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Could not rename %s', $path));
}
}

$stubsMapContents = file_get_contents($stubsMapPath);
if ($stubsMapContents === false) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Could not read %s', $stubsMapPath));
}

$stubsMapContents = str_replace('.php\',', '.stub\',', $stubsMapContents);

$putSuccess = file_put_contents($directory . '/PhpStormStubsMap.php', $stubsMapContents);
if ($putSuccess === false) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Could not write %s', $stubsMapPath));
}
}

}

0 comments on commit e18b3b4

Please sign in to comment.