Skip to content

Commit ba1e050

Browse files
committed
add command to upgrade using Rector
1 parent 2167299 commit ba1e050

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

rector.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
66
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
77
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
8-
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
98
use Rector\Config\RectorConfig;
109
use Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector;
1110
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
1211
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
1312
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
14-
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector;
1513
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
1614
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
1715
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
1816
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
1917
use Rector\Php82\Rector\Param\AddSensitiveParameterAttributeRector;
2018
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
2119
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
20+
use Rector\Renaming\Rector\Name\RenameClassRector;
2221
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
2322
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
2423
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
@@ -36,6 +35,9 @@
3635
'secret',
3736
],
3837
])
38+
->withConfiguredRule(RenameClassRector::class, [
39+
'Tempest\\Database\\Id' => 'Tempest\\Database\\PrimaryKey',
40+
])
3941
->withRules([
4042
ExplicitNullableParamTypeRector::class,
4143
])
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Framework\Commands;
6+
7+
use Composer\Semver\Semver;
8+
use Symfony\Component\Process\Exception\ProcessFailedException;
9+
use Symfony\Component\Process\Process;
10+
use Tempest\Console\Console;
11+
use Tempest\Console\ConsoleArgument;
12+
use Tempest\Console\ConsoleCommand;
13+
use Tempest\Console\ExitCode;
14+
use Tempest\Container\Singleton;
15+
use Tempest\Core\Kernel;
16+
17+
#[Singleton]
18+
final readonly class UpgradeCommand
19+
{
20+
public function __construct(
21+
private Console $console,
22+
) {}
23+
24+
#[ConsoleCommand(
25+
name: 'upgrade:tempest',
26+
description: 'Upgrades the application to the latest version',
27+
)]
28+
public function __invoke(
29+
#[ConsoleArgument(description: 'Upgrade to a specific version', help: 'If not specified, the latest version is used')]
30+
string $version = Kernel::VERSION,
31+
#[ConsoleArgument(description: 'Dry run the upgrade')]
32+
bool $dryRun = false,
33+
#[ConsoleArgument(description: 'Verbose output')]
34+
bool $_verbose = false,
35+
): ExitCode {
36+
$command = 'vendor/bin/rector --no-ansi --no-progress-bar --no-diffs';
37+
if ($dryRun) {
38+
$this->console->info('Dry run enabled');
39+
$command .= ' --dry-run';
40+
}
41+
42+
$rectors = $this->getRectorsForVersion($version);
43+
if (! $rectors->valid()) {
44+
$this->console->info('No rectors found for this version');
45+
46+
return ExitCode::SUCCESS;
47+
}
48+
49+
foreach ($rectors as $rule) {
50+
$command .= " --only=\"{$rule}\"";
51+
}
52+
53+
$this->console->info("Running <code>{$command}</code>");
54+
try {
55+
$processed = Process::fromShellCommandline($command)->mustRun();
56+
} catch (ProcessFailedException $e) {
57+
$this->console->error($e->getProcess()->getErrorOutput());
58+
59+
return ExitCode::ERROR;
60+
}
61+
62+
$this->console->info(trim($processed->getOutput()));
63+
64+
return ExitCode::SUCCESS;
65+
}
66+
67+
private function getRectorsForVersion(string $version): \Generator
68+
{
69+
return match (true) {
70+
Semver::satisfies($version, '^1.0.0') => [
71+
],
72+
Semver::satisfies($version, '^2.0.0') => [
73+
yield 'Rector\Renaming\Rector\Name\RenameClassRector',
74+
],
75+
default => [],
76+
};
77+
}
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Integration\Console\Commands;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Tempest\Console\ExitCode;
9+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
10+
11+
class UpgradeCommandTest extends FrameworkIntegrationTestCase
12+
{
13+
#[Test]
14+
public function no_upgrades_for_v1(): void
15+
{
16+
$this->console
17+
->call('upgrade:tempest', ['version' => 'v1.0.0', 'dry-run' => true])
18+
->assertContains(' No rectors found for this version')
19+
->assertExitCode(ExitCode::SUCCESS);
20+
21+
}
22+
23+
#[Test]
24+
public function upgrade_to_v2(): void
25+
{
26+
$this->console
27+
->call('upgrade:tempest', ['version' => 'v2.0.0', 'dry-run' => true])
28+
->assertContains('vendor/bin/rector --no-ansi --no-progress-bar --no-diffs --dry-run --only="Rector\Renaming\Rector\Name\RenameClassRector"')
29+
->assertContains('[OK] Rector is done!')
30+
->dump()
31+
->assertExitCode(ExitCode::SUCCESS);
32+
}
33+
}

0 commit comments

Comments
 (0)