Skip to content

Commit

Permalink
Support stub wildcard matching and update readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 10, 2024
1 parent 40a6fd2 commit 55be808
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 12 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ composer require lorisleiva/laravel-actions
```
The default DTO and Action stubs of this package reference classes from these packages. If this doesn't apply to your application, you may customize the stubs accordingly.

### Customizing Stubs
To publish one or more stubs for customization, you may use the `ddd:stub` command:
```bash
# Publish one or more stubs interactively via prompts
php artisan ddd:stub

# Publish all stubs
php artisan ddd:stub --all

# Publish and overwrite only the files that have already been published
php artisan ddd:stub --all --existing

# Overwrite any existing files
php artisan ddd:stub --all --force

# Publish one or more stubs specified as arguments
php artisan ddd:stub model
php artisan ddd:stub model dto action
php artisan ddd:stub controller controller.plain controller.api
```
To publish multiple related stubs at once, use `*` or `.` as a wildcard ending.
```bash
php artisan ddd:stub listener.
```
Output:
```bash
Publishing /stubs/ddd/listener.typed.queued.stub
Publishing /stubs/ddd/listener.queued.stub
Publishing /stubs/ddd/listener.typed.stub
Publishing /stubs/ddd/listener.stub
```
For a quick reference of available stubs, use the `--list` option:
```bash
php artisan ddd:stub --list
```

### Deployment
In production, run `ddd:optimize` during the deployment process to [optimize autoloading](#autoloading-in-production).
```bash
Expand Down
6 changes: 1 addition & 5 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ public function handle(): int
$this->registerDomainAutoload();

if ($this->confirm('Would you like to publish stubs?')) {
$this->comment('Publishing stubs...');

$this->callSilently('vendor:publish', [
'--tag' => 'ddd-stubs',
]);
$this->call('ddd:stub');
}

return self::SUCCESS;
Expand Down
51 changes: 47 additions & 4 deletions src/Commands/StubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
use Illuminate\Foundation\Events\PublishingStubs;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\Support\Path;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

use function Laravel\Prompts\multisearch;
use function Laravel\Prompts\select;
use function Laravel\Prompts\table;

class StubCommand extends Command
{
Expand All @@ -31,6 +34,7 @@ protected function getOptions()
['all', 'a', InputOption::VALUE_NONE, 'Publish all stubs'],
['existing', null, InputOption::VALUE_NONE, 'Publish and overwrite only the files that have already been published'],
['force', null, InputOption::VALUE_NONE, 'Overwrite any existing files'],
['list', 'l', InputOption::VALUE_NONE, 'List all available stubs'],
];
}

Expand All @@ -47,11 +51,23 @@ protected function resolveSelectedStubs(array $names = [])
$stubs = $this->getStubChoices();

if ($names) {
[$startsWith, $exactNames] = collect($names)
->partition(fn ($name) => str($name)->endsWith(['*', '.']));

$startsWith = $startsWith->map(
fn ($name) => str($name)
->replaceEnd('*', '.')
->replaceEnd('.', '')
);

return collect($stubs)
->filter(
fn ($stub, $path) => in_array($stub, $names)
|| in_array(str($stub)->replaceEnd('.stub', '')->toString(), $names)
)
->filter(function ($stub, $path) use ($startsWith, $exactNames) {
$stubWithoutExtension = str($stub)->replaceEnd('.stub', '');

return $exactNames->contains($stub)
|| $exactNames->contains($stubWithoutExtension)
|| str($stub)->startsWith($startsWith);
})
->all();
}

Expand All @@ -72,6 +88,7 @@ protected function resolveSelectedStubs(array $names = [])
public function handle(): int
{
$option = match (true) {
$this->option('list') => 'list',
$this->option('all') => 'all',
count($this->argument('name')) > 0 => 'named',
default => select(
Expand All @@ -85,6 +102,32 @@ public function handle(): int
)
};

if ($option === 'list') {
// $this->table(
// ['Stub', 'Path'],
// collect($this->getStubChoices())->map(
// fn($stub, $path) => [
// $stub,
// Str::after($path, $this->laravel->basePath())
// ]
// )
// );

table(
headers: ['Stub', 'Source'],
rows: collect($this->getStubChoices())->map(
fn ($stub, $path) => [
Str::replaceLast('.stub', '', $stub),
str($path)->startsWith(DDD::packagePath())
? 'ddd'
: 'laravel',
]
)
);

return self::SUCCESS;
}

$stubs = $option === 'all'
? $this->getStubChoices()
: $this->resolveSelectedStubs($this->argument('name'));
Expand Down
2 changes: 1 addition & 1 deletion src/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getCommandContext(): ?DomainCommandContext

public function packagePath($path = ''): string
{
return Path::normalize(__DIR__.'/../'.$path);
return Path::normalize(realpath(__DIR__.'/../'.$path));
}

public function laravelVersion($value)
Expand Down
4 changes: 2 additions & 2 deletions src/StubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function frameworkStubs()
$dir.'/stubs/resource-collection.stub' => 'resource-collection.stub',
$dir.'/stubs/rule.stub' => 'rule.stub',
$dir.'/stubs/scope.stub' => 'scope.stub',
$dir.'/stubs/test.stub' => 'test.stub',
$dir.'/stubs/test.unit.stub' => 'test.unit.stub',
// $dir.'/stubs/test.stub' => 'test.stub',
// $dir.'/stubs/test.unit.stub' => 'test.unit.stub',
$dir.'/stubs/trait.stub' => 'trait.stub',
$dir.'/stubs/view-component.stub' => 'view-component.stub',
// Factories will use a ddd-specific stub
Expand Down
28 changes: 28 additions & 0 deletions tests/Command/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@
'controller' => [['controller']],
]);

it('can publish stubs using wildcard', function ($argument, $stubsToPublish) {
$expectedStubFilenames = collect($stubsToPublish)
->map(fn ($stub) => $stub.'.stub')
->all();

$this
->artisan("ddd:stub {$argument}")
->assertSuccessful()
->execute();

$publishedStubFolder = app()->basePath('stubs/ddd');

assertDirectoryExists($publishedStubFolder);

$stubFiles = File::files($publishedStubFolder);

expect(count($stubFiles))->toEqual(count($stubsToPublish));

foreach ($stubFiles as $file) {
expect($file->getFilename())->toBeIn($expectedStubFilenames);
}
})->with([
'model*' => ['model*', ['model', 'model.pivot']],
'model.' => ['model*', ['model', 'model.pivot']],
'listener*' => ['listener*', ['listener', 'listener.typed', 'listener.queued', 'listener.typed.queued']],
'listener.' => ['listener*', ['listener', 'listener.typed', 'listener.queued', 'listener.typed.queued']],
]);

it('can publish specific stubs interactively', function () {
$publishedStubFolder = app()->basePath('stubs/ddd');

Expand Down

0 comments on commit 55be808

Please sign in to comment.