Skip to content

Commit

Permalink
Install rector/rector during first run of php artisan ray:clean
Browse files Browse the repository at this point in the history
instead of requiring `rector/rector`.

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Nov 12, 2024
1 parent 1d10366 commit 5e642a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
10 changes: 9 additions & 1 deletion src/Commands/CleanRayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

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
{
protected $signature = 'ray:clean';

protected $description = 'Remove all Ray calls from your codebase.';

public function handle()
public function handle(Filesystem $files)
{
$directories = [
'app',
Expand All @@ -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);

Expand Down
38 changes: 38 additions & 0 deletions src/Support/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\LaravelRay\Support;

use Closure;
use Symfony\Component\Console\Output\OutputInterface;

class Composer extends \Illuminate\Support\Composer
{
/**
* Install the given Composer packages into the application.
*
* @param array<int, string> $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
);
}
}

0 comments on commit 5e642a3

Please sign in to comment.