Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Mar 10, 2021
2 parents 13f3b0f + f03683d commit 4b240c7
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 25 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

All notable changes to `laravel-package-tools` will be documented in this file.

## 1.4.3 - 2021-03-10

- use package shortname for publishing

## 1.4.2 - 2021-03-05

- fix publishing views (#15)

## 1.4.1 - 2021-03-04

- ensure unique timestamp on migration publish (#14)

## 1.4.0 - 2021-02-15

- allows parameters for setup methods to be passed in as a spread array (#11)

## 1.3.1 - 2021-02-02

- fix `migrationFileExists` (#7)

## 1.3.0 - 2021-01-28

- add `hasRoute`
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ This will register your views with Laravel.
If you have a view `<package root>/resources/views/myView.blade.php`, you can use it like this: `view('your-package-name::myView')`. Of course, you can also use subdirectories to organise your views. A view located at `<package root>/resources/views/subdirectory/myOtherView.blade.php` can be used with `view('your-package-name::subdirectory.myOtherView')`.


Calling `hasViews` will also make views publishable. Users of your package will be able to publish the config file with this command:
Calling `hasViews` will also make views publishable. Users of your package will be able to publish the views with this command:

```bash
php artisan vendor:publish --tag=your-package-name-views
Expand Down Expand Up @@ -142,7 +142,7 @@ trans('your-package-name::translations.translatable'); // returns 'translation'
If your package name starts with `laravel-` then you should leave that off in the example above.


Calling `hasTranslations` will also make translations publishable. Users of your package will be able to publish the config file with this command:
Calling `hasTranslations` will also make translations publishable. Users of your package will be able to publish the translations with this command:

```bash
php artisan vendor:publish --tag=your-package-name-translations
Expand All @@ -160,7 +160,7 @@ $package
->hasAssets();
```

Users of your package will be able to publish the config file with this command:
Users of your package will be able to publish the assets with this command:

```bash
php artisan vendor:publish --tag=your-package-name-assets
Expand Down Expand Up @@ -190,7 +190,7 @@ $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 config file with this command:
Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the migrations with this command:

```bash
php artisan vendor:publish --tag=your-package-name-migrations
Expand Down
25 changes: 14 additions & 11 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function hasConfigFile(string $configFileName = null): self
return $this;
}

public function shortName(): string
{
return Str::after($this->name, 'laravel-');
}

public function hasViews(): self
{
$this->hasViews = true;
Expand Down Expand Up @@ -66,9 +71,12 @@ public function hasMigration(string $migrationFileName): self
return $this;
}

public function hasMigrations(array $migrationFileNames): self
public function hasMigrations(...$migrationFileNames): self
{
$this->migrationFileNames = array_merge($this->migrationFileNames, $migrationFileNames);
$this->migrationFileNames = array_merge(
$this->migrationFileNames,
collect($migrationFileNames)->flatten()->toArray()
);

return $this;
}
Expand All @@ -80,9 +88,9 @@ public function hasCommand(string $commandClassName): self
return $this;
}

public function hasCommands(array $commandClassNames): self
public function hasCommands(...$commandClassNames): self
{
$this->commands = array_merge($this->commands, $commandClassNames);
$this->commands = array_merge($this->commands, collect($commandClassNames)->flatten()->toArray());

return $this;
}
Expand All @@ -94,9 +102,9 @@ public function hasRoute(string $routeFileName): self
return $this;
}

public function hasRoutes(array $routeFileNames): self
public function hasRoutes(...$routeFileNames): self
{
$this->routeFileNames = array_merge($this->routeFileNames, $routeFileNames);
$this->routeFileNames = array_merge($this->routeFileNames, collect($routeFileNames)->flatten()->toArray());

return $this;
}
Expand All @@ -116,9 +124,4 @@ public function setBasePath(string $path): self

return $this;
}

public function shortName(): string
{
return Str::after($this->name, 'laravel-');
}
}
9 changes: 5 additions & 4 deletions src/PackageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ public function boot()

if ($this->package->hasViews) {
$this->publishes([
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->package->name}"),
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->package->shortName()}"),
], "{$this->package->shortName()}-views");
}

$now = now();
foreach ($this->package->migrationFileNames as $migrationFileName) {
if (! $this->migrationFileExists($migrationFileName)) {
$this->publishes([
$this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => database_path('migrations/' . now()->format('Y_m_d_His') . '_' . Str::finish($migrationFileName, '.php')),
$this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => database_path('migrations/' . $now->addSecond()->format('Y_m_d_His') . '_' . Str::finish($migrationFileName, '.php')),
], "{$this->package->shortName()}-migrations");
}
}
Expand Down Expand Up @@ -98,10 +99,10 @@ public function boot()

public static function migrationFileExists(string $migrationFileName): bool
{
$len = strlen($migrationFileName);
$len = strlen($migrationFileName) + 4;

foreach (glob(database_path("migrations/*.php")) as $filename) {
if ((substr($filename, -$len) === $migrationFileName)) {
if ((substr($filename, -$len) === $migrationFileName . '.php')) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;

use Spatie\LaravelPackageTools\Package;
use Spatie\TestTime\TestTime;

class MultiplePackageMigrationsTest 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')
->hasMigrations(['create_laravel_package_tools_table'])
->hasMigrations('create_other_laravel_package_tools_table', 'create_third_laravel_package_tools_table');
}

/** @test */
public function it_can_publish_the_migration()
{
$this
->artisan('vendor:publish --tag=package-tools-migrations')
->assertExitCode(0);

$this->assertFileExists(database_path('migrations/2020_01_01_000000_create_laravel_package_tools_table.php'));
$this->assertFileExists(database_path('migrations/2020_01_01_000001_create_other_laravel_package_tools_table.php'));
$this->assertFileExists(database_path('migrations/2020_01_01_000002_create_third_laravel_package_tools_table.php'));
}
}
5 changes: 4 additions & 1 deletion tests/PackageServiceProviderTests/PackageCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\Tests\TestClasses\FourthTestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\OtherTestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\TestCommand;
use Spatie\LaravelPackageTools\Tests\TestClasses\ThirdTestCommand;

class PackageCommandsTest extends PackageServiceProviderTestCase
{
Expand All @@ -13,7 +15,8 @@ public function configurePackage(Package $package)
$package
->name('laravel-package-tools')
->hasCommand(TestCommand::class)
->hasCommands([OtherTestCommand::class]);
->hasCommands([OtherTestCommand::class])
->hasCommands(ThirdTestCommand::class, FourthTestCommand::class);
}

/** @test */
Expand Down
6 changes: 3 additions & 3 deletions tests/PackageServiceProviderTests/PackageMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public function configurePackage(Package $package)

$package
->name('laravel-package-tools')
->hasMigration('create_laravel_package_tools_table');
->hasMigration('create_another_laravel_package_tools_table');
}

/** @test */
public function it_can_publish_the_migration()
{
$this
->artisan('vendor:publish --tag=laravel-package-tools-migrations')
->artisan('vendor:publish --tag=package-tools-migrations')
->assertExitCode(0);

$this->assertFileExists(database_path('migrations/2020_01_01_000000_create_laravel_package_tools_table.php'));
$this->assertFileExists(database_path('migrations/2020_01_01_000001_create_another_laravel_package_tools_table.php'));
}
}
34 changes: 34 additions & 0 deletions tests/PackageServiceProviderTests/PackageRoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\PackageServiceProviderTests;

use Spatie\LaravelPackageTools\Package;
use Spatie\TestTime\TestTime;

class PackageRoutesTest 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')
->hasRoutes('web', 'other');
}

/** @test */
public function it_can_load_the_route()
{
$response = $this->get('my-route');

$response->assertSeeText('my response');
}

/** @test */
public function it_can_load_multiple_route()
{
$adminResponse = $this->get('other-route');

$adminResponse->assertSeeText('other response');
}
}
4 changes: 2 additions & 2 deletions tests/PackageServiceProviderTests/PackageViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function it_can_load_the_views()
public function it_can_publish_the_views()
{
$this
->artisan('vendor:publish --tag=laravel-package-tools-views')
->artisan('vendor:publish --tag=package-tools-views')
->assertExitCode(0);

$this->assertFileExists(base_path('resources/views/vendor/laravel-package-tools/test.blade.php'));
$this->assertFileExists(base_path('resources/views/vendor/package-tools/test.blade.php'));
}
}
15 changes: 15 additions & 0 deletions tests/TestClasses/FourthTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\TestClasses;

use Illuminate\Console\Command;

class FourthTestCommand extends Command
{
public $name = 'fourth-test-command';

public function handle()
{
$this->info('output of test command');
}
}
15 changes: 15 additions & 0 deletions tests/TestClasses/ThirdTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\LaravelPackageTools\Tests\TestClasses;

use Illuminate\Console\Command;

class ThirdTestCommand extends Command
{
public $name = 'third-test-command';

public function handle()
{
$this->info('output of test command');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAnotherLaravelPackageToolsTable extends Migration
{
public function up()
{
Schema::create('laravel-package-tools_another-table', function (Blueprint $table) {
$table->bigIncrements('id');

$table->timestamps();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateLaravelPackageToolsTable extends Migration
{
public function up()
{
Schema::create('other-laravel-package-tools_table', function (Blueprint $table) {
$table->bigIncrements('id');

$table->timestamps();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateLaravelPackageToolsTable extends Migration
{
public function up()
{
Schema::create('third-laravel-package-tools_table', function (Blueprint $table) {
$table->bigIncrements('id');

$table->timestamps();
});
}
}

0 comments on commit 4b240c7

Please sign in to comment.