Skip to content

Commit

Permalink
Merge pull request #124 from awcodes/feat/minimal-output
Browse files Browse the repository at this point in the history
  • Loading branch information
bezhanSalleh authored Sep 27, 2022
2 parents 8fb1d26 + af78c90 commit 7fa1ae0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 57 deletions.
87 changes: 50 additions & 37 deletions src/Commands/MakeShieldGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class MakeShieldGenerateCommand extends Command
{--widget= : One or many widgets separated by comma (,) }
{--exclude : Exclude the given entities during generation }
{--ignore-config-exclude : Ignore config `<fg=yellow;options=bold>exclude</>` option during generation }
{--minimal : Output minimal amount of info to console}
';
// {--seeder : Exclude the given entities during generation }
// the idea is to generate a seeder that can be used on production deployment
Expand Down Expand Up @@ -100,7 +101,7 @@ public function handle(): int
$this->widgetInfo($widgets->toArray());
}

$this->info('Permission & Policies are generated according to your config or passed options.');
$this->comment('Permission & Policies are generated according to your config or passed options.');
$this->info('Enjoy!');

if (cache()->has('shield_general_exclude')) {
Expand Down Expand Up @@ -224,50 +225,62 @@ protected function generateForWidgets(array $widgets): Collection

protected function resourceInfo(array $resources): void
{
$this->info('Successfully generated Permissions & Policies for:');
$this->table(
['#', 'Resource', 'Policy', 'Permissions'],
collect($resources)->map(function ($resource, $key) {
return [
'#' => $key + 1,
'Resource' => $resource['model'],
'Policy' => "{$resource['model']}Policy.php".($this->generatorOption !== 'permissions' ? '' : ''),
'Permissions' => implode(','.PHP_EOL, collect(config('filament-shield.permission_prefixes.resource'))->map(function ($permission, $key) use ($resource) {
return $permission.'_'.$resource['resource'];
})->toArray()).($this->generatorOption !== 'policies' ? '' : ''),
];
})
);
if ($this->option('minimal')) {
$this->info('Successfully generated Permissions & Policies.');
} else {
$this->info('Successfully generated Permissions & Policies for:');
$this->table(
['#', 'Resource', 'Policy', 'Permissions'],
collect($resources)->map(function ($resource, $key) {
return [
'#' => $key + 1,
'Resource' => $resource['model'],
'Policy' => "{$resource['model']}Policy.php".($this->generatorOption !== 'permissions' ? '' : ''),
'Permissions' => implode(','.PHP_EOL, collect(config('filament-shield.permission_prefixes.resource'))->map(function ($permission, $key) use ($resource) {
return $permission.'_'.$resource['resource'];
})->toArray()).($this->generatorOption !== 'policies' ? '' : ''),
];
})
);
}
}

protected function pageInfo(array $pages): void
{
$this->info('Successfully generated Page Permissions for:');
$this->table(
['#', 'Page', 'Permission'],
collect($pages)->map(function ($page, $key) {
return [
'#' => $key + 1,
'Page' => Str::replace(config('filament-shield.permission_prefixes.page').'_', '', $page),
'Permission' => $page,
];
})
);
if ($this->option('minimal')) {
$this->info('Successfully generated Page Permissions.');
} else {
$this->info('Successfully generated Page Permissions for:');
$this->table(
['#', 'Page', 'Permission'],
collect($pages)->map(function ($page, $key) {
return [
'#' => $key + 1,
'Page' => Str::replace(config('filament-shield.permission_prefixes.page').'_', '', $page),
'Permission' => $page,
];
})
);
}
}

protected function widgetInfo(array $widgets): void
{
$this->info('Successfully generated Widget Permissions for:');
$this->table(
['#', 'Widget', 'Permission'],
collect($widgets)->map(function ($widget, $key) {
return [
'#' => $key + 1,
'Widget' => Str::replace(config('filament-shield.permission_prefixes.widget').'_', '', $widget),
'Permission' => $widget,
];
})
);
if ($this->option('minimal')) {
$this->info('Successfully generated Widget Permissions.');
} else {
$this->info('Successfully generated Widget Permissions for:');
$this->table(
['#', 'Widget', 'Permission'],
collect($widgets)->map(function ($widget, $key) {
return [
'#' => $key + 1,
'Widget' => Str::replace(config('filament-shield.permission_prefixes.widget').'_', '', $widget),
'Permission' => $widget,
];
})
);
}
}

protected static function getPolicyStub(string $model): string
Expand Down
51 changes: 31 additions & 20 deletions src/Commands/MakeShieldInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MakeShieldInstallCommand extends Command
public $signature = 'shield:install
{--F|fresh : re-run the migrations}
{--O|only : Only setups shield without generating permissions and creating super-admin}
{--minimal : Output minimal amount of info to console}
';

public $description = 'Setup Core Package requirements and Install Shield';
Expand All @@ -27,13 +28,17 @@ public function handle(): int
return self::INVALID;
}

