Skip to content

Commit

Permalink
feat: New filament command + auto commits
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Jun 6, 2024
1 parent f0ec0bf commit ed71d28
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
99 changes: 99 additions & 0 deletions src/Commands/RESTPresenterFilamentCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace XtendPackages\RESTPresenter\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;
use XtendPackages\RESTPresenter\Concerns\InteractsWithGit;

use function Laravel\Prompts\confirm;

#[AsCommand(name: 'rest-presenter:filament')]
final class RESTPresenterFilamentCommand extends Command
{
use InteractsWithGit;

protected $signature = 'rest-presenter:filament
{--install : Install REST Presenter for Filament}
{--uninstall : Uninstall REST Presenter for Filament}';

protected $description = 'REST Presenter for Filament';

public function __construct(protected Filesystem $filesystem)
{
parent::__construct();
}

public function handle(): int
{
if ($this->option('install')) {
return $this->install();
}

if (! $this->filesystem->exists(config('rest-presenter.generator.path').'/StarterKits')) {
$this->components->warn('REST Presenter for Filament is not currently installed.');

return self::FAILURE;
}

if ($this->option('uninstall')) {
return $this->uninstall();
}

$this->components->warn('Please specify an option: --install, --uninstall');

return self::FAILURE;
}

private function install(): int
{
$this->components->info('Installing REST Presenter for Filament');

if (confirm(__('Would you like to auto-commit all changes made by the installer?'))) {
$this->gitAutoCommit = $this->isCleanWorkingDirectory();
}

$this->call('rest-presenter:xtend-starter-kit', ['name' => 'Sanctum']);
if ($this->gitAutoCommit) {
$this->commitChanges('feat: REST Presenter Sanctum Starter Kit');
}

$this->call('rest-presenter:xtend-starter-kit', ['name' => 'Filament']);
if ($this->gitAutoCommit) {
$this->commitChanges('feat: REST Presenter Filament Starter Kit');
}

$this->components->info('REST Presenter Filament installed successfully 🚀');

$this->components->info('Next step when your ready run "php artisan rest-presenter:generate-api-collection" to auto-generate your API collection for Insomnia or Postman.');

return self::SUCCESS;
}

private function uninstall(): int
{
if (! confirm('Are you sure you want to uninstall REST Presenter for Filament?')) {
return self::FAILURE;
}

if (confirm(__('Would you like to auto-commit revert changes made by the installer?'))) {
$this->gitAutoCommit = $this->isCleanWorkingDirectory();
}

$this->filesystem->delete(config_path('rest-presenter.php'));
$this->filesystem->deleteDirectory(config('rest-presenter.generator.path').'/StarterKits');
$this->filesystem->deleteDirectory(app()->basePath('tests/StarterKits'));
$this->filesystem->deleteDirectory(resource_path('rest-presenter'));

if ($this->gitAutoCommit) {
$this->commitChanges('revert: Remove REST Presenter Sanctum & Filament Starter Kits');
}

$this->components->info('REST Presenter Filament uninstalled successfully.');

return self::SUCCESS;
}
}
6 changes: 5 additions & 1 deletion src/Commands/XtendStarterKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function handle(): int
}

$this->generateStarterKit($starterKitsDirectory, $kitPath);
$this->autoDiscoverResources($kitPath);

return self::SUCCESS;
}
Expand Down Expand Up @@ -118,7 +119,8 @@ private function autoDiscoverResources(string $kitPath): void
{
$kitNamespace = Str::of($kitPath)->replace('/', '\\')->value();
$supportedKit = match ($kitNamespace) {
'Filament\Base' => 'FilamentStarterKit',
'Auth\\Sanctum\\Base' => 'SanctumStarterKit',
'Filament\\Base' => 'FilamentStarterKit',
default => false,
};

Expand All @@ -132,6 +134,8 @@ private function autoDiscoverResources(string $kitPath): void
$starterKit = resolve('XtendPackages\\RESTPresenter\\StarterKits\\'.$kitNamespace.'\\'.$supportedKit);
$resources = $starterKit->autoDiscover();

$this->filesystem->deleteDirectory(config('rest-presenter.generator.path').'/StarterKits/'.$kitPath);

if (! $resources) {
$this->components->warn(__('No resources were found for ":supported_kit"', ['supported_kit' => $supportedKit]));

Expand Down
33 changes: 33 additions & 0 deletions src/Concerns/InteractsWithGit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace XtendPackages\RESTPresenter\Concerns;

use Illuminate\Support\Facades\Process;

trait InteractsWithGit
{
protected bool $gitAutoCommit = false;

protected function commitChanges(string $message): void
{
$this->components->info('Committing changes...');

Process::run('git add .');
Process::run('git commit -m "'.$message.'"');

$this->components->info('Changes committed successfully');
}

protected function isCleanWorkingDirectory(): bool
{
$cleanDir = (Process::run('git diff --quiet'))->exitCode() === 0;

if (! $cleanDir) {
$this->components->warn('Please commit or stash your changes before proceeding with auto-commit');
}

return $cleanDir;
}
}
2 changes: 2 additions & 0 deletions src/RESTPresenterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use XtendPackages\RESTPresenter\Commands\Generator\MakePresenter;
use XtendPackages\RESTPresenter\Commands\Generator\MakeResource;
use XtendPackages\RESTPresenter\Commands\Generator\MakeTest;
use XtendPackages\RESTPresenter\Commands\RESTPresenterFilamentCommand;
use XtendPackages\RESTPresenter\Commands\RESTPresenterSetupCommand;
use XtendPackages\RESTPresenter\Commands\XtendStarterKit;
use XtendPackages\RESTPresenter\Facades\XtendRoute;
Expand All @@ -34,6 +35,7 @@ public function configurePackage(Package $package): void
->hasTranslations()
->hasCommands([
RESTPresenterSetupCommand::class,
RESTPresenterFilamentCommand::class,
XtendStarterKit::class,
MakeResource::class,
MakePresenter::class,
Expand Down
30 changes: 30 additions & 0 deletions src/StarterKits/Auth/Sanctum/Base/SanctumStarterKit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace XtendPackages\RESTPresenter\StarterKits\Auth\Sanctum\Base;

use XtendPackages\RESTPresenter\Base\StarterKit;
use XtendPackages\RESTPresenter\Concerns\InteractsWithClassDefinitions;
use XtendPackages\RESTPresenter\Concerns\InteractsWithDbSchema;

final class SanctumStarterKit extends StarterKit
{
use InteractsWithClassDefinitions;
use InteractsWithDbSchema;

protected static string $packageName = 'laravel/sanctum';

/**
* @var array<string, array<string, mixed>>
*/
private array $resources = [];

/**
* @return array<string, array<string, mixed>>
*/
public function autoDiscover(): array
{
return $this->resources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use XtendPackages\RESTPresenter\Resources\ResourceController;
use XtendPackages\RESTPresenter\StarterKits\Auth\Sanctum\Actions;

final class AuthResourceController extends ResourceController
class AuthResourceController extends ResourceController
{
public static bool $onlyRegisterActionRoutes = true;

Expand Down

0 comments on commit ed71d28

Please sign in to comment.