Skip to content

Commit

Permalink
Merge branch 'add-install-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Sep 7, 2022
2 parents ec64288 + 734a42b commit a0964b8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ $package
```

The file that will be copied to the app should be stored in your package
in `/resources/stubs/{$nameOfYourServiceProvider}.php`.
in `/resources/stubs/{$nameOfYourServiceProvider}.php.stub`.

When your package is installed into an app, running this command...

Expand Down Expand Up @@ -381,7 +381,7 @@ Instead of letting your users manually publishing config files, migrations, and
add an install command that does all this work in one go. Packages like Laravel Horizon and Livewire provide such
commands.

When using Laravel Package Tools, you don't have to write an `InstallCommand` yourself. Instead you can simply
When using Laravel Package Tools, you don't have to write an `InstallCommand` yourself. Instead, you can simply
call, `hasInstallCommand` and configure it using a closure. Here's an example.

```php
Expand All @@ -402,6 +402,7 @@ class YourPackageServiceProvider extends PackageServiceProvider
$command
->publishConfigFile()
->publishMigrations()
->askToRunMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub('your-vendor/your-repo-name')
});
Expand All @@ -419,8 +420,9 @@ Using the code above, that command will:

- publish the config file
- publish the migrations
- copy the `MyProviderName.php` from your package to `app/Providers/MyServiceProviderName.php`, and also register that
- copy the `/resources/stubs/MyProviderName.php.stub` from your package to `app/Providers/MyServiceProviderName.php`, and also register that
provider in `config/app.php`
- ask if migrations should be run now
- prompt the user to open up `https://github.com/'your-vendor/your-repo-name'` in the browser in order to star it

You can also call `startWith` and `endWith` on the `InstallCommand`. They will respectively be executed at the start and
Expand All @@ -441,6 +443,7 @@ public function configurePackage(Package $package): void
})
->publishConfigFile()
->publishMigrations()
->askToRunMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub('your-vendor/your-repo-name')
->endWith(function(InstallCommand $command) {
Expand Down
17 changes: 17 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class InstallCommand extends Command

protected bool $shouldPublishMigrations = false;

protected bool $askToRunMigrations = false;

protected bool $copyServiceProviderInApp = false;

protected ?string $starRepo = null;
Expand Down Expand Up @@ -54,6 +56,14 @@ public function handle()
]);
}

if ($this->askToRunMigrations) {
if ($this->confirm('Would you like to run the migrations now?')) {
$this->info('Running migrations...');

$this->call('migrate');
}
}

if ($this->copyServiceProviderInApp) {
$this->copyServiceProviderInApp();
}
Expand Down Expand Up @@ -95,6 +105,13 @@ public function publishMigrations(): self
return $this;
}

public function askToRunMigrations(): self
{
$this->askToRunMigrations = true;

return $this;
}

public function copyAndRegisterServiceProviderInApp(): self
{
$this->copyServiceProviderInApp = true;
Expand Down
2 changes: 1 addition & 1 deletion src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function boot()

if ($this->package->publishableProviderName) {
$this->publishes([
$this->package->basePath("/../resources/stubs/{$this->package->publishableProviderName}.php") => base_path("app/Providers/{$this->package->publishableProviderName}.php"),
$this->package->basePath("/../resources/stubs/{$this->package->publishableProviderName}.php.stub") => base_path("app/Providers/{$this->package->publishableProviderName}.php"),
], "{$this->package->shortName()}-provider");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests\InstallCommandTests;

use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests\PackageServiceProviderTestCase;
use Spatie\TestTime\TestTime;

class AskToRunMigrationsTest extends PackageServiceProviderTestCase
{
public function configurePackage(Package $package)
{
TestTime::freeze('Y-m-d H:i:s', '2020-01-01 00:00:00');

$package
->name('laravel-package-tools')
->hasConfigFile()
->hasInstallCommand(function (InstallCommand $command) {
$command->askToRunMigrations();
});
}

/** @test */
public function it_can_ask_to_run_the_migrations()
{
$this
->artisan('package-tools:install')
->assertSuccessful()
->expectsConfirmation('Would you like to run the migrations now?');
}
}

0 comments on commit a0964b8

Please sign in to comment.