/
PuliPlugin.php
713 lines (593 loc) · 23.1 KB
/
PuliPlugin.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
<?php
/*
* This file is part of the puli/composer-plugin package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Puli\ComposerPlugin;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\CommandEvent;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Exception;
use RuntimeException;
use Webmozart\PathUtil\Path;
/**
* A Puli plugin for Composer.
*
* The plugin updates the Puli package repository based on the Composer
* packages whenever `composer install` or `composer update` is executed.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PuliPlugin implements PluginInterface, EventSubscriberInterface
{
/**
* The minimum version of the Puli CLI.
*/
const MIN_CLI_VERSION = '1.0.0-beta9';
/**
* The maximum version of the Puli CLI.
*/
const MAX_CLI_VERSION = '1.999.99999';
/**
* The name of the installer.
*/
const INSTALLER_NAME = 'composer';
/**
* @var bool
*/
private $initialized = false;
/**
* @var PuliRunner
*/
private $puliRunner;
/**
* @var string
*/
private $rootDir;
/**
* @var bool
*/
private $runPostInstall = true;
/**
* @var bool
*/
private $runPostAutoloadDump = true;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
ScriptEvents::POST_INSTALL_CMD => 'postInstall',
ScriptEvents::POST_UPDATE_CMD => 'postInstall',
ScriptEvents::POST_AUTOLOAD_DUMP => 'postAutoloadDump',
);
}
public function __construct(PuliRunner $puliRunner = null)
{
$this->puliRunner = $puliRunner;
$this->rootDir = Path::normalize(getcwd());
}
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$composer->getEventDispatcher()->addSubscriber($this);
}
public function postAutoloadDump(Event $event)
{
// Plugin has been uninstalled
if (!file_exists(__FILE__)) {
return;
}
if (!$this->initialized) {
$this->initialize($event->getComposer(), $event->getIO());
}
// This method is called twice. Run it only once.
if (!$this->runPostAutoloadDump) {
return;
}
$this->runPostAutoloadDump = false;
$io = $event->getIO();
$compConfig = $event->getComposer()->getConfig();
$vendorDir = $compConfig->get('vendor-dir');
// On TravisCI, $vendorDir is a relative path. Probably an old Composer
// build or something. Usually, $vendorDir should be absolute already.
$vendorDir = Path::makeAbsolute($vendorDir, $this->rootDir);
$autoloadFile = $vendorDir.'/autoload.php';
$classMapFile = $vendorDir.'/composer/autoload_classmap.php';
try {
$factoryClass = $this->getConfigKey('factory.in.class');
$factoryFile = $this->getConfigKey('factory.in.file');
} catch (PuliRunnerException $e) {
$this->printWarning($io, 'Could not load Puli configuration', $e);
return;
}
$factoryFile = Path::makeAbsolute($factoryFile, $this->rootDir);
$this->insertFactoryClassConstant($io, $autoloadFile, $factoryClass);
$this->insertFactoryClassMap($io, $classMapFile, $vendorDir, $factoryClass, $factoryFile);
$this->setBootstrapFile($io, $autoloadFile);
}
/**
* Updates the Puli repository after Composer installations/updates.
*
* @param CommandEvent $event The Composer event.
*/
public function postInstall(CommandEvent $event)
{
// Plugin has been uninstalled
if (!file_exists(__FILE__)) {
return;
}
if (!$this->initialized) {
$this->initialize($event->getComposer(), $event->getIO());
}
// This method is called twice. Run it only once.
if (!$this->runPostInstall) {
return;
}
$this->runPostInstall = false;
$io = $event->getIO();
$io->write('<info>Looking for updated Puli packages</info>');
$rootPackage = $event->getComposer()->getPackage();
$composerPackages = $this->loadComposerPackages($event->getComposer());
$prodPackageNames = $this->filterProdPackageNames($composerPackages, $rootPackage);
$env = $event->isDevMode() ? PuliPackage::ENV_DEV : PuliPackage::ENV_PROD;
try {
$puliPackages = $this->loadPuliPackages();
} catch (PuliRunnerException $e) {
$this->printWarning($io, 'Could not load Puli packages', $e);
return;
}
// Don't remove non-existing packages in production environment
// Removed packages could be dev dependencies (i.e. "require-dev"
// of the root package or "require" of another dev dependency), and
// we can't find out whether they are since Composer doesn't load them
if (PuliPackage::ENV_PROD !== $env) {
$this->removeRemovedPackages($composerPackages, $puliPackages, $io);
}
$this->installNewPackages($composerPackages, $prodPackageNames, $puliPackages, $io, $event->getComposer());
// Don't print warnings for non-existing packages in production
if (PuliPackage::ENV_PROD !== $env) {
$this->checkForNotFoundErrors($puliPackages, $io);
}
$this->checkForNotLoadableErrors($puliPackages, $io);
$this->adoptComposerName($puliPackages, $io, $event->getComposer());
$this->buildPuli($io);
}
/**
* @param Composer $composer
* @param IOInterface $io
*/
private function initialize(Composer $composer, IOInterface $io)
{
// This method must be run after all packages are installed, otherwise
// it could be that the puli/cli is not yet installed and hence the
// CLI is not available
// Previously, this was called in activate(), which is called
// immediately after installing the plugin, but potentially before
// installing the CLI
$this->initialized = true;
// Keep the manually set runner
if (!$this->puliRunner) {
try {
// Add Composer's bin directory in case the "puli" executable is
// installed with Composer
$this->puliRunner = new PuliRunner($composer->getConfig()->get('bin-dir'));
} catch (RuntimeException $e) {
$this->printWarning($io, 'Plugin initialization failed', $e);
$this->runPostAutoloadDump = false;
$this->runPostInstall = false;
}
}
// Use the runner to verify if Puli has the right version
try {
$this->verifyPuliVersion();
} catch (RuntimeException $e) {
$this->printWarning($io, 'Version check failed', $e);
$this->runPostAutoloadDump = false;
$this->runPostInstall = false;
}
}
/**
* @param PackageInterface[] $composerPackages
* @param bool[] $prodPackageNames
* @param PuliPackage[] $puliPackages
* @param IOInterface $io
* @param Composer $composer
*/
private function installNewPackages(array $composerPackages, array $prodPackageNames, array &$puliPackages, IOInterface $io, Composer $composer)
{
$installationManager = $composer->getInstallationManager();
foreach ($composerPackages as $packageName => $package) {
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
// We need to normalize the system-dependent paths returned by Composer
$installPath = Path::normalize($installationManager->getInstallPath($package));
$env = isset($prodPackageNames[$packageName]) ? PuliPackage::ENV_PROD : PuliPackage::ENV_DEV;
// Skip meta packages
if ('' === $installPath) {
continue;
}
if (isset($puliPackages[$packageName])) {
$puliPackage = $puliPackages[$packageName];
// Only proceed if the install path or environment has changed
if ($installPath === $puliPackage->getInstallPath() && $env === $puliPackage->getEnvironment()) {
continue;
}
// Only remove packages installed by Composer
if (self::INSTALLER_NAME === $puliPackage->getInstallerName()) {
$io->write(sprintf(
'Reinstalling <info>%s</info> (<comment>%s</comment>) in <comment>%s</comment>',
$packageName,
Path::makeRelative($installPath, $this->rootDir),
$env
));
try {
$this->removePackage($packageName);
} catch (PuliRunnerException $e) {
$this->printPackageWarning($io, 'Could not remove package "%s" (at ./%s)', $packageName, $installPath, $e);
continue;
}
}
} else {
$io->write(sprintf(
'Installing <info>%s</info> (<comment>%s</comment>) in <comment>%s</comment>',
$packageName,
Path::makeRelative($installPath, $this->rootDir),
$env
));
}
try {
$this->installPackage($installPath, $packageName, $env);
} catch (PuliRunnerException $e) {
$this->printPackageWarning($io, 'Could not install package "%s" (at ./%s)', $packageName, $installPath, $e);
continue;
}
$puliPackages[$packageName] = new PuliPackage(
$packageName,
self::INSTALLER_NAME,
$installPath,
PuliPackage::STATE_ENABLED,
$env
);
}
}
/**
* @param PackageInterface[] $composerPackages
* @param PuliPackage[] $puliPackages
* @param IOInterface $io
*/
private function removeRemovedPackages(array $composerPackages, array &$puliPackages, IOInterface $io)
{
/** @var PuliPackage[] $notFoundPackages */
$notFoundPackages = array_filter($puliPackages, function (PuliPackage $package) {
return PuliPackage::STATE_NOT_FOUND === $package->getState()
&& PuliPlugin::INSTALLER_NAME === $package->getInstallerName();
});
foreach ($notFoundPackages as $packageName => $package) {
// Check whether package was only moved
if (isset($composerPackages[$packageName])) {
continue;
}
$io->write(sprintf(
'Removing <info>%s</info> (<comment>%s</comment>)',
$packageName,
Path::makeRelative($package->getInstallPath(), $this->rootDir)
));
try {
$this->removePackage($packageName);
} catch (PuliRunnerException $e) {
$this->printPackageWarning($io, 'Could not remove package "%s" (at ./%s)', $packageName, $package->getInstallPath(), $e);
continue;
}
unset($puliPackages[$packageName]);
}
}
private function checkForNotFoundErrors(array $puliPackages, IOInterface $io)
{
/** @var PuliPackage[] $notFoundPackages */
$notFoundPackages = array_filter($puliPackages,
function (PuliPackage $package) {
return PuliPackage::STATE_NOT_FOUND === $package->getState()
&& PuliPlugin::INSTALLER_NAME === $package->getInstallerName();
});
foreach ($notFoundPackages as $package) {
$this->printPackageWarning(
$io,
'The package "%s" (at ./%s) could not be found',
$package->getName(),
$package->getInstallPath()
);
}
}
private function checkForNotLoadableErrors(array $puliPackages, IOInterface $io)
{
/** @var PuliPackage[] $notLoadablePackages */
$notLoadablePackages = array_filter($puliPackages, function (PuliPackage $package) {
return PuliPackage::STATE_NOT_LOADABLE === $package->getState()
&& PuliPlugin::INSTALLER_NAME === $package->getInstallerName();
});
foreach ($notLoadablePackages as $package) {
$this->printPackageWarning(
$io,
'The package "%s" (at ./%s) could not be loaded',
$package->getName(),
$package->getInstallPath()
);
}
}
private function adoptComposerName(array $puliPackages, IOInterface $io, Composer $composer)
{
$rootDir = $this->rootDir;
/** @var PuliPackage[] $rootPackages */
$rootPackages = array_filter($puliPackages, function (PuliPackage $package) use ($rootDir) {
return !$package->getInstallerName() && $rootDir === $package->getInstallPath();
});
if (0 === count($rootPackages)) {
// This should never happen
$this->printWarning($io, 'No root package could be found');
return;
}
if (count($rootPackages) > 1) {
// This should never happen
$this->printWarning($io, 'More than one root package was found');
return;
}
/** @var PuliPackage $rootPackage */
$rootPackage = reset($rootPackages);
$name = $rootPackage->getName();
$newName = $composer->getPackage()->getName();
// Rename the root package after changing the name in composer.json
if ($name !== $newName) {
try {
$this->renamePackage($name, $newName);
} catch (PuliRunnerException $e) {
$this->printWarning($io, sprintf(
'Could not rename root package to "%s"',
$newName
), $e);
}
}
}
private function insertFactoryClassConstant(IOInterface $io, $autoloadFile, $factoryClass)
{
if (!file_exists($autoloadFile)) {
throw new PuliPluginException(sprintf(
'Could not adjust autoloader: The file %s was not found.',
$autoloadFile
));
}
$io->write('<info>Generating PULI_FACTORY_CLASS constant</info>');
$contents = file_get_contents($autoloadFile);
$escFactoryClass = var_export($factoryClass, true);
$constant = "if (!defined('PULI_FACTORY_CLASS')) {\n";
$constant .= sprintf(" define('PULI_FACTORY_CLASS', %s);\n", $escFactoryClass);
$constant .= "}\n\n";
// Regex modifiers:
// "m": \s matches newlines
// "D": $ matches at EOF only
// Translation: insert before the last "return" in the file
$contents = preg_replace('/\n(?=return [^;]+;\s*$)/mD', "\n".$constant,
$contents);
file_put_contents($autoloadFile, $contents);
}
private function insertFactoryClassMap(IOInterface $io, $classMapFile, $vendorDir, $factoryClass, $factoryFile)
{
if (!file_exists($classMapFile)) {
throw new PuliPluginException(sprintf(
'Could not adjust autoloader: The file %s was not found.',
$classMapFile
));
}
$io->write(sprintf('<info>Registering %s with the class-map autoloader</info>', $factoryClass));
$relFactoryFile = Path::makeRelative($factoryFile, $vendorDir);
$escFactoryClass = var_export($factoryClass, true);
$escFactoryFile = var_export('/'.$relFactoryFile, true);
$classMap = sprintf("\n %s => \$vendorDir . %s,", $escFactoryClass, $escFactoryFile);
$contents = file_get_contents($classMapFile);
// Regex modifiers:
// "m": \s matches newlines
// "D": $ matches at EOF only
// Translation: insert before the last ");" in the file
$contents = preg_replace('/\n(?=\);\s*$)/mD', "\n".$classMap, $contents);
file_put_contents($classMapFile, $contents);
}
private function setBootstrapFile(IOInterface $io, $autoloadFile)
{
$bootstrapFile = $this->getConfigKey('bootstrap-file');
// Don't change user-defined bootstrap files
if (!empty($bootstrapFile)) {
return;
}
$relAutoloadFile = Path::makeRelative($autoloadFile, $this->rootDir);
$io->write(sprintf('<info>Setting "bootstrap-file" to "%s"</info>', $relAutoloadFile));
$this->setConfigKey('bootstrap-file', $relAutoloadFile);
}
/**
* Loads Composer's currently installed packages.
*
* @param Composer $composer The Composer instance.
*
* @return PackageInterface[] The installed packages indexed by their names.
*/
private function loadComposerPackages(Composer $composer)
{
$repository = $composer->getRepositoryManager()->getLocalRepository();
$packages = array();
foreach ($repository->getPackages() as $package) {
/* @var PackageInterface $package */
$packages[$package->getName()] = $package;
}
return $packages;
}
private function getConfigKey($key)
{
$value = trim($this->puliRunner->run('config %key% --parsed', array(
'key' => $key,
)));
switch ($value) {
case 'null':
return null;
case 'true':
return true;
case 'false':
return false;
default:
return $value;
}
}
private function setConfigKey($key, $value)
{
$this->puliRunner->run('config %key% %value%', array(
'key' => $key,
'value' => $value,
));
}
/**
* @return PuliPackage[]
*/
private function loadPuliPackages()
{
$packages = array();
$output = $this->puliRunner->run('package --list --format %format%', array(
'format' => '%name%;%installer%;%install_path%;%state%;%env%',
));
// PuliRunner replaces \r\n by \n for those Windows boxes
foreach (explode("\n", $output) as $packageLine) {
if (!$packageLine) {
continue;
}
$packageParts = explode(';', $packageLine);
$packages[$packageParts[0]] = new PuliPackage(
$packageParts[0],
$packageParts[1],
$packageParts[2],
$packageParts[3],
$packageParts[4]
);
}
return $packages;
}
private function installPackage($installPath, $packageName, $env)
{
$env = PuliPackage::ENV_DEV === $env ? ' --dev' : '';
$this->puliRunner->run('package --install %path% %package_name% --installer %installer%'.$env, array(
'path' => $installPath,
'package_name' => $packageName,
'installer' => self::INSTALLER_NAME,
));
}
private function removePackage($packageName)
{
$this->puliRunner->run('package --delete %package_name%', array(
'package_name' => $packageName,
));
}
private function buildPuli(IOInterface $io)
{
$io->write('<info>Running "puli build"</info>');
$this->puliRunner->run('build');
}
private function renamePackage($name, $newName)
{
$this->puliRunner->run('package --rename %old_name% %new_name%', array(
'old_name' => $name,
'new_name' => $newName,
));
}
/**
* @param IOInterface $io
* @param $message
* @param Exception|null $exception
*/
private function printWarning(IOInterface $io, $message, Exception $exception = null)
{
if (!$exception) {
$reasonPhrase = '';
} elseif ($io->isVerbose()) {
$reasonPhrase = $exception instanceof PuliRunnerException
? $exception->getFullError()
: $exception->getMessage()."\n\n".$exception->getTraceAsString();
} else {
$reasonPhrase = $exception instanceof PuliRunnerException
? $exception->getShortError()
: $exception->getMessage();
}
$io->writeError(sprintf(
'<warning>Warning: %s%s</warning>',
$message,
$reasonPhrase ? ': '.$reasonPhrase : '.'
));
}
private function printPackageWarning(IOInterface $io, $message, $packageName, $installPath, PuliRunnerException $exception = null)
{
$this->printWarning($io, sprintf(
$message,
$packageName,
Path::makeRelative($installPath, $this->rootDir)
), $exception);
}
private function filterProdPackageNames(array $composerPackages, PackageInterface $package, array &$result = array())
{
// Resolve aliases
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
// Package was processed already
if (isset($result[$package->getName()])) {
return $result;
}
$result[$package->getName()] = true;
// Recursively filter package names
foreach ($package->getRequires() as $packageName => $link) {
if (isset($composerPackages[$packageName])) {
$this->filterProdPackageNames($composerPackages, $composerPackages[$packageName], $result);
}
}
return $result;
}
private function verifyPuliVersion()
{
$versionString = $this->puliRunner->run('-V');
if (!preg_match('~\d+\.\d+\.\d+(-\w+)?~', $versionString, $matches)) {
throw new RuntimeException(sprintf(
'Could not determine Puli version. "puli -V" returned: %s',
$versionString
));
}
if (version_compare($matches[0], self::MIN_CLI_VERSION, '<')) {
throw new RuntimeException(sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'upgrade to version %s or higher. You can also install the '.
'puli/cli dependency at version %s in your project.',
$matches[0],
self::MIN_CLI_VERSION,
self::MIN_CLI_VERSION
));
}
if (version_compare($matches[0], self::MAX_CLI_VERSION, '>')) {
throw new RuntimeException(sprintf(
'Found an unsupported version of the Puli CLI: %s. Please '.
'downgrade to a lower version than %s. You can also install '.
'the puli/cli dependency in your project.',
$matches[0],
self::MAX_CLI_VERSION
));
}
}
}