diff --git a/README.md b/README.md index 165658c..199de8d 100644 --- a/README.md +++ b/README.md @@ -324,6 +324,26 @@ php artisan vendor:publish --tag=your-package-name-assets This will copy over the assets to the `public/vendor/` directory in the app where your package is installed in. +### Working with stubs + +Any assets your package provides, should be placed in the `/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 @@ -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() diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 1453bdb..6a00fe7 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -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'); diff --git a/src/Package.php b/src/Package.php index 940c144..5a26646 100644 --- a/src/Package.php +++ b/src/Package.php @@ -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 = []; @@ -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; diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 3d2cbb1..f9d94c3 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -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)) { diff --git a/tests/PackageServiceProviderTests/InstallCommandTests/StubsTest.php b/tests/PackageServiceProviderTests/InstallCommandTests/StubsTest.php new file mode 100644 index 0000000..d4c3497 --- /dev/null +++ b/tests/PackageServiceProviderTests/InstallCommandTests/StubsTest.php @@ -0,0 +1,31 @@ +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')); +}); diff --git a/tests/PackageServiceProviderTests/InstallCommandTests/StubsWithoutWrapperTest.php b/tests/PackageServiceProviderTests/InstallCommandTests/StubsWithoutWrapperTest.php new file mode 100644 index 0000000..bc3efe1 --- /dev/null +++ b/tests/PackageServiceProviderTests/InstallCommandTests/StubsWithoutWrapperTest.php @@ -0,0 +1,31 @@ +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')); +}); diff --git a/tests/PackageServiceProviderTests/PackageStubsTest.php b/tests/PackageServiceProviderTests/PackageStubsTest.php new file mode 100644 index 0000000..5e34f6f --- /dev/null +++ b/tests/PackageServiceProviderTests/PackageStubsTest.php @@ -0,0 +1,25 @@ +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')); +}); diff --git a/tests/TestPackage/stubs/dummy.stub b/tests/TestPackage/stubs/dummy.stub new file mode 100644 index 0000000..240e34b --- /dev/null +++ b/tests/TestPackage/stubs/dummy.stub @@ -0,0 +1,6 @@ +