From 1ce24874e19aa188b78ff6dd9c568efd345dbecc Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Mar 2024 23:49:02 -0300 Subject: [PATCH 1/7] Adds Laravel 11 support --- .github/workflows/run-tests.yml | 6 +- composer.json | 10 +-- .../Concerns/InstallsForImportmap.php | 73 ++++++++++--------- src/Commands/Concerns/InstallsForNode.php | 56 ++++++-------- src/Commands/InstallCommand.php | 16 ++-- 5 files changed, 81 insertions(+), 80 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 2101270..fc12afb 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,13 +14,13 @@ jobs: matrix: os: [ubuntu-latest, windows-latest] php: [8.1, 8.2] - laravel: [9.*, 10.*] + laravel: [10.*, 11*] stability: [prefer-lowest, prefer-stable] include: - - laravel: 9.* - testbench: 7.* - laravel: 10.* testbench: 8.* + - laravel: 11.* + testbench: 9.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index bf25535..7f7d853 100644 --- a/composer.json +++ b/composer.json @@ -18,15 +18,15 @@ } ], "require": { - "php": "^8.1", + "php": "^8.1|^8.2", "spatie/laravel-package-tools": "^1.9.2", - "illuminate/contracts": "^9.0|^10.0" + "illuminate/contracts": "^10.0|^11.0" }, "require-dev": { "laravel/pint": "^1.0", - "nunomaduro/collision": "^6.0", - "orchestra/testbench": "^7.0|^8.0", - "phpunit/phpunit": "^9.5" + "nunomaduro/collision": "^7.10|^8.1", + "orchestra/testbench": "^8.0|^9.1", + "phpunit/phpunit": "^10.5" }, "autoload": { "psr-4": { diff --git a/src/Commands/Concerns/InstallsForImportmap.php b/src/Commands/Concerns/InstallsForImportmap.php index e5e853f..762d599 100644 --- a/src/Commands/Concerns/InstallsForImportmap.php +++ b/src/Commands/Concerns/InstallsForImportmap.php @@ -3,6 +3,7 @@ namespace HotwiredLaravel\StimulusLaravel\Commands\Concerns; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Process; /** * @mixin \HotwiredLaravel\StimulusLaravel\Commands\InstallCommand @@ -17,51 +18,55 @@ protected function installsForImportmaps() protected function publishJsFilesForImportmaps() { - $this->components->task('publishing JS files', function () { - File::ensureDirectoryExists(resource_path('js/controllers')); - File::ensureDirectoryExists(resource_path('js/libs')); + File::ensureDirectoryExists(resource_path('js/controllers')); + File::ensureDirectoryExists(resource_path('js/libs')); - File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js')); - File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); - File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-importmap.js', resource_path('js/controllers/index.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-importmap.js', resource_path('js/controllers/index.js')); - $libsIndexFile = resource_path('js/libs/index.js'); - $libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-importmap.js'; + $libsIndexFile = resource_path('js/libs/index.js'); + $libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-importmap.js'; - if (File::exists($libsIndexFile)) { - $importLine = trim(File::get($libsIndexSourceFile)); + if (File::exists($libsIndexFile)) { + $importLine = trim(File::get($libsIndexSourceFile)); - if (! str_contains(File::get($libsIndexFile), $importLine)) { - File::append($libsIndexFile, PHP_EOL.$importLine.PHP_EOL); - } - } else { - File::copy($libsIndexSourceFile, $libsIndexFile); + if (! str_contains(File::get($libsIndexFile), $importLine)) { + File::append($libsIndexFile, PHP_EOL.$importLine.PHP_EOL); } - - return true; - }); + } else { + File::copy($libsIndexSourceFile, $libsIndexFile); + } } protected function registerImportmapPins() { - $this->components->task('pinning JS dependency (importmap)', function () { - $this->callSilently('importmap:pin', [ - 'packages' => collect($this->jsPackages()) - ->map(fn ($version, $package) => "{$package}@{$version}") - ->values() - ->all(), - ]); - - // Publishes the `@hotwired/stimulus-loading` package to public/ - $this->callSilently('vendor:publish', [ - '--tag' => 'stimulus-laravel-assets', - ]); + $dependencies = collect($this->jsPackages()) + ->map(fn ($version, $package) => "{$package}@{$version}") + ->values() + ->all(); - File::append($this->importmapsFile(), <<<'IMPORTMAP' - Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true); - IMPORTMAP); + Process::forever()->run(array_merge([ + $this->phpBinary(), + 'artisan', + 'importmap:pin', + ], $dependencies), function ($_type, $output) { + $this->output->write($output); + }); - return true; + // Publishes the `@hotwired/stimulus-loading` package to public/vendor + Process::forever()->run([ + $this->phpBinary(), + 'artisan', + 'vendor:publish', + '--tag', + 'stimulus-laravel-assets', + ], function ($_type, $output) { + $this->output->write($output); }); + + File::append($this->importmapsFile(), <<<'IMPORTMAP' + Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true); + IMPORTMAP); } } diff --git a/src/Commands/Concerns/InstallsForNode.php b/src/Commands/Concerns/InstallsForNode.php index 1088ac6..4622eb4 100644 --- a/src/Commands/Concerns/InstallsForNode.php +++ b/src/Commands/Concerns/InstallsForNode.php @@ -17,49 +17,41 @@ protected function installsForNode() protected function publishJsFilesForNode() { - $this->components->task('publishing JS files', function () { - File::ensureDirectoryExists(resource_path('js/controllers')); - File::ensureDirectoryExists(resource_path('js/libs')); + File::ensureDirectoryExists(resource_path('js/controllers')); + File::ensureDirectoryExists(resource_path('js/libs')); - File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js')); - File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); - File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-node.js', resource_path('js/controllers/index.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); + File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-node.js', resource_path('js/controllers/index.js')); - $libsIndexFile = resource_path('js/libs/index.js'); - $libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-node.js'; + $libsIndexFile = resource_path('js/libs/index.js'); + $libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-node.js'; - if (File::exists($libsIndexFile)) { - $importLine = trim(File::get($libsIndexSourceFile)); + if (File::exists($libsIndexFile)) { + $importLine = trim(File::get($libsIndexSourceFile)); - if (! str_contains(File::get($libsIndexFile), $importLine)) { - File::append($libsIndexFile, $importLine.PHP_EOL); - } - } else { - File::copy($libsIndexSourceFile, $libsIndexFile); + if (! str_contains(File::get($libsIndexFile), $importLine)) { + File::append($libsIndexFile, $importLine.PHP_EOL); } + } else { + File::copy($libsIndexSourceFile, $libsIndexFile); + } - if (! str_contains(File::get(resource_path('js/app.js')), "import './libs';")) { - File::append(resource_path('js/app.js'), <<<'JS' - import './libs'; - - JS); - } + if (! str_contains(File::get(resource_path('js/app.js')), "import './libs';")) { + File::append(resource_path('js/app.js'), <<<'JS' + import './libs'; - return true; - }); + JS); + } } protected function updateNpmPackagesForNode() { - $this->components->task('registering NPM dependency', function () { - $this->updateNodePackages(function ($packages) { - return array_merge( - $packages, - $this->jsPackages(), - ); - }); - - return true; + $this->updateNodePackages(function ($packages) { + return array_merge( + $packages, + $this->jsPackages(), + ); }); } diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index f94c732..fe68bf0 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; use RuntimeException; +use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; class InstallCommand extends Command @@ -20,8 +21,6 @@ class InstallCommand extends Command public function handle(): int { - $this->components->info('Installing Stimulus Laravel'); - if ($this->usingImportmaps()) { $this->installsForImportmaps(); } else { @@ -37,8 +36,8 @@ public function handle(): int } $this->newLine(); - $this->components->info('Done'); - $this->newLine(); + + $this->components->info('Stimulus Laravel was installed successfully.'); return self::SUCCESS; } @@ -74,13 +73,18 @@ protected function runCommands($commands) }); } - protected function usingImportmaps(): bool + private function usingImportmaps(): bool { return File::exists($this->importmapsFile()); } - protected function importmapsFile(): string + private function importmapsFile(): string { return base_path('routes/importmap.php'); } + + protected function phpBinary() + { + return (new PhpExecutableFinder())->find(false) ?: 'php'; + } } From e9fa188bd7e4e9f8d7da229f6ef4800cc44f1244 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Mar 2024 23:50:26 -0300 Subject: [PATCH 2/7] Pint --- src/StimulusGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StimulusGenerator.php b/src/StimulusGenerator.php index 14e9d4e..be912b0 100644 --- a/src/StimulusGenerator.php +++ b/src/StimulusGenerator.php @@ -12,7 +12,7 @@ public function __construct(private ?string $targetFolder = null) $this->targetFolder ??= rtrim(resource_path('js/controllers'), '/'); } - public function create(string $name, string $stub = null, callable $replacementsCallback = null): array + public function create(string $name, ?string $stub = null, ?callable $replacementsCallback = null): array { $replacementsCallback ??= fn ($replacements) => $replacements; $controllerName = $this->controllerName($name); @@ -36,7 +36,7 @@ public function create(string $name, string $stub = null, callable $replacements ]; } - public function createStrada(string $prefix, string $name, string $bridgeName = null): array + public function createStrada(string $prefix, string $name, ?string $bridgeName = null): array { return $this->create("$prefix/$name", stub: __DIR__.'/../stubs/strada.stub', replacementsCallback: function (array $replacements) use ($bridgeName) { return array_merge( From f5a871cdd94d3d5b1b355110eaba03e1b5fe8067 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Mar 2024 23:52:50 -0300 Subject: [PATCH 3/7] Fix workflow --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index fc12afb..7f85a27 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,7 +14,7 @@ jobs: matrix: os: [ubuntu-latest, windows-latest] php: [8.1, 8.2] - laravel: [10.*, 11*] + laravel: [10.*, 11.*] stability: [prefer-lowest, prefer-stable] include: - laravel: 10.* From 54904eadf9b3044fa2d4b8504af388ee0b59ee47 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Mar 2024 23:54:19 -0300 Subject: [PATCH 4/7] Tweaks phpunit config --- .gitignore | 1 + phpunit.xml.dist | 42 ++++++++++++------------------------------ 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 83c9b9f..8054c8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .phpunit.result.cache +.phpunit.cache build composer.lock coverage diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c3b5e6e..d549932 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,39 +1,21 @@ - + - + tests - + + + + ./src - - - - - - - - - + From a1379824c2138376df9611ac6bf6452d488fddd5 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Tue, 5 Mar 2024 23:56:10 -0300 Subject: [PATCH 5/7] Exclude L11 on 8.1 --- .github/workflows/run-tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7f85a27..b405869 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -21,6 +21,9 @@ jobs: testbench: 8.* - laravel: 11.* testbench: 9.* + exclude: + - laravel: 11.* + php: 8.1 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} From e6e4c5b911352b402c7f71946f58fbf657ff8ebe Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Wed, 6 Mar 2024 00:04:46 -0300 Subject: [PATCH 6/7] Skip windows build on L11 and PHP8.2 --- .github/workflows/run-tests.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b405869..576b748 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -24,6 +24,9 @@ jobs: exclude: - laravel: 11.* php: 8.1 + - laravel: 11.x + php: 8.2 + os: windows-latest name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} From ee0dcadfb7fec212099c2351e266dbfe9dc15f8a Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Wed, 6 Mar 2024 00:08:23 -0300 Subject: [PATCH 7/7] Tweaks the exclusion rule --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 576b748..2af4350 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -24,7 +24,7 @@ jobs: exclude: - laravel: 11.* php: 8.1 - - laravel: 11.x + - laravel: 11.* php: 8.2 os: windows-latest