From 6f30d85b1c51917881b3dab21159c792fa59f029 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Wed, 18 Dec 2024 16:18:55 +0000 Subject: [PATCH 1/3] feature: discover migrations --- README.md | 10 ++- src/Package.php | 15 ++++ src/PackageServiceProvider.php | 73 ++++++++++++++----- .../PackageDiscoversMigrationsTest.php | 65 +++++++++++++++++ 4 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 tests/PackageServiceProviderTests/PackageDiscoversMigrationsTest.php diff --git a/README.md b/README.md index 165658c..ef6cd74 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,15 @@ $package ->hasMigrations(['my_package_tables', 'some_other_migration']); ``` -Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the +Alternatively, if you wish to publish all migrations in your package by default, you may call `discoversMigrations`. + +```php +$package + ->name('your-package-name') + ->discoversMigrations(); +``` + +Calling either `hasMigration`, `hasMigration` or `discoversMigrations` will also make migrations publishable. Users of your package will be able to publish the migrations with this command: ```bash diff --git a/src/Package.php b/src/Package.php index 11258be..2e05b95 100644 --- a/src/Package.php +++ b/src/Package.php @@ -21,6 +21,8 @@ class Package public bool $hasAssets = false; + public bool $discoversMigrations = false; + public bool $runsMigrations = false; public array $migrationFileNames = []; @@ -152,6 +154,13 @@ public function hasAssets(): static return $this; } + public function discoversMigrations(bool $discoversMigrations = true): static + { + $this->discoversMigrations = $discoversMigrations; + + return $this; + } + public function runsMigrations(bool $runsMigrations = true): static { $this->runsMigrations = $runsMigrations; @@ -168,6 +177,12 @@ public function hasMigration(string $migrationFileName): static public function hasMigrations(...$migrationFileNames): static { + if (empty($migrationFileNames)) { + $this->discoversMigrations(); + + return $this; + } + $this->migrationFileNames = array_merge( $this->migrationFileNames, collect($migrationFileNames)->flatten()->toArray() diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index 4a737c0..a2af6d7 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -3,6 +3,8 @@ namespace Spatie\LaravelPackageTools; use Carbon\Carbon; +use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; @@ -76,24 +78,7 @@ public function boot() ], "{$this->packageView($this->package->viewNamespace)}-inertia-components"); } - $now = Carbon::now(); - foreach ($this->package->migrationFileNames as $migrationFileName) { - $filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php"); - if (! file_exists($filePath)) { - // Support for the .stub file extension - $filePath .= '.stub'; - } - - $this->publishes([ - $filePath => $this->generateMigrationName( - $migrationFileName, - $now->addSecond() - ), ], "{$this->package->shortName()}-migrations"); - - if ($this->package->runsMigrations) { - $this->loadMigrationsFrom($filePath); - } - } + $this->bootPackageMigrations(); if ($this->package->hasTranslations) { $this->publishes([ @@ -213,4 +198,56 @@ public function packageView(?string $namespace) { return is_null($namespace) ? $this->package->shortName() : $this->package->viewNamespace; } + + protected function bootPackageMigrations(): void + { + $now = Carbon::now(); + + if ($this->package->discoversMigrations) { + $this->discoverMigrations(); + + return; + } + + foreach ($this->package->migrationFileNames as $migrationFileName) { + $filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php"); + if (! file_exists($filePath)) { + // Support for the .stub file extension + $filePath .= '.stub'; + } + + $this->publishes([ + $filePath => $this->generateMigrationName( + $migrationFileName, + $now->addSecond() + ), ], "{$this->package->shortName()}-migrations"); + + if ($this->package->runsMigrations) { + $this->loadMigrationsFrom($filePath); + } + } + } + + protected function discoverMigrations(): void + { + $now = Carbon::now(); + + $files = (new Filesystem())->files($this->package->basePath('/../database/migrations')); + + foreach ($files as $file) { + $filePath = $file->getPathname(); + + $migrationFileName = Str::replace(['.stub', '.php'], '', $file->getFilename()); + + $this->publishes([ + $filePath => $this->generateMigrationName( + $migrationFileName, + $now->addSecond() + ), ], "{$this->package->shortName()}-migrations"); + + if ($this->package->runsMigrations) { + $this->loadMigrationsFrom($filePath); + } + } + } } diff --git a/tests/PackageServiceProviderTests/PackageDiscoversMigrationsTest.php b/tests/PackageServiceProviderTests/PackageDiscoversMigrationsTest.php new file mode 100644 index 0000000..bf554ed --- /dev/null +++ b/tests/PackageServiceProviderTests/PackageDiscoversMigrationsTest.php @@ -0,0 +1,65 @@ +freeze('2020-01-01 00:00:00'); + + $package + ->name('laravel-package-tools') + ->discoversMigrations() + ->runsMigrations(); + } +} + +uses(ConfigurePackageDiscoverMigrationsTest::class); + +it('publishes discovered migrations', function () { + $this + ->artisan('vendor:publish --tag=package-tools-migrations') + ->doesntExpectOutput('hey') + ->assertExitCode(0); + + assertMigrationPublished('create_another_laravel_package_tools_table.php'); +}); + +it('can publish the migration without being stubbed', function () { + $this + ->artisan('vendor:publish --tag=package-tools-migrations') + ->assertExitCode(0); + + assertMigrationPublished('create_regular_laravel_package_tools_table.php'); +}); + +it('does not overwrite the existing migration', function () { + $this + ->artisan('vendor:publish --tag=package-tools-migrations') + ->assertExitCode(0); + + $filePath = database_path('migrations/2020_01_01_000001_create_another_laravel_package_tools_table.php'); + + assertMigrationPublished('create_another_laravel_package_tools_table.php'); + + + file_put_contents($filePath, 'modified'); + + $this + ->artisan('vendor:publish --tag=package-tools-migrations') + ->assertExitCode(0); + + $this->assertStringEqualsFile($filePath, 'modified'); +}); + +it('can run migrations which registers them', function () { + /** @var \Illuminate\Database\Migrations\Migrator $migrator */ + $migrator = app('migrator'); + + $this->assertCount(5, $migrator->paths()); + $this->assertStringContainsString('laravel_package_tools', $migrator->paths()[0]); +}); From ef00afaecffb400a2688ee0ceb1a1a875bf97c60 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Tue, 24 Dec 2024 09:19:26 +0000 Subject: [PATCH 2/3] update: define migration discovery path --- README.md | 9 ++++++ src/Package.php | 11 +++---- src/PackageServiceProvider.php | 6 ++-- .../MultiplePackageMigrationsTest.php | 32 ------------------- 4 files changed, 17 insertions(+), 41 deletions(-) delete mode 100644 tests/PackageServiceProviderTests/MultiplePackageMigrationsTest.php diff --git a/README.md b/README.md index ef6cd74..ecb6ee2 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,15 @@ $package ->discoversMigrations(); ``` +Calling this method will look for migrations in the `./database/migrations` directory of your project. However, if you have defined your migrations +in a different folder, you may pass a value to the `$path` variable to instruct the app to discover migrations from that location. + +```php +$package + ->name('your-package-name') + ->discoversMigrations(path: '/path/to/your/migrations/folder'); +``` + Calling either `hasMigration`, `hasMigration` or `discoversMigrations` will also make migrations publishable. Users of your package will be able to publish the migrations with this command: diff --git a/src/Package.php b/src/Package.php index 2e05b95..50d8f88 100644 --- a/src/Package.php +++ b/src/Package.php @@ -23,6 +23,8 @@ class Package public bool $discoversMigrations = false; + public ?string $migrationsPath = null; + public bool $runsMigrations = false; public array $migrationFileNames = []; @@ -154,9 +156,10 @@ public function hasAssets(): static return $this; } - public function discoversMigrations(bool $discoversMigrations = true): static + public function discoversMigrations(bool $discoversMigrations = true, string $path = '/database/migrations'): static { $this->discoversMigrations = $discoversMigrations; + $this->migrationsPath = $path; return $this; } @@ -177,12 +180,6 @@ public function hasMigration(string $migrationFileName): static public function hasMigrations(...$migrationFileNames): static { - if (empty($migrationFileNames)) { - $this->discoversMigrations(); - - return $this; - } - $this->migrationFileNames = array_merge( $this->migrationFileNames, collect($migrationFileNames)->flatten()->toArray() diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index a2af6d7..e8cb5e5 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -203,14 +203,16 @@ protected function bootPackageMigrations(): void { $now = Carbon::now(); - if ($this->package->discoversMigrations) { + if ($this->package->discoversMigrations && $this->package->migrationsPath) { $this->discoverMigrations(); return; } + $path = trim($this->package->migrationsPath, '/'); + foreach ($this->package->migrationFileNames as $migrationFileName) { - $filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php"); + $filePath = $this->package->basePath("/../${$path}/${$migrationFileName}.php"); if (! file_exists($filePath)) { // Support for the .stub file extension $filePath .= '.stub'; diff --git a/tests/PackageServiceProviderTests/MultiplePackageMigrationsTest.php b/tests/PackageServiceProviderTests/MultiplePackageMigrationsTest.php deleted file mode 100644 index ca9f69f..0000000 --- a/tests/PackageServiceProviderTests/MultiplePackageMigrationsTest.php +++ /dev/null @@ -1,32 +0,0 @@ -freeze('2020-01-01 00:00:00'); - - $package - ->name('laravel-package-tools') - ->hasMigrations(['create_laravel_package_tools_table']) - ->hasMigrations('create_other_laravel_package_tools_table', 'create_third_laravel_package_tools_table') - ->hasMigration('folder/create_laravel_package_tools_table_in_the_folder'); - } -} - -uses(ConfigureMultiplePackageMigrationsTest::class); - -it('can publish multiple migrations', function () { - $this - ->artisan('vendor:publish --tag=package-tools-migrations') - ->assertExitCode(0); - - assertFileExists(database_path('migrations/2020_01_01_000001_create_laravel_package_tools_table.php')); - assertFileExists(database_path('migrations/2020_01_01_000002_create_other_laravel_package_tools_table.php')); - assertFileExists(database_path('migrations/2020_01_01_000003_create_third_laravel_package_tools_table.php')); - assertFileExists(database_path('migrations/folder/2020_01_01_000004_create_laravel_package_tools_table_in_the_folder.php')); -}); From ab2bcacbdc1446e800d3cdd130d083864ea9e571 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Tue, 24 Dec 2024 12:40:02 +0000 Subject: [PATCH 3/3] fix: bugs --- src/PackageServiceProvider.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php index e8cb5e5..96a2571 100644 --- a/src/PackageServiceProvider.php +++ b/src/PackageServiceProvider.php @@ -201,18 +201,15 @@ public function packageView(?string $namespace) protected function bootPackageMigrations(): void { - $now = Carbon::now(); - - if ($this->package->discoversMigrations && $this->package->migrationsPath) { + if ($this->package->discoversMigrations) { $this->discoverMigrations(); return; } - $path = trim($this->package->migrationsPath, '/'); - + $now = Carbon::now(); foreach ($this->package->migrationFileNames as $migrationFileName) { - $filePath = $this->package->basePath("/../${$path}/${$migrationFileName}.php"); + $filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php"); if (! file_exists($filePath)) { // Support for the .stub file extension $filePath .= '.stub'; @@ -233,8 +230,8 @@ protected function bootPackageMigrations(): void protected function discoverMigrations(): void { $now = Carbon::now(); - - $files = (new Filesystem())->files($this->package->basePath('/../database/migrations')); + $path = trim($this->package->migrationsPath, '/'); + $files = (new Filesystem())->files($this->package->basePath("/../{$path}")); foreach ($files as $file) { $filePath = $file->getPathname();