forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerCommandLineFactory.php
More file actions
203 lines (167 loc) · 6.99 KB
/
WorkerCommandLineFactory.php
File metadata and controls
203 lines (167 loc) · 6.99 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
<?php
declare(strict_types=1);
namespace Rector\Parallel\Command;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Configuration\Option;
use Rector\FileSystem\FilePathHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyParallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyParallel\Reflection\CommandFromReflectionFactory;
/**
* @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, $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 ($input->getOption(Option::ONLY) !== null) {
$workerCommandArray[] = self::OPTION_DASHES . Option::ONLY;
$workerCommandArray[] = escapeshellarg((string) $input->getOption(Option::ONLY));
}
return implode(' ', $workerCommandArray);
}
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;
}
/**
* @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|null $optionValue */
$optionValue = $input->getOption($mainCommandOptionName);
// skip clutter
if ($optionValue === null) {
continue;
}
if (is_bool($optionValue)) {
if ($optionValue) {
$workerCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
}
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;
}
}