From e52cb911cbe4d8f950a3ebfc65e938634dfdf0b2 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Wed, 4 Dec 2024 10:54:33 -0600 Subject: [PATCH 1/7] Support Single and Multi-Selection for Publishing Stubs --- .../Foundation/Console/StubPublishCommand.php | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index 1ffcf7158131..2e3dd3904888 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -7,6 +7,8 @@ use Illuminate\Foundation\Events\PublishingStubs; use Symfony\Component\Console\Attribute\AsCommand; +use function Laravel\Prompts\multisearch; + #[AsCommand(name: 'stub:publish')] class StubPublishCommand extends Command { @@ -17,7 +19,8 @@ class StubPublishCommand extends Command */ protected $signature = 'stub:publish {--existing : Publish and overwrite only the files that have already been published} - {--force : Overwrite any existing files}'; + {--force : Overwrite any existing files} + {--all : Publish all stubs that are available for customization}'; /** * The console command description. @@ -94,7 +97,7 @@ public function handle() realpath(__DIR__.'/../../Routing/Console/stubs/middleware.stub') => 'middleware.stub', ]; - $this->laravel['events']->dispatch($event = new PublishingStubs($stubs)); + $this->laravel['events']->dispatch($event = new PublishingStubs($this->determineStubsToPublish($stubs))); foreach ($event->stubs as $from => $to) { $to = $stubsPath.DIRECTORY_SEPARATOR.ltrim($to, DIRECTORY_SEPARATOR); @@ -107,4 +110,23 @@ public function handle() $this->components->info('Stubs published successfully.'); } + + /** + * Determine the stubs that should be published. + */ + protected function determineStubsToPublish(array $stubs): array + { + if ($this->option('all')) { + return $stubs; + } + + return collect($stubs)->only(multisearch( + 'Which stubs would you like to publish?', + options: fn($search) => collect($stubs) + ->filter(fn($to) => str_contains($to, $search)) + ->map(fn($to) => $to) + ->all(), + scroll: 10, + ))->all(); + } } From 33e0c01c357a3559cc618f48c65e4a9801417c14 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Wed, 4 Dec 2024 11:11:43 -0600 Subject: [PATCH 2/7] wip --- src/Illuminate/Foundation/Console/StubPublishCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index 2e3dd3904888..bf5d8df9910e 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -122,9 +122,9 @@ protected function determineStubsToPublish(array $stubs): array return collect($stubs)->only(multisearch( 'Which stubs would you like to publish?', - options: fn($search) => collect($stubs) - ->filter(fn($to) => str_contains($to, $search)) - ->map(fn($to) => $to) + options: fn ($search) => collect($stubs) + ->filter(fn ($to) => str_contains($to, $search)) + ->map(fn ($to) => $to) ->all(), scroll: 10, ))->all(); From 2b50600212dbcf8f33e85b0c0e751f9bcd6ad303 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Sun, 8 Dec 2024 22:09:31 -0600 Subject: [PATCH 3/7] Provide a human-readable name for a given stub file --- .../Foundation/Console/StubPublishCommand.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index bf5d8df9910e..3d8acb3c432c 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -2,6 +2,7 @@ namespace Illuminate\Foundation\Console; +use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Events\PublishingStubs; @@ -124,9 +125,31 @@ protected function determineStubsToPublish(array $stubs): array 'Which stubs would you like to publish?', options: fn ($search) => collect($stubs) ->filter(fn ($to) => str_contains($to, $search)) - ->map(fn ($to) => $to) + ->mapWithKeys(fn ($to, $from) => [$from => $this->forHumans($to)]) ->all(), scroll: 10, ))->all(); } + + /** + * Get a human-readable name for the given stub. + */ + private function forHumans(string $stub): string + { + $parts = Str::of($stub) + ->beforeLast('.stub') + ->replace('-', ' ') + ->explode('.'); + + if ($parts->count() <= 1) { + return Str::title($parts->first()); + } + + return $parts + ->splice(1, 1) + ->pipe(fn ($lead) => collect([ + Str::title($lead->first()), + $parts->map(fn ($part) => $part === 'api' ? 'API' : Str::title($part))->join(' '), + ])->filter()->join(' ')); + } } From 3d535b5aff45b1b0c89a967df32613e636551b24 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Sun, 8 Dec 2024 22:10:50 -0600 Subject: [PATCH 4/7] wip --- src/Illuminate/Foundation/Console/StubPublishCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index 3d8acb3c432c..e2951b4028e2 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -2,10 +2,10 @@ namespace Illuminate\Foundation\Console; -use Illuminate\Support\Str; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Events\PublishingStubs; +use Illuminate\Support\Str; use Symfony\Component\Console\Attribute\AsCommand; use function Laravel\Prompts\multisearch; @@ -134,7 +134,7 @@ protected function determineStubsToPublish(array $stubs): array /** * Get a human-readable name for the given stub. */ - private function forHumans(string $stub): string + private function forHumans(string $stub): string { $parts = Str::of($stub) ->beforeLast('.stub') From 52718ec9725197cb4d5341c0a7e22e9720816e50 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Mon, 9 Dec 2024 20:25:47 -0600 Subject: [PATCH 5/7] Display Common Stubs in Menu Selection --- .../Foundation/Console/StubPublishCommand.php | 56 ++++++++++++++----- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index e2951b4028e2..362c8ec4a23c 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -121,14 +121,37 @@ protected function determineStubsToPublish(array $stubs): array return $stubs; } - return collect($stubs)->only(multisearch( - 'Which stubs would you like to publish?', - options: fn ($search) => collect($stubs) - ->filter(fn ($to) => str_contains($to, $search)) - ->mapWithKeys(fn ($to, $from) => [$from => $this->forHumans($to)]) - ->all(), - scroll: 10, - ))->all(); + $commonStubs = collect($stubs) + ->filter(fn ($to, $from) => Str::contains($from, [ + 'model', + 'controller', + 'migration', + 'seeder', + 'factory', + 'test', + 'pest', + 'resource', + 'notification', + 'mail', + 'job', + ])) + ->all(); + + return collect($commonStubs) + ->only( + multisearch( + 'Which stubs would you like to publish?', + options: fn ($search) => collect($stubs) + ->filter(fn ($to) => str_contains($to, $search)) + ->mapWithKeys(fn ($to, $from) => [ + $from => $this->forHumans($to) + ]) + ->sort() + ->all(), + scroll: 10, + ) + ) + ->all(); } /** @@ -137,19 +160,24 @@ protected function determineStubsToPublish(array $stubs): array private function forHumans(string $stub): string { $parts = Str::of($stub) - ->beforeLast('.stub') + ->before('.stub') ->replace('-', ' ') - ->explode('.'); + ->explode('.') + ->map(fn ($part) => Str::of($part) + ->title() + ->replace('Api', 'API') + ); if ($parts->count() <= 1) { - return Str::title($parts->first()); + return $parts->first(); } - + + // Set the second segment as the prefix and join the remaining segments. return $parts ->splice(1, 1) ->pipe(fn ($lead) => collect([ - Str::title($lead->first()), - $parts->map(fn ($part) => $part === 'api' ? 'API' : Str::title($part))->join(' '), + $lead->first(), + $parts->join(' '), ])->filter()->join(' ')); } } From 7e85e38a89b53bbd77b7e6be736df937656777a2 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Mon, 9 Dec 2024 20:26:51 -0600 Subject: [PATCH 6/7] apply styleci rules --- .../Foundation/Console/StubPublishCommand.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index 362c8ec4a23c..f66e2e996b85 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -123,18 +123,18 @@ protected function determineStubsToPublish(array $stubs): array $commonStubs = collect($stubs) ->filter(fn ($to, $from) => Str::contains($from, [ - 'model', - 'controller', - 'migration', - 'seeder', - 'factory', - 'test', - 'pest', - 'resource', - 'notification', - 'mail', - 'job', - ])) + 'model', + 'controller', + 'migration', + 'seeder', + 'factory', + 'test', + 'pest', + 'resource', + 'notification', + 'mail', + 'job', + ])) ->all(); return collect($commonStubs) @@ -144,7 +144,7 @@ protected function determineStubsToPublish(array $stubs): array options: fn ($search) => collect($stubs) ->filter(fn ($to) => str_contains($to, $search)) ->mapWithKeys(fn ($to, $from) => [ - $from => $this->forHumans($to) + $from => $this->forHumans($to), ]) ->sort() ->all(), @@ -171,7 +171,7 @@ private function forHumans(string $stub): string if ($parts->count() <= 1) { return $parts->first(); } - + // Set the second segment as the prefix and join the remaining segments. return $parts ->splice(1, 1) From 4497cb90584d8ebd5d28a9a5e7cbf159e3a3a9f1 Mon Sep 17 00:00:00 2001 From: Andy Hinkle Date: Mon, 9 Dec 2024 20:47:35 -0600 Subject: [PATCH 7/7] Fix Stub Filter Selection --- src/Illuminate/Foundation/Console/StubPublishCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/StubPublishCommand.php b/src/Illuminate/Foundation/Console/StubPublishCommand.php index f66e2e996b85..e4168039a500 100644 --- a/src/Illuminate/Foundation/Console/StubPublishCommand.php +++ b/src/Illuminate/Foundation/Console/StubPublishCommand.php @@ -141,7 +141,7 @@ protected function determineStubsToPublish(array $stubs): array ->only( multisearch( 'Which stubs would you like to publish?', - options: fn ($search) => collect($stubs) + options: fn ($search) => collect($commonStubs) ->filter(fn ($to) => str_contains($to, $search)) ->mapWithKeys(fn ($to, $from) => [ $from => $this->forHumans($to),