diff --git a/composer.json b/composer.json index 10e36a9..aa00d48 100644 --- a/composer.json +++ b/composer.json @@ -28,11 +28,11 @@ "require": { "php": "^7.4 || ^8.0", "ext-json": "*", + "composer-runtime-api": "^2.2", "illuminate/contracts": "^7.20 || ^8.19 || ^9.0 || ^10.0 || ^11.0", "illuminate/database": "^7.20 || ^8.19 || ^9.0 || ^10.0 || ^11.0", "illuminate/queue": "^7.20 || ^8.19 || ^9.0 || ^10.0 || ^11.0", "illuminate/support": "^7.20 || ^8.19 || ^9.0 || ^10.0 || ^11.0", - "rector/rector": "^2.0.0-rc2", "spatie/backtrace": "^1.0", "spatie/ray": "^1.41.3", "symfony/stopwatch": "4.2 || ^5.1 || ^6.0 || ^7.0", @@ -45,6 +45,7 @@ "pestphp/pest": "^1.22 || ^2.0", "phpstan/phpstan": "^1.10.57 || ^2.0.2", "phpunit/phpunit": "^9.3 || ^10.1", + "rector/rector": "dev-main", "spatie/pest-plugin-snapshots": "^1.1 || ^2.0", "symfony/var-dumper": "^4.2 || ^5.1 || ^6.0 || ^7.0.3" }, diff --git a/src/Commands/CleanRayCommand.php b/src/Commands/CleanRayCommand.php index a5ac864..e0160c5 100644 --- a/src/Commands/CleanRayCommand.php +++ b/src/Commands/CleanRayCommand.php @@ -2,8 +2,11 @@ namespace Spatie\LaravelRay\Commands; +use Composer\InstalledVersions; use Illuminate\Console\Command; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Process; +use Spatie\LaravelRay\Support\Composer; class CleanRayCommand extends Command { @@ -11,7 +14,7 @@ class CleanRayCommand extends Command protected $description = 'Remove all Ray calls from your codebase.'; - public function handle() + public function handle(Filesystem $files) { $directories = [ 'app', @@ -23,6 +26,11 @@ public function handle() 'tests', ]; + if (! InstalledVersions::isInstalled('rector/rector')) { + (new Composer($files, defined('TESTBENCH_WORKING_PATH') ? TESTBENCH_WORKING_PATH : base_path())) + ->requirePackages(['rector/rector'], true, $this->output); + } + $this->withProgressBar($directories, function ($directory) { $result = Process::run('./vendor/bin/remove-ray.sh ' . $directory); diff --git a/src/Support/Composer.php b/src/Support/Composer.php new file mode 100644 index 0000000..11229ff --- /dev/null +++ b/src/Support/Composer.php @@ -0,0 +1,40 @@ + $packages + * @param bool $dev + * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output + * @param string|null $composerBinary + * @return bool + */ + public function requirePackages(array $packages, bool $dev = false, Closure|OutputInterface|null $output = null, $composerBinary = null) + { + $command = collect([ + ...$this->findComposer($composerBinary), + 'require', + ...$packages, + ]) + ->when($dev, function ($command) { + $command->push('--dev'); + })->all(); + + return 0 === $this->getProcess($command, ['COMPOSER_MEMORY_LIMIT' => '-1']) + ->run( + $output instanceof OutputInterface + ? function ($type, $line) use ($output) { + $output->write(' '.$line); + } : $output + ); + } +}