Skip to content

Commit 1bec27f

Browse files
authored
Merge pull request #2021 from alissn/FixModulePrune
[fix] fix module prune command
2 parents a3fe058 + 35d70ef commit 1bec27f

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/Commands/Actions/ModelPruneCommand.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
use Illuminate\Support\Str;
99
use InvalidArgumentException;
1010
use Nwidart\Modules\Facades\Module;
11+
use Symfony\Component\Console\Attribute\AsCommand;
1112
use Symfony\Component\Console\Input\InputInterface;
1213
use Symfony\Component\Console\Output\OutputInterface;
1314
use Symfony\Component\Finder\Finder;
1415

1516
use function Laravel\Prompts\multiselect;
1617

18+
#[AsCommand(name: 'module:prune')]
1719
class ModelPruneCommand extends PruneCommand implements PromptsForMissingInput
1820
{
1921
public const ALL = 'All';
@@ -48,12 +50,17 @@ protected function promptForMissingArguments(InputInterface $input, OutputInterf
4850
return;
4951
}
5052

53+
if (! empty($input->getArgument('module'))) {
54+
return;
55+
}
56+
5157
$selected_item = multiselect(
5258
label : 'Select Modules',
53-
options : [
54-
self::ALL,
55-
...array_keys(Module::all()),
56-
],
59+
options : collect(Module::allEnabled())
60+
->map(fn (\Nwidart\Modules\Module $module) => $module->getName())
61+
->prepend(self::ALL)
62+
->values()
63+
->toArray(),
5764
required: 'You must select at least one module',
5865
);
5966

@@ -82,19 +89,23 @@ protected function models(): Collection
8289
throw new InvalidArgumentException('The --models and --except options cannot be combined.');
8390
}
8491

85-
if ($this->argument('module') == [self::ALL]) {
92+
$modules = collect($this->argument('module'));
93+
94+
if ($modules->contains(self::ALL)) {
8695
$path = sprintf(
8796
'%s/*/%s',
8897
config('modules.paths.modules'),
8998
config('modules.paths.generator.model.path')
9099
);
91100
} else {
92-
$path = sprintf(
93-
'%s/{%s}/%s',
101+
$path = collect($modules)->map(fn ($module) => sprintf(
102+
'%s/%s/%s',
94103
config('modules.paths.modules'),
95-
collect($this->argument('module'))->implode(','),
104+
$module,
96105
config('modules.paths.generator.model.path')
97-
);
106+
))
107+
->filter(fn ($path) => is_dir($path))
108+
->toArray();
98109
}
99110

100111
return collect(Finder::create()->in($path)->files()->name('*.php'))
@@ -107,7 +118,8 @@ protected function models(): Collection
107118
['\\', ''],
108119
Str::after($model->getRealPath(), realpath(config('modules.paths.modules')))
109120
);
110-
})->values()
121+
})
122+
->values()
111123
->when(! empty($except), function ($models) use ($except) {
112124
return $models->reject(function ($model) use ($except) {
113125
return in_array($model, $except);

0 commit comments

Comments
 (0)