$this->alert('Following operations will be performed:');
$this->info('- Publishes core package config');
$this->info('- Publishes core package migration');
$this->warn(' - On fresh applications database will be migrated');
$this->warn(' - You can also force this behavior by supplying the --fresh option');
if ($this->option('minimal')) {
$confirmed = true;
} else {
$this->alert('Following operations will be performed:');
$this->info('- Publishes core package config');
$this->info('- Publishes core package migration');
$this->warn(' - On fresh applications database will be migrated');
$this->warn(' - You can also force this behavior by supplying the --fresh option');

$confirmed = $this->confirm('Do you wish to continue?', true);
$confirmed = $this->confirm('Do you wish to continue?', true);
}

if ($this->CheckIfAlreadyInstalled() && ! $this->option('fresh')) {
$this->comment('Seems you have already installed the Core package(`spatie/laravel-permission`)!');
Expand All @@ -52,18 +57,20 @@ public function handle(): int
$this->comment('`shield:install` command was cancelled.');
}

if ($this->confirm('Would you like to show some love by starring the repo?', true)) {
if (PHP_OS_FAMILY === 'Darwin') {
exec('open https://github.com/bezhanSalleh/filament-shield');
}
if (PHP_OS_FAMILY === 'Linux') {
exec('xdg-open https://github.com/bezhanSalleh/filament-shield');
if (! $this->option('minimal')) {
if ($this->confirm('Would you like to show some love by starring the repo?', true)) {
if (PHP_OS_FAMILY === 'Darwin') {
exec('open https://github.com/bezhanSalleh/filament-shield');
}
if (PHP_OS_FAMILY === 'Linux') {
exec('xdg-open https://github.com/bezhanSalleh/filament-shield');
}
if (PHP_OS_FAMILY === 'Windows') {
exec('start https://github.com/bezhanSalleh/filament-shield');
}

$this->line('Thank you!');
}
if (PHP_OS_FAMILY === 'Windows') {
exec('start https://github.com/bezhanSalleh/filament-shield');
}

$this->line('Thank you!');
}

return self::SUCCESS;
Expand All @@ -90,13 +97,13 @@ protected function getTables(): Collection

protected function install(bool $fresh = false)
{
$this->call('vendor:publish', [
$this->{$this->option('minimal') ? 'callSilent' : 'call'}('vendor:publish', [
'--provider' => 'Spatie\Permission\PermissionServiceProvider',
]);

$this->info('Core Package config published.');

$this->call('vendor:publish', [
$this->{$this->option('minimal') ? 'callSilent' : 'call'}('vendor:publish', [
'--tag' => 'filament-shield-config',
]);

Expand All @@ -115,19 +122,23 @@ protected function install(bool $fresh = false)
$this->info('running shield migrations.');
}

$this->call('migrate');
$this->{$this->option('minimal') ? 'callSilent' : 'call'}('migrate');

if (! $this->option('only')) {
$this->newLine();
$this->info('Generating permissions ...');
$this->call('shield:generate', [
'--all' => true,
'--minimal' => $this->option('minimal'),
]);

$this->newLine();
$this->info('Creating a filament user with Super Admin Role...');
$this->call('shield:super-admin');
} else {
$this->call('shield:generate', [
'--resource' => 'RoleResource',
'--minimal' => $this->option('minimal'),
]);
}

Expand Down

0 comments on commit 7fa1ae0

Please sign in to comment.