-
Notifications
You must be signed in to change notification settings - Fork 27
/
Plugin.php
182 lines (147 loc) · 4.56 KB
/
Plugin.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
<?php declare(strict_types = 1);
namespace PHPStan\ExtensionInstaller;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Util\Filesystem;
use function array_keys;
use function dirname;
use function file_exists;
use function file_put_contents;
use function getcwd;
use function in_array;
use function is_file;
use function ksort;
use function md5;
use function md5_file;
use function sort;
use function sprintf;
use function strpos;
use function var_export;
use const DIRECTORY_SEPARATOR;
final class Plugin implements PluginInterface, EventSubscriberInterface
{
/** @var string */
private static $generatedFileTemplate = <<<'PHP'
<?php declare(strict_types = 1);
namespace PHPStan\ExtensionInstaller;
/**
* This class is generated by phpstan/extension-installer.
* @internal
*/
final class GeneratedConfig
{
public const EXTENSIONS = %s;
public const NOT_INSTALLED = %s;
private function __construct()
{
}
}
PHP;
public function activate(Composer $composer, IOInterface $io): void
{
// noop
}
public function deactivate(Composer $composer, IOInterface $io): void
{
// noop
}
public function uninstall(Composer $composer, IOInterface $io): void
{
// noop
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
ScriptEvents::POST_INSTALL_CMD => 'process',
ScriptEvents::POST_UPDATE_CMD => 'process',
];
}
public function process(Event $event): void
{
$io = $event->getIO();
if (!file_exists(__DIR__)) {
$io->write('<info>phpstan/extension-installer:</info> Package not found (probably scheduled for removal); extensions installation skipped.');
return;
}
$composer = $event->getComposer();
$installationManager = $composer->getInstallationManager();
$generatedConfigFilePath = __DIR__ . '/GeneratedConfig.php';
$oldGeneratedConfigFileHash = null;
if (is_file($generatedConfigFilePath)) {
$oldGeneratedConfigFileHash = md5_file($generatedConfigFilePath);
}
$notInstalledPackages = [];
$installedPackages = [];
$ignoredPackages = [];
$data = [];
$fs = new Filesystem();
$ignore = [];
$packageExtra = $composer->getPackage()->getExtra();
if (isset($packageExtra['phpstan/extension-installer']['ignore'])) {
$ignore = $packageExtra['phpstan/extension-installer']['ignore'];
}
foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) {
if (
$package->getType() !== 'phpstan-extension'
&& !isset($package->getExtra()['phpstan'])
) {
if (
strpos($package->getName(), 'phpstan') !== false
&& !in_array($package->getName(), [
'phpstan/phpstan',
'phpstan/phpstan-shim',
'phpstan/phpdoc-parser',
'phpstan/extension-installer',
], true)
) {
$notInstalledPackages[$package->getName()] = $package->getFullPrettyVersion();
}
continue;
}
if (in_array($package->getName(), $ignore, true)) {
$ignoredPackages[] = $package->getName();
continue;
}
$installPath = $installationManager->getInstallPath($package);
if ($installPath === null) {
continue;
}
$absoluteInstallPath = $fs->isAbsolutePath($installPath)
? $installPath
: getcwd() . DIRECTORY_SEPARATOR . $installPath;
$data[$package->getName()] = [
'install_path' => $absoluteInstallPath,
'relative_install_path' => $fs->findShortestPath(dirname($generatedConfigFilePath), $absoluteInstallPath, true),
'extra' => $package->getExtra()['phpstan'] ?? null,
'version' => $package->getFullPrettyVersion(),
];
$installedPackages[$package->getName()] = true;
}
ksort($data);
ksort($installedPackages);
ksort($notInstalledPackages);
sort($ignoredPackages);
$generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true));
file_put_contents($generatedConfigFilePath, $generatedConfigFileContents);
$io->write('<info>phpstan/extension-installer:</info> Extensions installed');
if ($oldGeneratedConfigFileHash === md5($generatedConfigFileContents)) {
return;
}
foreach (array_keys($installedPackages) as $name) {
$io->write(sprintf('> <info>%s:</info> installed', $name));
}
foreach (array_keys($notInstalledPackages) as $name) {
$io->write(sprintf('> <comment>%s:</comment> not supported', $name));
}
foreach ($ignoredPackages as $name) {
$io->write(sprintf('> <comment>%s:</comment> ignored', $name));
}
}
}