Skip to content

Commit

Permalink
Ability to optimize command
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley Aven committed Oct 28, 2024
1 parent 7efdb79 commit 6f79fa1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Package

public ?string $publishableProviderName = null;

public array $optimizeCommands = [];

public function name(string $name): static
{
$this->name = $name;
Expand Down Expand Up @@ -218,6 +220,25 @@ public function hasRoutes(...$routeFileNames): static
return $this;
}

public function hasOptimization(?string $optimize = null, ?string $clear = null, ?string $key = null): static
{
$this->optimizeCommands[] = [
'optimize' => $optimize,
'clear' => $clear,
'key' => $key
];

if (! in_array($optimize, $this->consoleCommands)) {
$this->consoleCommands[] = $optimize;
}

if (! in_array($clear, $this->consoleCommands)) {
$this->consoleCommands[] = $clear;
}

return $this;
}

public function basePath(string $directory = null): string
{
if ($directory === null) {
Expand Down
6 changes: 6 additions & 0 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public function boot()
$this->package->basePath('/../resources/dist') => public_path("vendor/{$this->package->shortName()}"),
], "{$this->package->shortName()}-assets");
}

if (! empty($this->package->optimizeCommands) && method_exists($this, 'optimizes')) {
foreach($this->package->optimizeCommands as $optimizeCmd) {
$this->optimizes($optimizeCmd['optimize'], $optimizeCmd['clear'], $optimizeCmd['key'] ?? null);
}
}
}

if (! empty($this->package->commands)) {
Expand Down
57 changes: 57 additions & 0 deletions tests/PackageServiceProviderTests/PackageOptimizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\Tests\TestClasses\FourthTestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\OtherTestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\TestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\ThirdTestCommand;

trait ConfigurePackageOptimizationTest
{
public function configurePackage(Package $package)
{
$package->name('laravel-package-tools');

if (version_compare(app()->version(), '11.27.1', '>=')) {
$package->hasOptimization(TestCommand::class, OtherTestCommand::class, 'laravel-package-tools');
}
}
}

uses(ConfigurePackageOptimizationTest::class);

it('can call optimize commands', function () {
if (version_compare(app()->version(), '11.27.1', '<')) {
$this->markTestSkipped('Laravel 11+ functionality');
}

$this
->artisan('test-command')
->assertExitCode(0);
});

it('can call optimize:clear commands', function () {
if (version_compare(app()->version(), '11.27.1', '<')) {
$this->markTestSkipped('Laravel 11+ functionality');
}

$this
->artisan('other-test-command')
->assertExitCode(0);
});

it('registered optimize with laravel', function() {
if (version_compare(app()->version(), '11.27.1', '<')) {
$this->markTestSkipped('Laravel 11+ functionality');
}

$this->artisan('optimize')->expectsOutputToContain('laravel-package-tools');
});

it('registered optimize:clear with laravel', function() {
if (version_compare(app()->version(), '11.27.1', '<')) {
$this->markTestSkipped('Laravel 11+ functionality');
}

$this->artisan('optimize:clear')->expectsOutputToContain('laravel-package-tools');
});

0 comments on commit 6f79fa1

Please sign in to comment.