/
CheckComposerUpdatesTask.php
506 lines (433 loc) · 13.6 KB
/
CheckComposerUpdatesTask.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
<?php
use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
use Packagist\Api\Client;
/**
* Task which does the actual checking of updates
*
* Originally from https://github.com/XploreNet/silverstripe-composerupdates
*
* @author Matt Dwen
* @license MIT
*/
class CheckComposerUpdatesTask extends BuildTask
{
/**
* @var string
*/
protected $title = 'Composer update checker';
/**
* @var string
*/
protected $description = 'Checks if any composer dependencies can be updated.';
private static $dependencies = [
'PackagistClient' => '%$Packagist\\Api\\Client',
'ComposerLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\ComposerLoader',
];
/**
* Minimum required stability defined in the site composer.json
*
* @var string
*/
protected $minimumStability;
/**
* Whether or not to prefer stable packages
*
* @var bool
*/
protected $preferStable;
/**
* Known stability values
*
* @var array
*/
protected $stabilityOptions = array(
'dev',
'alpha',
'beta',
'rc',
'stable'
);
/**
* @var Client
*/
protected $packagistClient;
/**
* @var ComposerLoader
*/
protected $composerLoader;
/**
* Retrieve an array of primary composer dependencies from composer.json
*
* @return array
*/
protected function getPackages()
{
$composerJson = $this->getComposerLoader()->getJson();
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Set the stability parameters
$this->setMinimumStability(
isset($composerJson->{'minimum-stability'}) ? $composerJson->{'minimum-stability'} : 'stable'
);
$this->setPreferStable(
isset($composerJson->{'prefer-stable'}) ? $composerJson->{'prefer-stable'} : true
);
$packages = [];
foreach ($composerJson->require as $package => $version) {
// Ensure there's a / in the name, probably not an addon with it
if (!strpos($package, '/')) {
continue;
}
$packages[] = $package;
}
return $packages;
}
/**
* Return an array of all Composer dependencies from composer.lock
*
* Example: `['silverstripe/cms' => '3.4.1', ...]`
*
* @return array
*/
protected function getDependencies()
{
$packages = [];
foreach ($this->getComposerLoader()->getLock()->packages as $package) {
$packages[$package->name] = $package->version;
}
return $packages;
}
/**
* Check if an available version is better than the current version,
* considering stability requirements
*
* Returns FALSE if no update is available.
* Returns the best available version if an update is available.
*
* @param string $currentVersion
* @param string[] $availableVersions
* @return bool|string
*/
protected function hasUpdate($currentVersion, $availableVersions)
{
$currentVersion = strtolower($currentVersion);
// Check there are some versions
if (count($availableVersions) < 1) {
return false;
}
// If this is dev-master, compare the hashes
if ($currentVersion === 'dev-master') {
return $this->hasUpdateOnDevMaster($availableVersions);
}
// Loop through each available version
$currentStability = $this->getStability($currentVersion);
$bestVersion = $currentVersion;
$bestStability = $currentStability;
$availableVersions = array_reverse($availableVersions, true);
foreach ($availableVersions as $version => $details) {
// Get the stability of the version
$versionStability = $this->getStability($version);
// Does this meet minimum stability
if (!$this->isStableEnough($this->getMinimumStability(), $versionStability)) {
continue;
}
if ($this->getPreferStable()) {
// A simple php version compare rules out the dumb stuff
if (version_compare($bestVersion, $version) !== -1) {
continue;
}
} else {
// We're doing a straight version compare
$pureBestVersion = $this->getPureVersion($bestVersion);
$pureVersion = $this->getPureVersion($version);
// Checkout the version
$continue = false;
switch (version_compare($pureBestVersion, $pureVersion)) {
case -1:
// The version is better, take it
break;
case 0:
// The version is the same.
// Do another straight version compare to rule out rc1 vs rc2 etc...
if ($bestStability == $versionStability) {
if (version_compare($bestVersion, $version) !== -1) {
$continue = true;
break;
}
}
break;
case 1:
// The version is worse, ignore it
$continue = true;
break;
}
if ($continue) {
continue;
}
}
$bestVersion = $version;
$bestStability = $versionStability;
}
if ($bestVersion !== $currentVersion || $bestStability !== $currentStability) {
if ($bestStability === 'stable') {
return $bestVersion;
}
return $bestVersion . '-' . $bestStability;
}
return false;
}
/**
* Check the latest hash on the dev-master branch, and return it if different to the local hash
*
* FALSE is returned if the hash is the same.
*
* @param $availableVersions
* @return bool|string
*/
protected function hasUpdateOnDevMaster($availableVersions)
{
// Get the dev-master version
$devMaster = $availableVersions['dev-master'];
// Sneak the name of the package
$packageName = $devMaster->getName();
// Get the local package details
$localPackage = $this->getLocalPackage($packageName);
// What's the current hash?
$localHash = $localPackage->source->reference;
// What's the latest hash in the available versions
$remoteHash = $devMaster->getSource()->getReference();
// return either the new hash or false
return ($localHash != $remoteHash) ? $remoteHash : false;
}
/**
* Return details from composer.lock for a specific package
*
* @param string $packageName
* @return object
* @throws Exception if package cannot be found in composer.lock
*/
protected function getLocalPackage($packageName)
{
foreach ($this->getComposerLoader()->getLock()->packages as $package) {
if ($package->name == $packageName) {
return $package;
}
}
throw new Exception('Cannot locate local package ' . $packageName);
}
/**
* Retrieve the pure numerical version
*
* @param string $version
* @return string|null
*/
protected function getPureVersion($version)
{
$matches = [];
preg_match("/^(\d+\\.)?(\d+\\.)?(\\*|\d+)/", $version, $matches);
if (count($matches) > 0) {
return $matches[0];
}
return null;
}
/**
* Determine the stability of a given version
*
* @param string $version
* @return string
*/
protected function getStability($version)
{
$version = strtolower($version);
foreach ($this->getStabilityOptions() as $option) {
if (strpos($version, $option) !== false) {
return $option;
}
}
return 'stable';
}
/**
* Return a numerical representation of a stability
*
* Higher is more stable
*
* @param string $stability
* @return int
* @throws Exception If the stability is unknown
*/
protected function getStabilityIndex($stability)
{
$stability = strtolower($stability);
$index = array_search($stability, $this->getStabilityOptions(), true);
if ($index === false) {
throw new Exception("Unknown stability: $stability");
}
return $index;
}
/**
* Check if a stability meets a given minimum requirement
*
* @param $currentStability
* @param $possibleStability
* @return bool
*/
protected function isStableEnough($currentStability, $possibleStability)
{
$minimumIndex = $this->getStabilityIndex($currentStability);
$possibleIndex = $this->getStabilityIndex($possibleStability);
return ($possibleIndex >= $minimumIndex);
}
/**
* Record package details in the database
*
* @param string $package Name of the Composer Package
* @param string $installed Currently installed version
* @param string|boolean $latest The latest available version
*/
protected function recordUpdate($package, $installed, $latest)
{
// Is there a record already for the package? If so find it.
$packages = Package::get()->filter(['Name' => $package]);
// Get the hash installed
$localPackage = $this->getLocalPackage($package);
$installedHash = $localPackage->source->reference;
// if there is already one use it otherwise create a new data object
if ($packages->count() > 0) {
$update = $packages->first();
} else {
$update = Package::create();
$update->Name = $package;
$update->Version = $installed;
$update->VersionHash = $installedHash;
}
// Set the new details and save it
$update->LatestVersion = $latest;
$update->write();
}
/**
* runs the actual steps to verify if there are updates available
*
* @param SS_HTTPRequest $request
*/
public function run($request)
{
// Retrieve the packages
$packages = $this->getPackages();
$dependencies = $this->getDependencies();
// run through the packages and check each for updates
foreach ($packages as $package) {
// verify that we need to check this package.
if (!isset($dependencies[$package])) {
continue;
} else {
// get information about this package from packagist.
try {
$latest = $this->getPackagistClient()->get($package);
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
SS_Log::log($e->getMessage(), SS_Log::WARN);
continue;
}
// Check if there is a newer version
$currentVersion = $dependencies[$package];
$result = $this->hasUpdate($currentVersion, $latest->getVersions());
// Check if there is a newer version and if so record the update
if ($result !== false) {
$this->recordUpdate($package, $currentVersion, $result);
}
}
}
// finished message
$this->message('The task finished running. You can find the updated information in the database now.');
}
/**
* prints a message during the run of the task
*
* @param string $text
*/
protected function message($text)
{
if (!Director::is_cli()) {
$text = '<p>' . $text . '</p>';
}
echo $text . PHP_EOL;
}
/**
* @param string $minimumStability
* @return $this
*/
public function setMinimumStability($minimumStability)
{
$this->minimumStability = $minimumStability;
return $this;
}
/**
* @return string
*/
public function getMinimumStability()
{
return $this->minimumStability;
}
/**
* @param bool $preferStable
* @return $this
*/
public function setPreferStable($preferStable)
{
$this->preferStable = $preferStable;
return $this;
}
/**
* @return bool
*/
public function getPreferStable()
{
return $this->preferStable;
}
/**
* @param array $stabilityOptions
* @return $this
*/
public function setStabilityOptions($stabilityOptions)
{
$this->stabilityOptions = $stabilityOptions;
return $this;
}
/**
* @return array
*/
public function getStabilityOptions()
{
return $this->stabilityOptions;
}
/**
* @return Client
*/
public function getPackagistClient()
{
return $this->packagistClient;
}
/**
* @param Client $packagistClient
*/
public function setPackagistClient(Client $packagistClient)
{
$this->packagistClient = $packagistClient;
return $this;
}
/**
* @param ComposerLoader $composerLoader
* @return $this
*/
public function setComposerLoader(ComposerLoader $composerLoader)
{
$this->composerLoader = $composerLoader;
return $this;
}
/**
* @return ComposerLoader
*/
public function getComposerLoader()
{
return $this->composerLoader;
}
}