|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
|
4
|
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\FileSystemFixes; |
|
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes; |
|
7
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
|
8
|
|
|
|
|
9
|
|
|
//use either of the following to create the info.json file required |
|
10
|
|
|
//your project will also require a composer.json.default file |
|
11
|
|
|
//this file is used to reset the project to the default state before attempting to install each library |
|
12
|
|
|
//composer info --format=json > info.json |
|
13
|
|
|
//composer info --direct --format=json > info.json |
|
14
|
|
|
|
|
15
|
|
|
class ComposerCompatibilityCheckerStep2 extends Task |
|
16
|
|
|
{ |
|
17
|
|
|
protected $taskStep = 's10'; |
|
18
|
|
|
protected $alwaysKeepArray = [ |
|
19
|
|
|
'silverstripe/recipe-cms', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
public function getTitle() |
|
23
|
|
|
{ |
|
24
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
|
|
|
|
|
|
25
|
|
|
return 'For module upgrades, this is not being used right now.'; |
|
26
|
|
|
} |
|
27
|
|
|
return 'Check what composer requirements can be added.'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getDescription() |
|
31
|
|
|
{ |
|
32
|
|
|
return 'Goes through all the composer requirements and adds them one by one....'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $params |
|
38
|
|
|
* @return string|null |
|
39
|
|
|
*/ |
|
40
|
|
|
public function runActualTask($params = []) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function runInner() |
|
50
|
|
|
{ |
|
51
|
|
|
$composerData = ComposerJsonFixes::inst($this->mu())->getJSON( |
|
52
|
|
|
$this->mu()->getGitRootDir() |
|
|
|
|
|
|
53
|
|
|
); |
|
54
|
|
|
foreach (['require', 'require-dev'] as $section) { |
|
55
|
|
|
if(! empty($composerData[$section])) { |
|
56
|
|
|
$composerData[$section.'-tmp'] = $composerData[$section]; |
|
57
|
|
|
unset($composerData[$section]); |
|
58
|
|
|
} |
|
59
|
|
|
foreach($this->alwaysKeepArray as $package) { |
|
60
|
|
|
if(isset($composerData[$section.'-tmp'][$package])) { |
|
61
|
|
|
$composerData[$section][$package] = $composerData[$section.'-tmp'][$package]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
ComposerJsonFixes::inst($this->mu())->setJSON( |
|
66
|
|
|
$this->mu()->getGitRootDir(), |
|
67
|
|
|
$composerData |
|
68
|
|
|
); |
|
69
|
|
|
foreach (['require-tmp', 'require-dev-tmp'] as $section) { |
|
70
|
|
|
if (isset($composerData[$section]) && is_array($composerData[$section]) && count($composerData[$section])) { |
|
71
|
|
|
foreach ($composerData[$section] as $package => $version) { |
|
72
|
|
|
if (in_array($package, $this->alwaysKeepArray)) { |
|
73
|
|
|
// Skip silverstripe/recipe-cms since it's already included |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->mu()->colourPrint('trying to add '.$package.':'.$version, 'yellow', 1); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
// Attempt to require the package |
|
80
|
|
|
$output = ''; |
|
81
|
|
|
exec("composer require $package:$version --no-update", $output, $returnVar); |
|
|
|
|
|
|
82
|
|
|
$this->mu()->colourPrint('result'.print_r($output), 'yellow', 1); |
|
|
|
|
|
|
83
|
|
|
if ($returnVar !== 0) { |
|
84
|
|
|
$this->mu()->colourPrint("$package:$version could not be added. Skipping...", 'red', 1); |
|
85
|
|
|
$this->updateComposerJson($section, $package, $version, 'remove'); |
|
86
|
|
|
// Skip adding to suggest or removing since it's being processed dynamically |
|
87
|
|
|
} else { |
|
88
|
|
|
// If require was successful, update composer |
|
89
|
|
|
$output = ''; |
|
90
|
|
|
exec('composer update', $output, $updateReturnVar); |
|
91
|
|
|
if ($updateReturnVar !== 0) { |
|
92
|
|
|
// If update fails, revert the require |
|
93
|
|
|
$this->mu()->colourPrint("Update failed after requiring $package. Reverting...", 'red', 1); |
|
94
|
|
|
$this->updateComposerJson($section, $package, $version, 'remove'); |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->updateComposerJson($section, $package, $version, 'add'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($this->mu()->getIsProjectUpgrade()) { |
|
|
|
|
|
|
104
|
|
|
$this->mu()->execMe( |
|
|
|
|
|
|
105
|
|
|
$this->mu()->getGitRootDir(), |
|
106
|
|
|
'composer update -vvv --no-interaction', |
|
107
|
|
|
'run composer update', |
|
108
|
|
|
false |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Update composer.json to add, remove, or suggest a package. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function updateComposerJson(string $section, string $package, string $version, string $action, string $message = ''): void |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$composerData = ComposerJsonFixes::inst($this->mu())->getJSON( |
|
120
|
|
|
$this->mu()->getGitRootDir() |
|
121
|
|
|
); |
|
122
|
|
|
switch ($action) { |
|
123
|
|
|
case 'remove': |
|
124
|
|
|
unset($composerJson[$section][$package]); |
|
|
|
|
|
|
125
|
|
|
$composerJson['suggest'][$package] = 'Could not load with version '.$version; |
|
|
|
|
|
|
126
|
|
|
$this->mu()->colourPrint('removing to add '.$package.':'.$version, 'red', 1); |
|
127
|
|
|
break; |
|
128
|
|
|
case 'add': |
|
129
|
|
|
$this->mu()->colourPrint('adding '.$package.':'.$version, 'green', 1); |
|
130
|
|
|
$composerJson[$section][$package] = $version; |
|
131
|
|
|
break; |
|
132
|
|
|
} |
|
133
|
|
|
ComposerJsonFixes::inst($this->mu())->setJSON( |
|
134
|
|
|
$this->mu()->getGitRootDir(), |
|
135
|
|
|
$composerData |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
protected function hasCommitAndPush() |
|
142
|
|
|
{ |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|