1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks; |
4
|
|
|
|
5
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Api\FileSystemFixes; |
6
|
|
|
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task; |
7
|
|
|
|
8
|
|
|
//use either of the following to create the info.json file required |
9
|
|
|
//your project will also require a composer.json.default file |
10
|
|
|
//this file is used to reset the project to the default state before attempting to install each library |
11
|
|
|
//composer info --format=json > info.json |
12
|
|
|
//composer info --direct --format=json > info.json |
13
|
|
|
|
14
|
|
|
class ComposerCompatibilityCheckerStep3 extends Task |
15
|
|
|
{ |
16
|
|
|
protected $taskStep = 's10'; |
17
|
|
|
|
18
|
|
|
protected $infoFileFileName = 'composer-requirements-info.json'; |
19
|
|
|
|
20
|
|
|
protected $resultsFileAsJSON = 'composer-requirements-info.upgraded.json'; |
21
|
|
|
|
22
|
|
|
protected $lessUpgradeIsBetter = false; |
23
|
|
|
|
24
|
|
|
private $outputArray = []; |
25
|
|
|
|
26
|
|
|
private $firstTimeReset = true; |
27
|
|
|
|
28
|
|
|
private $webRootLocation = ''; |
29
|
|
|
|
30
|
|
|
public function getTitle() |
31
|
|
|
{ |
32
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
|
|
|
|
33
|
|
|
return 'For moduls upgrades, this is not being used right now.'; |
34
|
|
|
} |
35
|
|
|
return 'Check what composer requirements packages are best to use, using the ' |
36
|
|
|
. $this->getJsonFileLocation() . ' file and placing results in ' |
37
|
|
|
. $this->getJsonFileLocationJSONResults(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getDescription() |
41
|
|
|
{ |
42
|
|
|
return ' |
43
|
|
|
THIS IS STILL UNDER CONSTRUCTION! |
44
|
|
|
'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $params |
49
|
|
|
*/ |
50
|
|
|
public function runActualTask($params = []): ?string |
51
|
|
|
{ |
52
|
|
|
if ($this->mu()->getIsModuleUpgrade()) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
$this->webRootLocation = $this->mu()->getWebRootDirLocation(); |
|
|
|
|
56
|
|
|
|
57
|
|
|
file_put_contents($this->getJsonFileLocationJSONResults(), ''); |
58
|
|
|
|
59
|
|
|
$jsonFile = file_get_contents($this->getJsonFileLocation()); |
60
|
|
|
$jsonData = json_decode($jsonFile, true); |
61
|
|
|
|
62
|
|
|
$libraries = $jsonData['installed'] ?? []; |
63
|
|
|
|
64
|
|
|
$this->resetProject(); |
65
|
|
|
foreach ($libraries as $library) { |
66
|
|
|
$commit = ''; |
67
|
|
|
$name = $library['name']; |
68
|
|
|
$version = $library['version']; |
69
|
|
|
if (strpos($version, 'dev-master') !== false) { |
70
|
|
|
$commit = $version; |
71
|
|
|
$version = str_replace(' ', '#', $version); |
72
|
|
|
} |
73
|
|
|
unset($libraryOutput); |
74
|
|
|
$libraryOutput = $this->mu()->execMe( |
75
|
|
|
$this->webRootLocation, |
76
|
|
|
'composer require ' . $name . " '" . $version . "' 2>&1 --no-interaction", |
77
|
|
|
'adding module' |
78
|
|
|
); |
79
|
|
|
$message = 'composer require ' . $name . ':' . $version . ' ... '; |
80
|
|
|
if (in_array(' [InvalidArgumentException]', $libraryOutput, true)) { |
81
|
|
|
$message .= "unsuccessful, could not find a matching version of package.\n"; |
82
|
|
|
$this->mu()->colourPrint($message); |
83
|
|
|
} elseif (in_array('Installation failed, reverting ./composer.json to its original content.', $libraryOutput, true)) { |
84
|
|
|
$message .= "unsuccessful, searching for next best version.\n"; |
85
|
|
|
$this->mu()->colourPrint($message); |
86
|
|
|
unset($show); |
87
|
|
|
$show = $this->mu()->execMe( |
88
|
|
|
$this->webRootLocation, |
89
|
|
|
'composer show -a ' . $name . ' --no-interaction 2>&1 ', |
90
|
|
|
'show details of module' |
91
|
|
|
); |
92
|
|
|
$versionsString = $show[3]; |
93
|
|
|
$versionsString = str_replace('versions : ', '', $versionsString); |
94
|
|
|
$currentVersionPos = strpos($versionsString, ', ' . $version); |
95
|
|
|
$versionsString = substr($versionsString, 0, $currentVersionPos); |
96
|
|
|
$newerVersions = explode(', ', $versionsString); |
97
|
|
|
if ($this->lessUpgradeIsBetter) { |
98
|
|
|
$newerVersions = array_reverse($newerVersions); |
99
|
|
|
} |
100
|
|
|
$output = 0; |
101
|
|
|
$versionFound = false; |
102
|
|
|
foreach ($newerVersions as $newVersion) { |
103
|
|
|
unset($output); |
104
|
|
|
$output = $this->mu()->execMe( |
105
|
|
|
$this->webRootLocation, |
106
|
|
|
'composer require ' . $name . " '" . $newVersion . "' 2>&1 ", |
107
|
|
|
'show details of module', |
108
|
|
|
false |
109
|
|
|
); |
110
|
|
|
$message = 'composer require ' . $name . ':' . $newVersion . ' ...... '; |
111
|
|
|
if (! in_array('Installation failed', $output, true)) { |
112
|
|
|
$versionFound = true; |
113
|
|
|
$message .= "successful!, it is the next best version.\n"; |
114
|
|
|
$this->mu()->colourPrint($message); |
115
|
|
|
$this->addToOutputArray($name, $newVersion); |
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
$message .= "unsuccessful, searching for next best version.\n"; |
119
|
|
|
$this->mu()->colourPrint($message, false); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (! $versionFound) { |
123
|
|
|
$message = 'Could not find any compatible versions for: ' . $name . "!'\n "; |
124
|
|
|
$this->mu()->colourPrint($message); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
$message .= "successful!\n "; |
128
|
|
|
$this->mu()->colourPrint($message); |
129
|
|
|
$version = $commit ?: $version; |
130
|
|
|
$this->addToOutputArray($name, $version); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
file_put_contents( |
135
|
|
|
$this->getJsonFileLocationJSONResults(), |
136
|
|
|
json_encode($this->outputArray), |
137
|
|
|
FILE_APPEND | LOCK_EX |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function resetProject() |
144
|
|
|
{ |
145
|
|
|
$this->mu()->colourPrint('resetting project to composer.json.default', false); |
|
|
|
|
146
|
|
|
if ($this->firstTimeReset === true) { |
147
|
|
|
$this->mu()->execMe( |
|
|
|
|
148
|
|
|
$this->webRootLocation, |
149
|
|
|
'cp composer.json composer.json.temp.default', |
150
|
|
|
'make temporary copy of composer.json' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
$fixer = FileSystemFixes::inst($this->mu()) |
|
|
|
|
154
|
|
|
->removeDirOrFile($this->webRootLocation . '/composer.json'); |
155
|
|
|
$this->mu()->execMe( |
156
|
|
|
$this->webRootLocation, |
157
|
|
|
'cp composer.json.temp.default composer.json', |
158
|
|
|
'back to default composer file' |
159
|
|
|
); |
160
|
|
|
if ($this->firstTimeReset === false) { |
161
|
|
|
$this->mu()->execMe( |
162
|
|
|
$this->webRootLocation, |
163
|
|
|
'composer update', |
164
|
|
|
'run composer update' |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
$this->firstTimeReset = false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function addToOutputArray($name, $version) |
171
|
|
|
{ |
172
|
|
|
$pos = strpos($name, '/') + 1; |
173
|
|
|
$array['folder'] = substr($name, $pos); |
|
|
|
|
174
|
|
|
$array['tag'] = $version; |
175
|
|
|
$array['repo'] = null; |
176
|
|
|
|
177
|
|
|
$strings = $this->mu()->execMe( |
178
|
|
|
$this->webRootLocation, |
179
|
|
|
'composer show -a ' . $name . ' 2>&1 ', |
180
|
|
|
'run composer update' |
181
|
|
|
); |
182
|
|
|
$source = ''; |
|
|
|
|
183
|
|
|
|
184
|
|
|
foreach ($strings as $string) { |
185
|
|
|
if (strpos($string, 'source') !== false) { |
186
|
|
|
$source = $string; |
187
|
|
|
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $source, $match); |
188
|
|
|
if (! isset($match[0][0])) { |
189
|
|
|
preg_match_all( |
190
|
|
|
'#((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?#', |
191
|
|
|
$source, |
192
|
|
|
$match |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
if (isset($match[0][0])) { |
196
|
|
|
$array['repo'] = $match[0][0]; |
197
|
|
|
} |
198
|
|
|
break; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
array_push($this->outputArray, $array); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
protected function getJsonFileLocation(): string |
206
|
|
|
{ |
207
|
|
|
return $this->mu()->getWebRootDirLocation() . '/' . $this->infoFileFileName; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function getJsonFileLocationJSONResults(): string |
211
|
|
|
{ |
212
|
|
|
return $this->mu()->getWebRootDirLocation() . '/' . $this->resultsFileAsJSON; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
protected function hasCommitAndPush() |
216
|
|
|
{ |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|