Skip to content

Commit

Permalink
Merge pull request #363 from crynobone/optional-rector
Browse files Browse the repository at this point in the history
Install `rector/rector` during first run of `php artisan ray:clean` instead of requiring `rector/rector`
  • Loading branch information
freekmurze authored Dec 9, 2024
2 parents 2c9d66b + 638ddde commit d9262d1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
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
40 changes: 40 additions & 0 deletions src/Support/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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.
*
* Override this method for `illuminate/support` 10 and below.
*
* @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 d9262d1

Please sign in to comment.