From 5e642a3a2426760e668204ee304a7703a72af2f0 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 12 Nov 2024 08:58:02 +0800 Subject: [PATCH 1/2] Install `rector/rector` during first run of `php artisan ray:clean` instead of requiring `rector/rector`. Signed-off-by: Mior Muhammad Zaki --- composer.json | 3 ++- src/Commands/CleanRayCommand.php | 10 ++++++++- src/Support/Composer.php | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/Support/Composer.php diff --git a/composer.json b/composer.json index caa7203..871cb55 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,11 @@ "require": { "ext-json": "*", "php": "^7.4|^8.0", + "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": "^0.19.2|^1.0", "spatie/backtrace": "^1.0", "spatie/ray": "^1.41.1", "symfony/stopwatch": "4.2|^5.1|^6.0|^7.0", @@ -35,6 +35,7 @@ "pestphp/pest": "^1.22|^2.0", "phpstan/phpstan": "^1.10.57", "phpunit/phpunit": "^9.3|^10.1", + "rector/rector": "^0.19.2|^1.0", "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..12281b3 --- /dev/null +++ b/src/Support/Composer.php @@ -0,0 +1,38 @@ + $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 + ); + } +} From c9fa643b64c5e49f80b33546ff53c8f085f5873f Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 12 Nov 2024 09:02:27 +0800 Subject: [PATCH 2/2] wip Signed-off-by: Mior Muhammad Zaki --- src/Support/Composer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Support/Composer.php b/src/Support/Composer.php index 12281b3..11229ff 100644 --- a/src/Support/Composer.php +++ b/src/Support/Composer.php @@ -10,6 +10,8 @@ class Composer extends \Illuminate\Support\Composer /** * Install the given Composer packages into the application. * + * Override this method for `illuminate/support` 10 and below. + * * @param array $packages * @param bool $dev * @param \Closure|\Symfony\Component\Console\Output\OutputInterface|null $output