-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBinaryInstaller.php
274 lines (249 loc) · 9.04 KB
/
BinaryInstaller.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
<?php
declare(strict_types=1);
namespace Lullabot\Drainpipe;
use Composer\Cache;
use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\Util\Filesystem;
class BinaryInstaller implements PluginInterface, EventSubscriberInterface
{
/**
* The binaries to manage and download.
*
* @var array[]
*/
protected $binaries = [];
/**
* @var IOInterface
*/
protected $io;
/**
* Composer instance configuration.
*
* @var Config
*/
protected $config;
/**
* Composer cache.
*
* @var Cache
*/
protected $cache;
/**
* The platform the installer is being run on.
*
* @var string linux, windows, or darwin
*/
protected $platform;
/**
* System architecture.
*
* @var string 386, amd64, or arm64
*/
protected $processor;
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->io = $io;
$this->config = $composer->getConfig();
$this->platform = strtolower(\PHP_OS_FAMILY);
$uname = strtolower(php_uname('m'));
if ($uname === 'arm64' || $uname === 'aarch64') {
$this->processor = 'arm64';
} elseif ($uname === 'x86_64' || $uname === 'amd64') {
$this->processor = 'amd64';
} else {
$this->processor = '386';
}
if (!empty(getenv('DRAINPIPE_PLATFORM'))) {
$this->platform = getenv('DRAINPIPE_PLATFORM');
}
if (!empty(getenv('DRAINPIPE_PROCESSOR'))) {
$this->processor = 'DRAINPIPE_PROCESSOR';
}
$this->cache = new Cache(
$this->io,
implode(\DIRECTORY_SEPARATOR, [
$this->config->get('cache-dir'),
'files',
'lullabot',
'drainpipe',
'bin',
])
);
}
/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io)
{
}
/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io)
{
$fs = new Filesystem();
foreach ($this->binaries as $binary => $info) {
$fs->remove($this->config->get('bin-dir').\DIRECTORY_SEPARATOR.$binary);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
ScriptEvents::POST_INSTALL_CMD => 'onPostInstallCmd',
ScriptEvents::POST_UPDATE_CMD => 'onPostUpdateCmd',
];
}
/**
* Handle post install command events.
*
* @param Event $event the event to handle
*/
public function onPostInstallCmd(Event $event): void
{
$this->installBinaries($event);
}
/**
* Handle post update command events.
*
* @param event $event The event to handle
*/
public function onPostUpdateCmd(Event $event): void
{
$this->installBinaries($event);
}
/**
* Install all binaries into vendor/bin.
*
* @param event $event The event to handle
*/
public function installBinaries(Event $event): void
{
foreach ($this->binaries as $binary => $info) {
$platform = $this->platform;
$processor = $this->processor;
// Allow platform and processor to be overridden for this binary by
// the user.
if (!empty(getenv('DRAINPIPE_PLATFORM_'.$binary))) {
$platform = getenv('DRAINPIPE_PLATFORM_'.$binary);
}
if (!empty(getenv('DRAINPIPE_PROCESSOR_'.$binary))) {
$processor = getenv('DRAINPIPE_PROCESSOR_'.$binary);
}
if (empty($info['releases'][$platform][$processor])) {
// Generate a list of available releases.
$releases = array_reduce(array_keys($info['releases']), function ($carry, $platform) use ($info) {
$platform_processors = array_map(function ($processor) use ($platform) {
return "$platform/$processor";
}, array_keys($info['releases'][$platform]));
$carry = array_merge($carry, $platform_processors);
return $carry;
}, []);
throw new \Exception("No release available for $binary on $platform/$processor. You can override this with the environment variable \"DRAINPIPE_PLATFORM_$binary\" and \"DRAINPIPE_PROCESSOR_$binary\". Releases are available for:\n".implode("\n", $releases));
}
$this->installBinary($binary, $info['version'], $info['releases'][$platform][$processor]['url'], $info['releases'][$platform][$processor]['sha'], $info['hashalgo']);
}
}
/**
* Install an individual binary.
*
* @param string $binary
* The final filename of the binary
* @param string $version
* The version number of the binary
* @param string $url
* The URL to download the binary
* @param string $sha
* The hash to validate
* @param string $hashalgo
* The hashing algorithm to use
*
* @see https://www.php.net/manual/en/function.hash-file.php
*/
protected function installBinary($binary, $version, $url, $sha, $hashalgo = 'sha256'): void
{
$bin = $this->config->get('bin-dir');
$fs = new Filesystem();
$fs->ensureDirectoryExists($bin);
$httpDownloader = Factory::createHttpDownloader($this->io, $this->config);
$parts = explode('/', $url);
$fileName = array_pop($parts);
if ($this->cache->isEnabled()) {
$cacheFolder = $this->cache->getRoot() . $binary . \DIRECTORY_SEPARATOR . $version;
}
else {
$cacheFolder = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . $binary . \DIRECTORY_SEPARATOR . $version;
}
$cacheDestination = $cacheFolder . \DIRECTORY_SEPARATOR . $fileName;
$cacheExtractedBinary = $cacheFolder . \DIRECTORY_SEPARATOR . $binary;
$binDestination = $bin . \DIRECTORY_SEPARATOR . $binary;
// Check the cache.
$fs->ensureDirectoryExists($cacheFolder);
if ($this->needsDownload($cacheDestination, $hashalgo, $sha)) {
// Fetch a new copy of the binary.
$httpDownloader->copy($url, $cacheDestination);
} else {
$this->io->write(sprintf('Using cached version of %s v%s (%s)', $binary, $version, $sha));
}
// Compare checksums.
if (hash_file($hashalgo, $cacheDestination) !== $sha) {
throw new \Exception('SHA does not match for '.$binary);
}
if ('.tar.gz' === substr($fileName, -7)) {
// Remove .tar
$fs->remove(substr($cacheDestination, 0, -3));
$archive = new \PharData($cacheDestination);
$archive->decompress();
$archive = new \PharData(substr($cacheDestination, 0, -3));
$archive->extractTo($cacheFolder, $binary, true);
// Remove .tar
$fs->remove(substr($cacheDestination, 0, -3));
} elseif ('.zip' === substr($fileName, -4)) {
$archive = new \ZipArchive();
$archive->open(substr($cacheDestination, 0, -4));
$archive->extractTo($cacheFolder, $binary);
} else {
$fs->rename($cacheDestination, $cacheExtractedBinary);
}
// Check the vendor/bin directory first, otherwise we could hit a
// condition where task has called "composer install --no-dev" after
// "composer install" and tries to replace the task binary - this will
// fail because the binary is already being run and you'll get a "failed
// to open stream: Text file busy" error.
if (file_exists($binDestination) && hash_file('sha256', $binDestination) === hash_file('sha256', $cacheExtractedBinary)) {
$this->io->write(sprintf('%s v%s (%s) already exists in bin-dir, not overwriting.', $binary, $version, $sha));
}
else {
$fs->remove($binDestination);
$fs->copy($cacheExtractedBinary, $binDestination);
// Make executable.
if ('windows' !== $this->platform) {
chmod($binDestination, 0755);
}
}
}
/**
* Return if a file needs to be downloaded or not.
*
* @param string $cacheDestination The destination path to the downloaded file.
* @param string $hashalgo The hash algorithm used to validate the file.
* @param string $hash The hash used to validate the file.
*
* @return bool True if the file needs to be downloaded again, false otherwise.
*/
private function needsDownload(string $cacheDestination, string $hashalgo, string $hash): bool {
return !$this->cache->isEnabled() || !file_exists($cacheDestination) || hash_file($hashalgo, $cacheDestination) !== $hash;
}
}