-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: asciito <ayax.cordova@aydev.mx>
- Loading branch information
Showing
6 changed files
with
232 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Asciito\LaravelPackage\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class InstallPackageCommand extends Command | ||
{ | ||
protected function configure(): void | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Asciito\LaravelPackage\Package\Concerns; | ||
|
||
use Closure; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Artisan; | ||
use function Laravel\Prompts\intro; | ||
use function Laravel\Prompts\outro; | ||
use function Laravel\Prompts\spin; | ||
|
||
trait HasInstallCommand | ||
{ | ||
protected string $commandSignature; | ||
|
||
protected ?Closure $afterCallback; | ||
|
||
protected array $components = [ | ||
'config' => 'Install Config Component', | ||
'migrations' => 'Install Migrations Component', | ||
]; | ||
|
||
protected bool $commandShouldAdded = false; | ||
|
||
public function hasInstallCommand(): bool | ||
{ | ||
return $this->commandShouldAdded; | ||
} | ||
|
||
public function withInstallCommand(string $signature = '', ?Closure $after = null): static | ||
{ | ||
$this->commandSignature = $signature; | ||
|
||
$this->afterCallback = $after; | ||
|
||
$this->commandShouldAdded = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function command(Command $command): int | ||
{ | ||
intro('Installing Package Components'); | ||
|
||
$all = $command->option('all'); | ||
|
||
$some = false; | ||
|
||
foreach ($this->components as $name => $description) { | ||
$name = str($name); | ||
|
||
if ($all || $command->option($name)) { | ||
spin(function () use($command, $name) { | ||
Artisan::call( | ||
'vendor:publish', | ||
['--tag' => $this->prefixWithPackageName($name)] | ||
); | ||
}, "Publishing Component [{$name->title()}] files..."); | ||
|
||
$some = true; | ||
} | ||
} | ||
|
||
call_user_func($this->afterCallback, $command); | ||
|
||
$message = 'Package Component(s) installed'; | ||
|
||
if (! $some) { | ||
$message = 'None package components were installed'; | ||
} | ||
|
||
outro($message); | ||
|
||
return $command::SUCCESS; | ||
} | ||
|
||
public function getInstallCommandSignature(): string | ||
{ | ||
if (isset($this->commandSignature)) { | ||
return $this->commandSignature; | ||
} | ||
|
||
return $this->prefixWithPackageName('install', ':'); | ||
} | ||
|
||
public function preConfigureInstallCommand(Command $command): void | ||
{ | ||
foreach ($this->components as $component => $description) { | ||
$command->addOption($component, description: $description); | ||
} | ||
|
||
$command->addOption('all', description: 'Install all the package components'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Asciito\LaravelPackage\Package\Contracts; | ||
|
||
use Closure; | ||
use Illuminate\Console\Command; | ||
|
||
interface WithInstallCommand | ||
{ | ||
/** | ||
* Check if the installation command is available | ||
*/ | ||
public function hasInstallCommand(): bool; | ||
|
||
/** | ||
* Register and configure the installation command | ||
*/ | ||
public function withInstallCommand(string $signature = '', ?Closure $after = null): static; | ||
|
||
/** | ||
* The installation command itself | ||
* | ||
* @param Command $command The instance of the command to be run as the installation command | ||
*/ | ||
public function command(Command $command): int; | ||
|
||
/** | ||
* Return the command signature | ||
*/ | ||
public function getInstallCommandSignature(): string; | ||
|
||
/** | ||
* Pre-configure the command with a fresh instance of the command | ||
*/ | ||
public function preConfigureInstallCommand(Command $command): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
|
||
use Asciito\LaravelPackage\Package\Package; | ||
use Laravel\Prompts\Prompt; | ||
use function Pest\Laravel\artisan; | ||
|
||
trait PackageInstallCommandTest { | ||
protected function configurePackage(Package $package): void | ||
{ | ||
$package | ||
->setName('install-package') | ||
->withConfig() | ||
->withMigration() | ||
->withInstallCommand( | ||
'package:install', | ||
function ($command) { | ||
$command->info('This is a message'); | ||
} | ||
); | ||
} | ||
} | ||
|
||
uses(PackageInstallCommandTest::class); | ||
|
||
it('install command is registered', function () { | ||
Prompt::fake(); | ||
|
||
artisan('list') | ||
->expectsOutputToContain('package:install') | ||
->assertSuccessful(); | ||
|
||
|
||
artisan('package:install') | ||
->expectsOutputToContain('Installing Package Components') | ||
->expectsOutput('This is a message') | ||
->expectsOutputToContain('None package components were installed') | ||
->assertSuccessful(); | ||
}); | ||
|
||
it('install config', function () { | ||
artisan('package:install --config') | ||
->expectsOutputToContain('Publishing Component [Config] files...') | ||
->doesntExpectOutputToContain('Publishing Component [Migration] files') | ||
->doesntExpectOutputToContain('None package components were installed') | ||
->expectsOutputToContain('Package Component(s) installed') | ||
->assertSuccessful(); | ||
|
||
expect(config_path('one.php')) | ||
->toBeFile() | ||
->and(config_path('two.php')) | ||
->toBeFile(); | ||
}); | ||
|
||
it('install migrations', function () { | ||
artisan('package:install --migrations') | ||
->expectsOutputToContain('Publishing Component [Migrations] files...') | ||
->doesntExpectOutputToContain('Publishing Component [Config] files') | ||
->doesntExpectOutputToContain('None package components were installed') | ||
->expectsOutputToContain('Package Component(s) installed') | ||
->assertSuccessful(); | ||
|
||
expect(database_path('migrations/create_package_test_one_table.php')) | ||
->toBeFile() | ||
->and(database_path('migrations/create_package_test_two_table.php')) | ||
->toBeFile(); | ||
}); | ||
|
||
it('install all the components', function () { | ||
artisan('package:install --all') | ||
->expectsOutputToContain('Publishing Component [Migrations] files...') | ||
->expectsOutputToContain('Publishing Component [Config] files') | ||
->expectsOutputToContain('Package Component(s) installed') | ||
->doesntExpectOutputToContain('None package components were installed') | ||
->assertSuccessful(); | ||
}); |