|
| 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 | + |
| 16 | +#[Singleton] |
| 17 | +final readonly class UpgradeCommand |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + private Console $console, |
| 21 | + ) {} |
| 22 | + |
| 23 | + #[ConsoleCommand( |
| 24 | + name: 'upgrade', |
| 25 | + description: 'Upgrades the application to the latest version', |
| 26 | + )] |
| 27 | + public function __invoke( |
| 28 | + #[ConsoleArgument(description: 'Upgrade to a specific version')] |
| 29 | + string $version, |
| 30 | + #[ConsoleArgument(description: 'Dry run the upgrade')] |
| 31 | + bool $dryRun, |
| 32 | + #[ConsoleArgument(description: 'Verbose output')] |
| 33 | + bool $_verbose = false, |
| 34 | + ): ExitCode { |
| 35 | + if (empty($version)) { |
| 36 | + $this->console->error('Please specify a version to upgrade to'); |
| 37 | + |
| 38 | + return ExitCode::INVALID; |
| 39 | + } |
| 40 | + |
| 41 | + $command = 'vendor/bin/rector --no-ansi'; |
| 42 | + if ($dryRun) { |
| 43 | + $this->console->info('Dry run enabled'); |
| 44 | + $command .= ' --dry-run'; |
| 45 | + } |
| 46 | + |
| 47 | + foreach ($this->getRectorsForVersion($version) as $rule) { |
| 48 | + $command .= " --only=\"{$rule}\""; |
| 49 | + } |
| 50 | + |
| 51 | + $this->console->info("Running <code>{$command}</code>"); |
| 52 | + try { |
| 53 | + Process::fromShellCommandline($command)->mustRun(); |
| 54 | + } catch (ProcessFailedException $e) { |
| 55 | + $this->console->error($e->getProcess()->getErrorOutput()); |
| 56 | + |
| 57 | + return ExitCode::ERROR; |
| 58 | + } |
| 59 | + |
| 60 | + return ExitCode::SUCCESS; |
| 61 | + } |
| 62 | + |
| 63 | + private function getRectorsForVersion(string $version): \Generator |
| 64 | + { |
| 65 | + return match (true) { |
| 66 | + Semver::satisfies($version, '^2.0.0') => [ |
| 67 | + yield 'Utils\Rector\Rector\RenameIdToPrimaryKeyRector', |
| 68 | + ], |
| 69 | + }; |
| 70 | + } |
| 71 | +} |
0 commit comments