Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow command optimization #147

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a command right? Rename the variable to optimizeCommand, same for clearCommand and packagenameKey

'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
55 changes: 55 additions & 0 deletions tests/PackageServiceProviderTests/PackageOptimizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

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

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');
});
Loading