-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-composer-require.php
243 lines (199 loc) · 6.46 KB
/
update-composer-require.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
<?php
declare(strict_types=1);
$output = new Output();
$jsonFormatter = new JsonFormatter($output);
$opts = getopt('', ['env::', 'shopware:', 'admin', 'storefront', 'release']);
if (! $opts) {
$output->error('No options set. Option "env" is required.');
return;
}
// Get selected env
switch ($opts['env']) {
case 'dev':
case 'develop':
case 'development':
$env = 'development';
$require = 'require-dev';
break;
case 'prod':
case 'production':
$env = 'production';
$require = 'require';
break;
}
if (! isset($require)) {
$output->error('Env needs to be one of: dev, develop, development, prod, production');
return;
}
// Get minimum Shopware version
$shopwareVersion = '*';
if (isset($opts['shopware'])) {
$shopwareVersion = (string) $opts['shopware'];
}
// Should admin package be added to require
$addDependencies = array_key_exists('release', $opts);
try {
$composerContent = $jsonFormatter->read(__DIR__ . '/composer.json');
unset(
$composerContent['require']['shopware/core'],
$composerContent['require']['shopware/administration'],
$composerContent['require']['shopware/storefront'],
$composerContent['require-dev']['shopware/core'],
$composerContent['require-dev']['shopware/administration'],
$composerContent['require-dev']['shopware/storefront']
);
if (empty($composerContent['require'])) {
unset($composerContent['require']);
}
if (empty($composerContent['require-dev'])) {
unset($composerContent['require-dev']);
}
if ($addDependencies) {
$composerContent[$require]['shopware/core'] = $shopwareVersion;
$composerContent[$require]['shopware/administration'] = $shopwareVersion;
$composerContent[$require]['shopware/storefront'] = $shopwareVersion;
}
$jsonFormatter->write(
__DIR__ . '/composer.json',
$jsonFormatter->sort($composerContent, [
"name",
"description",
"version",
"type",
"license",
"authors",
"extra",
"autoload",
"autoload-dev",
"require",
"require-dev",
"scripts",
"config",
])
);
$output->success(sprintf('Switched composer.json to %s requiring Shopware version %s', $env, $shopwareVersion));
} catch (Exception $e) {
$output->error($e->getMessage());
}
class JsonFormatter
{
/** @var Output */
private $output;
public function __construct(Output $output)
{
$this->output = $output;
}
public function fixEncoding(string $json): string
{
$currentEncoding = mb_detect_encoding($json, ['UTF-8', 'ISO-8859-1'], true);
switch ($currentEncoding) {
case 'UTF-8': // Already UTF-8, do nothing
break;
case 'ISO-8859-1':
$this->output->warn('Detected ISO-8859-1 encoding. Attempting to switch to UTF-8');
$json = mb_convert_encoding($json, 'UTF-8', 'ISO-8859-1');
break;
default: // Unknown encoding, warn user they should convert manually.
throw new Exception('Unable to detect current json file encoding. Please convert to UTF-8 manually.');
}
return $json;
}
public function read(string $path)
{
$json = file_get_contents($path);
if (empty($json)) {
throw new Exception(sprintf('Something went wrong reading %s', $path));
}
$json = json_decode($this->fixEncoding($json), true);
if (empty($json)) {
throw new Exception(sprintf('Something went wrong decoding %s', $path));
}
return $json;
}
public function sort(array $json, array $keyOrder): array
{
$sortedArray = [];
foreach ($keyOrder as $key) {
if (isset($json[$key])) {
$sortedArray[$key] = $json[$key];
unset($json[$key]);
}
}
if (! empty($json)) {
$sortedArray += $json;
}
return $sortedArray;
}
public function write(string $path, array $json): bool
{
return (bool) file_put_contents(
$path,
json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
);
}
}
class Output
{
const COLORS = [
'black' => 0,
'red' => 1,
'green' => 2,
'yellow' => 3,
'blue' => 4,
'magenta' => 5,
'cyan' => 6,
'white' => 7,
'default' => 9,
];
public function createBlock(iterable $messages, int $indentLength = 2)
{
$lines = [];
$lineLength = 80;
$lineIndentation = str_repeat(' ', $indentLength);
foreach ($messages as $message) {
$messageLineLength = $lineLength - ($indentLength * 2);
$messageLines = explode(PHP_EOL, wordwrap($message, $messageLineLength, PHP_EOL, true));
foreach ($messageLines as $messageLine) {
$lines[] = $messageLine;
}
}
array_unshift($lines, '');
$lines[] = '';
foreach ($lines as &$line) {
$line = $lineIndentation . $line;
$line .= str_repeat(' ', max($lineLength - strlen($line), 0));
}
return $lines;
}
public function error($text)
{
$this->writeLn($this->createBlock([$text]), self::COLORS['white'], self::COLORS['red']);
$this->writeLn();
}
public function info($text)
{
$this->writeLn($this->createBlock([$text]), self::COLORS['black'], self::COLORS['cyan']);
$this->writeLn();
}
public function success($text)
{
$this->writeLn($this->createBlock([$text]), self::COLORS['black'], self::COLORS['green']);
$this->writeLn();
}
public function warn($text)
{
$this->writeLn($this->createBlock([$text]), self::COLORS['black'], self::COLORS['yellow']);
$this->writeLn();
}
public function writeLn($messages = "", $fg = self::COLORS['default'], $bg = self::COLORS['default'])
{
if (! is_iterable($messages)) {
$messages = [$messages];
}
$fg = '3' . $fg;
$bg = '4' . $bg;
foreach ($messages as $message) {
echo sprintf("\033[%s;%sm%s\033[0m%s", $fg, $bg, $message, PHP_EOL);
}
}
}