Repository navigation
Expand file tree
/
Copy pathWorkerCommandLineFactory.php
More file actions
280 lines (232 loc) · 9.78 KB
/
Copy pathWorkerCommandLineFactory.php
File metadata and controls
280 lines (232 loc) · 9.78 KB
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
<?php
declare(strict_types=1);
namespace Rector\Parallel\Command;
use PHPStan\Turbo\TurboExtensionSelector;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Configuration\Option;
use Rector\FileSystem\FilePathHelper;
use Rector\Parallel\Exception\ParallelShouldNotHappenException;
use Rector\Parallel\Reflection\CommandFromReflectionFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
/**
* @see \Rector\Tests\Parallel\Command\WorkerCommandLineFactoryTest
* @todo possibly extract to symplify/easy-parallel
*/
final readonly class WorkerCommandLineFactory
{
private const string OPTION_DASHES = '--';
public function __construct(
private CommandFromReflectionFactory $commandFromReflectionFactory,
private FilePathHelper $filePathHelper
) {
}
/**
* @param class-string<Command> $mainCommandClass
*/
public function create(
string $mainScript,
string $mainCommandClass,
string $workerCommandName,
InputInterface $input,
string $identifier,
int $port
): string {
$commandArguments = array_slice($_SERVER['argv'], 1);
// add implicit "process" command name if missing
if ($commandArguments !== [] && ($commandArguments[0] !== 'process' && $commandArguments[0] !== 'p') && ! defined(
'PHPUNIT_COMPOSER_INSTALL'
)) {
$commandArguments = array_merge(['process'], $commandArguments);
}
$args = [PHP_BINARY, ...$this->createTurboExtensionArguments(), $mainScript, ...$commandArguments];
$workerCommandArray = [];
$mainCommand = $this->commandFromReflectionFactory->create($mainCommandClass);
if ($mainCommand->getName() === null) {
$errorMessage = sprintf('The command name for "%s" is missing', $mainCommand::class);
throw new ParallelShouldNotHappenException($errorMessage);
}
$mainCommandName = $mainCommand->getName();
$mainCommandNames = [$mainCommandName, $mainCommandName[0]];
foreach ($args as $arg) {
// skip command name
if (in_array($arg, $mainCommandNames, true)) {
break;
}
$workerCommandArray[] = escapeshellarg((string) $arg);
}
$workerCommandArray[] = $workerCommandName;
$mainCommandOptionNames = $this->getCommandOptionNames($mainCommand);
$workerCommandOptions = $this->mirrorCommandOptions($input, $mainCommandOptionNames);
$workerCommandArray = array_merge($workerCommandArray, $workerCommandOptions);
// for TCP local server
$workerCommandArray[] = '--port';
$workerCommandArray[] = $port;
$workerCommandArray[] = '--identifier';
$workerCommandArray[] = escapeshellarg($identifier);
/** @var string[] $paths */
$paths = $input->getArgument(Option::SOURCE);
foreach ($paths as $path) {
$workerCommandArray[] = escapeshellarg($path);
}
// set json output
$workerCommandArray[] = self::OPTION_DASHES . Option::OUTPUT_FORMAT;
$workerCommandArray[] = escapeshellarg(JsonOutputFormatter::NAME);
// disable colors, breaks json_decode() otherwise
// @see https://github.com/symfony/symfony/issues/1238
$workerCommandArray[] = '--no-ansi';
// Only pass --config if explicitly set via command line
// If not set, the worker will resolve config using RectorConfigsResolver fallback mechanism
if ($input->hasOption(Option::CONFIG)) {
$configValue = $input->getOption(Option::CONFIG);
if (is_string($configValue) && $configValue !== '') {
$workerCommandArray[] = '--config';
/**
* On parallel, the command is generated with `--config` addition
* Using escapeshellarg() to ensure the --config path escaped, even when it has a space.
*
* eg:
* --config /path/e2e/parallel with space/rector.php
*
* that can cause error:
*
* File /rector-src/e2e/parallel\" was not found
*
* the escaped result is:
*
* --config '/path/e2e/parallel with space/rector.php'
*
* tested in macOS and Ubuntu (github action)
*/
$config = $configValue;
$workerCommandArray[] = escapeshellarg($this->filePathHelper->relativePath($config));
}
}
if ((bool) $input->getOption(Option::COMPOSER_BASED)) {
$workerCommandArray[] = self::OPTION_DASHES . Option::COMPOSER_BASED;
}
return implode(' ', $workerCommandArray);
}
/**
* The input of the worker command for a worker that is forked from the main process instead of spawned;
* carries the same options as the command line of @see create()
*
* @param class-string<Command> $mainCommandClass
* @return array<string, mixed>
*/
public function createInputParameters(
string $mainCommandClass,
InputInterface $input,
string $identifier,
int $port
): array {
$mainCommand = $this->commandFromReflectionFactory->create($mainCommandClass);
$parameters = [];
foreach ($this->getCommandOptionNames($mainCommand) as $mainCommandOptionName) {
if ($this->shouldSkipOption($input, $mainCommandOptionName)) {
continue;
}
$optionValue = $input->getOption($mainCommandOptionName);
// skip clutter
if ($optionValue === null || $optionValue === false) {
continue;
}
$parameters[self::OPTION_DASHES . $mainCommandOptionName] = $optionValue;
}
$parameters[self::OPTION_DASHES . Option::PARALLEL_PORT] = (string) $port;
$parameters[self::OPTION_DASHES . Option::PARALLEL_IDENTIFIER] = $identifier;
$parameters[Option::SOURCE] = $input->getArgument(Option::SOURCE);
$parameters[self::OPTION_DASHES . Option::OUTPUT_FORMAT] = JsonOutputFormatter::NAME;
if ($input->hasOption(Option::CONFIG)) {
$configValue = $input->getOption(Option::CONFIG);
if (is_string($configValue) && $configValue !== '') {
$parameters[self::OPTION_DASHES . Option::CONFIG] = $configValue;
}
}
return $parameters;
}
private function shouldSkipOption(InputInterface $input, string $optionName): bool
{
if (! $input->hasOption($optionName)) {
return true;
}
// skip output format and config, handled separately in create()
return $optionName === Option::OUTPUT_FORMAT || $optionName === Option::CONFIG;
}
/**
* The workers get PHPStan Turbo on their command line, so the main process needs no restart to enable it.
*
* @return string[]
*/
private function createTurboExtensionArguments(): array
{
$extensionPath = TurboExtensionSelector::findExtensionForWorkers();
if ($extensionPath === null) {
return [];
}
// quote the value, so PHP parses it as a string even when the path contains an operator like ~
return [
'-d',
"extension='" . $extensionPath . "'",
'-d',
TurboExtensionSelector::EXTENSION_PATH_INI . "='" . $extensionPath . "'",
];
}
/**
* @return string[]
*/
private function getCommandOptionNames(Command $command): array
{
$inputDefinition = $command->getDefinition();
$optionNames = [];
foreach ($inputDefinition->getOptions() as $inputOption) {
$optionNames[] = $inputOption->getName();
}
return $optionNames;
}
/**
* Keeps all options that are allowed in check command options
*
* @param string[] $mainCommandOptionNames
* @return string[]
*/
private function mirrorCommandOptions(InputInterface $input, array $mainCommandOptionNames): array
{
$workerCommandOptions = [];
foreach ($mainCommandOptionNames as $mainCommandOptionName) {
if ($this->shouldSkipOption($input, $mainCommandOptionName)) {
continue;
}
/** @var bool|string|string[]|null $optionValue */
$optionValue = $input->getOption($mainCommandOptionName);
// skip clutter
if ($optionValue === null) {
continue;
}
if (is_bool($optionValue)) {
if ($optionValue) {
$workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
}
continue;
}
// array options (e.g. repeated --only) are mirrored one flag per value
if (is_array($optionValue)) {
foreach ($optionValue as $singleOptionValue) {
$workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
$workerCommandOptions[] = \escapeshellarg($singleOptionValue);
}
continue;
}
if ($mainCommandOptionName === 'memory-limit') {
// symfony/console does not accept -1 as value without assign
$workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName . '=' . \escapeshellarg(
$optionValue
);
} else {
$workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
$workerCommandOptions[] = \escapeshellarg($optionValue);
}
}
return $workerCommandOptions;
}
}