Skip to content

Commit

Permalink
feat: exclude command file from being register/publish
Browse files Browse the repository at this point in the history
Signed-off-by: Ayax Córdova <ayax.cordova@aydev.mx>
  • Loading branch information
asciito committed Sep 24, 2023
1 parent 86f793c commit 1b2339b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/Package/Concerns/HasCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,34 @@ trait HasCommands
{
protected array $commands = [];

protected bool $preventLoadDefaultCommandsFolder = false;

protected bool $shouldLoadCommandsFromDefaultFolder = false;

protected array $excludedCommands = [];

public function hasCommands(): bool
{
return filled($this->commands) || $this->shouldLoadCommandsFromDefaultFolder;
return $this->shouldLoadDefaultCommandsFolder() || filled($this->commands);
}

private function shouldLoadDefaultCommandsFolder(): bool
{
return !$this->preventLoadDefaultCommandsFolder && $this->shouldLoadCommandsFromDefaultFolder;
}

public function preventDefaultCommands(): static
{
$this->preventLoadDefaultCommandsFolder = true;

return $this;
}

public function withCommands(string|array ...$command): static
{
if (filled($command)) {
$this->commands = collect($command)
->flatten()
->mapWithKeys(fn (string $value) => [$value => true])
->merge($this->commands)
->all();
}
Expand All @@ -36,29 +51,36 @@ public function getRegisteredCommands(): Collection
{
$commands = [];

if ($this->shouldLoadCommandsFromDefaultFolder) {
if ($this->shouldLoadDefaultCommandsFolder()) {
$commands = $this->loadCommandsFromDefaultFolder();
}

return collect($this->commands)
->merge($commands)
->keys();
->filter(fn (string $command) => !in_array($command, $this->excludedCommands));
}

public function loadCommandsFromDefaultFolder()
{
return collect(File::files($this->getBasePath('Console/Commands')))
->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php')
->mapWithKeys(function (SplFileInfo $file) {
$fqcn = str($file)
->after('Console/')
->replace('/', '\\')
->remove('.php')
->prepend($this->getNamespace().'\\Console\\')
->toString();

return [$fqcn => true];
})
->map(fn (SplFileInfo $file) => str($file)
->after('Console/')
->replace('/', '\\')
->remove('.php')
->prepend($this->getNamespace() . '\\Console\\')
->toString()
)
->all();
}

/**
* Un-register a previously registered command
*/
public function unregisterCommand(string $fqcn): static
{
$this->excludedCommands[] = $fqcn;

return $this;
}
}
13 changes: 13 additions & 0 deletions src/Package/Contracts/WithCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ interface WithCommands
*/
public function hasCommands(): bool;

/**
* Prevent registering the default command folder
*
* Calling this method ensures that the command files in the default folder
* wouldn't be loaded automatically.
*/
public function preventDefaultCommands(): static;

/**
* Register the config file(s) for the package
*
Expand All @@ -29,4 +37,9 @@ public function withCommands(string|array ...$command): static;
* @return Collection The commands registered in the package
*/
public function getRegisteredCommands(): Collection;

/**
* Un-register a previously registered command
*/
public function unregisterCommand(string $fqcn): static;
}
34 changes: 34 additions & 0 deletions tests/Feature/Package/UnregisterCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Asciito\LaravelPackage\Package\Package;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Workbench\App\Console\Commands\Extras\PackageTestCommandTwo;
use Workbench\App\Console\Commands\PackageTestCommandOne;
use function Pest\Laravel\{artisan};

trait UnregisterPackageCommand
{
protected function configurePackage(Package $package): void
{
$package
->withCommands(PackageTestCommandTwo::class)
->unregisterCommand(PackageTestCommandOne::class);
}
}

uses(UnregisterPackageCommand::class);

it('register commands', function () {
artisan('list')
->expectsOutputToContain('package:two')
->doesntExpectOutputToContain('package:one')
->assertSuccessful();
});

it('run commands', function () {
artisan('package:two')
->expectsOutput('Package command two')
->assertSuccessful();

artisan('package:one')->run();
})->throws(CommandNotFoundException::class);

0 comments on commit 1b2339b

Please sign in to comment.