Skip to content

Commit 3d75cf2

Browse files
committed
add symfony process as a command wrapper for rector
1 parent 3d0b9f9 commit 3d75cf2

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"cakephp/console": "^5.1.5",
1010
"nette/utils": "^4.0",
1111
"rector/rector": "dev-main",
12+
"symfony/process": "^5.4|^6.0|^7.0",
1213
"symfony/string": "^6.0 || ^7.0"
1314
},
1415
"autoload": {

config/rector/sets/cakephp60.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@
362362
// Can't rename _moveUp/_moveDown as it conflicts with the TreeBehavior::moveUp()/moveDown() methods
363363
// Can't rename _removeFromTree as it conflicts with the TreeBehavior::removeFromTree() method
364364
'_setChildrenLevel', '_setParent', '_setAsRoot', '_unmarkInternalTree',
365-
'_removeFromTree', '_getNode', '_recoverTree', '_getMax',
366-
'_sync', '_scope', '_ensureFields', '_getPrimaryKey',
365+
'_getNode', '_recoverTree', '_getMax', '_sync', '_scope', '_ensureFields', '_getPrimaryKey',
367366
],
368367
'Cake\ORM\Behavior\CounterCacheBehavior' => [
369368
'_processAssociations', '_processAssociation', '_shouldUpdateCount', '_getCount',

src/Command/RectorCommand.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Cake\Console\BaseCommand;
2121
use Cake\Console\ConsoleIo;
2222
use Cake\Console\ConsoleOptionParser;
23+
use Symfony\Component\Process\Process;
2324

2425
/**
2526
* Runs rector rulesets against the provided path.
@@ -59,11 +60,11 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
5960

6061
$result = $this->runRector($io, $args, $autoload);
6162
if ($result === false) {
62-
$io->error('Could not run rector. Ensure that `php` is on your PATH.');
63+
$io->error('Something went wrong while running rector. Ensure that `php` is on your PATH.');
6364

6465
return static::CODE_ERROR;
6566
}
66-
$io->success('Rector applied successfully');
67+
$io->success('🎉 Upgrade complete! 🎉');
6768

6869
return static::CODE_SUCCESS;
6970
}
@@ -93,39 +94,28 @@ protected function runRector(ConsoleIo $io, Arguments $args, string $autoload):
9394
);
9495
$io->verbose("Running <info>{$command}</info>");
9596

96-
$descriptorSpec = [
97-
0 => ['pipe', 'r'],
98-
1 => ['pipe', 'w'],
99-
2 => ['pipe', 'w'],
100-
];
101-
$process = proc_open(
102-
$command,
103-
$descriptorSpec,
104-
$pipes,
105-
);
106-
if (!is_resource($process)) {
107-
$io->error('Could not create rector process');
97+
$io->info('Starting rector at ' . date('Y-m-d H:i:s'));
10898

109-
return false;
110-
}
99+
$process = Process::fromShellCommandline($command);
100+
$process->setEnv($_ENV);
101+
$process->setTimeout(null);
102+
$process->start();
111103

112-
while (true) {
113-
if (feof($pipes[1]) && feof($pipes[2])) {
114-
break;
115-
}
116-
$output = fread($pipes[1], 1024);
117-
if ($output) {
118-
$io->out($output);
119-
}
120-
$error = fread($pipes[2], 1024);
121-
if ($error) {
122-
$io->err($error);
104+
foreach ($process as $type => $data) {
105+
if ($type === Process::OUT) {
106+
$io->out($data);
107+
} elseif ($type === Process::ERR) {
108+
$io->err($data);
123109
}
124110
}
125111

126-
fclose($pipes[1]);
127-
fclose($pipes[2]);
128-
proc_close($process);
112+
if (!$process->isSuccessful()) {
113+
$io->error('Something went wrong while running rector.');
114+
115+
return false;
116+
}
117+
118+
$io->info('Rector completed successfully at ' . date('Y-m-d H:i:s'));
129119

130120
return true;
131121
}

0 commit comments

Comments
 (0)