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

add stubs features #119

Closed
Closed
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,26 @@ php artisan vendor:publish --tag=your-package-name-assets
This will copy over the assets to the `public/vendor/<your-package-name>` directory in the app where your package is
installed in.

### Working with stubs

Any assets your package provides, should be placed in the `<package root>/stubs/` directory.

You can make these stubs publishable using the `hasStubs` method.

```php
$package
->name('your-package-name')
->hasStubs();
```

Users of your package will be able to publish the stubs with this command:

```bash
php artisan vendor:publish --tag=your-package-name-stubs
```

Also, the stubs are available in a convenient way with your package [installer-command](#adding-an-installer-command)

### Working with migrations

The `PackageServiceProvider` assumes that any migrations are placed in this
Expand Down Expand Up @@ -433,11 +453,13 @@ class YourPackageServiceProvider extends PackageServiceProvider
$package
->name('your-package-name')
->hasConfigFile()
->hasStubs()
->hasMigration('create_package_tables')
->publishesServiceProvider('MyServiceProviderName')
->hasInstallCommand(function(InstallCommand $command) {
$command
->publishConfigFile()
->publishStubs()
->publishAssets()
->publishMigrations()
->askToRunMigrations()
Expand Down
5 changes: 5 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public function publishAssets(): self
return $this->publish('assets');
}

public function publishStubs(): self
{
return $this->publish('stubs');
}

public function publishInertiaComponents(): self
{
return $this->publish('inertia-components');
Expand Down
13 changes: 13 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Package

public bool $hasAssets = false;

public bool $hasStubs = false;

public bool $stubsWrapper = false;

public bool $runsMigrations = false;

public array $migrationFileNames = [];
Expand Down Expand Up @@ -152,6 +156,15 @@ public function hasAssets(): static
return $this;
}

public function hasStubs($stubsWrapper = true): static
{
$this->hasStubs = true;

$this->stubsWrapper = $stubsWrapper;

return $this;
}

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

if ($this->package->hasStubs) {

$destination = $this->package->stubsWrapper ? "stubs/{$this->package->shortName()}" : 'stubs';

$this->publishes([
$this->package->basePath('/../stubs') => base_path($destination),
], "{$this->package->shortName()}-stubs");
}
}

if (! empty($this->package->commands)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use function PHPUnit\Framework\assertFileExists;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use function Spatie\PestPluginTestTime\testTime;

trait ConfigureStubsFileTest
{
public function configurePackage(Package $package)
{
testTime()->freeze('2020-01-01 00:00:00');

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

uses(ConfigureStubsFileTest::class);

it('can install the stub files', function () {
$this
->artisan('package-tools:install')
->assertSuccessful();

assertFileExists(base_path('stubs/package-tools/dummy.stub'));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use function PHPUnit\Framework\assertFileExists;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use function Spatie\PestPluginTestTime\testTime;

trait ConfigureStubsWithoutPackageNameWrapperFileTest
{
public function configurePackage(Package $package)
{
testTime()->freeze('2020-01-01 00:00:00');

$package
->name('laravel-package-tools')
->hasStubs(false)
->hasInstallCommand(function (InstallCommand $command) {
$command->publishStubs();
});
}
}

uses(ConfigureStubsWithoutPackageNameWrapperFileTest::class);

it('can install the stub files without package name wrapper', function () {
$this
->artisan('package-tools:install')
->assertSuccessful();

assertFileExists(base_path('stubs/dummy.stub'));
});
25 changes: 25 additions & 0 deletions tests/PackageServiceProviderTests/PackageStubsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertFileExists;
use Spatie\LaravelPackageTools\Package;

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

uses(ConfigurePackageStubsTest::class);

it('can publish the stub files', function () {
$this
->artisan('vendor:publish --tag=package-tools-stubs')
->assertExitCode(0);

assertFileExists(base_path('stubs/package-tools/dummy.stub'));
});
6 changes: 6 additions & 0 deletions tests/TestPackage/stubs/dummy.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class DummyClass
{

}
Loading