-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.php
133 lines (108 loc) · 4.12 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
<?php
namespace Pixelfear\ComposerDistPlugin;
use Composer\Composer;
use Composer\Downloader\TransportException;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
class Plugin implements PluginInterface, EventSubscriberInterface
{
protected $composer;
protected $io;
protected $files;
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}
public static function getSubscribedEvents()
{
return [
PackageEvents::POST_PACKAGE_INSTALL => 'downloadInstall',
PackageEvents::POST_PACKAGE_UPDATE => 'downloadUpdate',
];
}
public function downloadInstall(PackageEvent $event)
{
$this->download($event->getOperation()->getPackage());
}
public function downloadUpdate(PackageEvent $event)
{
$this->download($event->getOperation()->getTargetPackage());
}
protected function download($package)
{
$extra = $package->getExtra();
if (! isset($extra['download-dist'])) {
return;
}
$config = (new ConfigNormalizer)->normalize($extra['download-dist']);
foreach ($config['bundles'] as $bundle) {
$this->downloadBundle($package, $bundle);
}
}
protected function downloadBundle($package, $config)
{
$installer = $this->composer->getInstallationManager();
$path = $installer->getInstallPath($package) . '/' . ($config['path'] ?? 'dist');
// The downloader will delete the directory in order to unzip the file.
// https://github.com/composer/composer/blob/88cff792cc6f2fa4df736e9abb612b955b783087/src/Composer/Downloader/FileDownloader.php#L123
// If the download fails, the existing directory will be gone. We'll back it up and restore
// when there's an error. If devs expect a failure and manually created or linked the
// directory (eg. when working on a master branch), at least they won't lose it.
$backup = Backup::of($path)->create();
(new Filesystem)->remove($path);
$url = strtr($config['url'], [
'{$version}' => $version = $package->getPrettyVersion()
]);
$subpackage = new Subpackage($package, $config['name'], $url);
try {
$this->attemptDownload($subpackage, $path);
} catch (TransportException $e) {
$backup->restore();
// Allow failures when referencing a branch.
// Users will likely be developing and be okay with manually compiling files.
// We assume that tagged releases will exist and not 404.
if (preg_match('/^dev-/', $version) || preg_match('/\.x-dev$/', $version)) {
$this->writeDownloadFailedError($subpackage, $url);
} else {
throw $e;
}
} finally {
$backup->delete();
}
}
public function deactivate(Composer $composer, IOInterface $io): void
{
//
}
public function uninstall(Composer $composer, IOInterface $io): void
{
//
}
private function attemptDownload(Subpackage $package, string $path)
{
$downloadManager = $this->composer->getDownloadManager();
if ($this->usingComposerTwo()) {
$loop = $this->composer->getLoop();
$loop->wait([$downloadManager->download($package, $path)]);
$loop->wait([$downloadManager->install($package, $path)]);
} else {
$downloadManager->download($package, $path);
}
}
private function writeDownloadFailedError(Subpackage $package, string $url)
{
if ($this->usingComposerTwo()) {
$this->io->writeError(' <error>Failed to download</error>');
} else {
$this->io->write(''); // Composer v1 already shows "failed" in red.
}
}
private function usingComposerTwo(): bool
{
return strpos(PluginInterface::PLUGIN_API_VERSION, '2') === 0;
}
}