-
Notifications
You must be signed in to change notification settings - Fork 428
/
CompileCommand.php
224 lines (189 loc) · 6.02 KB
/
CompileCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php declare(strict_types = 1);
namespace PHPStan\Compiler\Console;
use Exception;
use PHPStan\Compiler\Filesystem\Filesystem;
use PHPStan\Compiler\Process\ProcessFactory;
use PHPStan\ShouldNotHappenException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use function chdir;
use function dirname;
use function escapeshellarg;
use function exec;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function is_dir;
use function json_decode;
use function json_encode;
use function realpath;
use function rename;
use function sprintf;
use function str_replace;
use function strlen;
use function substr;
use function unlink;
use function var_export;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
final class CompileCommand extends Command
{
public function __construct(
private Filesystem $filesystem,
private ProcessFactory $processFactory,
private string $dataDir,
private string $buildDir,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->setName('phpstan:compile')
->setDescription('Compile PHAR');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->processFactory->setOutput($output);
$this->buildPreloadScript();
$this->deleteUnnecessaryVendorCode();
$this->fixComposerJson($this->buildDir);
$this->renamePhpStormStubs();
$this->renamePhp8Stubs();
$this->transformSource();
$this->processFactory->create(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir);
return 0;
}
private function fixComposerJson(string $buildDir): void
{
$json = json_decode($this->filesystem->read($buildDir . '/composer.json'), true);
unset($json['replace']);
$json['name'] = 'phpstan/phpstan';
$json['require']['php'] = '^7.1';
// simplify autoload (remove not packed build directory]
$json['autoload']['psr-4']['PHPStan\\'] = 'src/';
$encodedJson = json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
if ($encodedJson === false) {
throw new Exception('json_encode() was not successful.');
}
$this->filesystem->write($buildDir . '/composer.json', $encodedJson);
}
private function renamePhpStormStubs(): void
{
$directory = $this->buildDir . '/vendor/jetbrains/phpstorm-stubs';
if (!is_dir($directory)) {
return;
}
$stubFinder = Finder::create();
$stubsMapPath = realpath($directory . '/PhpStormStubsMap.php');
if ($stubsMapPath === false) {
throw new Exception('realpath() failed');
}
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 ShouldNotHappenException(sprintf('Could not rename %s', $path));
}
}
$stubsMapContents = file_get_contents($stubsMapPath);
if ($stubsMapContents === false) {
throw new ShouldNotHappenException(sprintf('Could not read %s', $stubsMapPath));
}
$stubsMapContents = str_replace('.php\',', '.stub\',', $stubsMapContents);
$putSuccess = file_put_contents($stubsMapPath, $stubsMapContents);
if ($putSuccess === false) {
throw new ShouldNotHappenException(sprintf('Could not write %s', $stubsMapPath));
}
}
private function renamePhp8Stubs(): void
{
$directory = $this->buildDir . '/vendor/phpstan/php-8-stubs/stubs';
if (!is_dir($directory)) {
return;
}
$stubFinder = Finder::create();
$stubsMapPath = $directory . '/../Php8StubsMap.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 ShouldNotHappenException(sprintf('Could not rename %s', $path));
}
}
$stubsMapContents = file_get_contents($stubsMapPath);
if ($stubsMapContents === false) {
throw new ShouldNotHappenException(sprintf('Could not read %s', $stubsMapPath));
}
$stubsMapContents = str_replace('.php\',', '.stub\',', $stubsMapContents);
$putSuccess = file_put_contents($stubsMapPath, $stubsMapContents);
if ($putSuccess === false) {
throw new ShouldNotHappenException(sprintf('Could not write %s', $stubsMapPath));
}
}
private function buildPreloadScript(): void
{
$vendorDir = $this->buildDir . '/vendor';
if (!is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
return;
}
$preloadScript = $this->buildDir . '/preload.php';
$template = <<<'php'
<?php declare(strict_types = 1);
%s
php;
$finder = Finder::create();
$root = realpath(__DIR__ . '/../../..');
if ($root === false) {
return;
}
$output = '';
foreach ($finder->files()->name('*.php')->in([
$this->buildDir . '/src',
$vendorDir . '/nikic/php-parser/lib/PhpParser',
$vendorDir . '/phpstan/phpdoc-parser/src',
])->exclude([
'Testing',
]) as $phpFile) {
$realPath = $phpFile->getRealPath();
if ($realPath === false) {
return;
}
$path = substr($realPath, strlen($root));
$output .= 'require_once __DIR__ . ' . var_export($path, true) . ';' . "\n";
}
file_put_contents($preloadScript, sprintf($template, $output));
}
private function deleteUnnecessaryVendorCode(): void
{
$vendorDir = $this->buildDir . '/vendor';
if (!is_dir($vendorDir . '/nikic/php-parser')) {
return;
}
@unlink($vendorDir . '/nikic/php-parser/grammar/rebuildParsers.php');
@unlink($vendorDir . '/nikic/php-parser/bin/php-parse');
}
private function transformSource(): void
{
chdir(__DIR__ . '/../../..');
exec(escapeshellarg(__DIR__ . '/../../../build/transform-source') . ' 7.1', $outputLines, $exitCode);
if ($exitCode === 0) {
return;
}
throw new ShouldNotHappenException(implode("\n", $outputLines));
}
}