Skip to content

Commit

Permalink
feat: Setup command now has the option to auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-code-labx committed Jun 7, 2024
1 parent 4207592 commit 82f268d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
64 changes: 28 additions & 36 deletions src/Commands/RESTPresenterSetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
use Symfony\Component\Console\Attribute\AsCommand;
use UnhandledMatchError;

use XtendPackages\RESTPresenter\Concerns\InteractsWithGit;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\multiselect;

#[AsCommand(name: 'rest-presenter:setup')]
final class RESTPresenterSetupCommand extends Command
{
protected $signature = 'rest-presenter:setup {--starter-kit= : Install starter kit}';
use InteractsWithGit;

protected $signature = 'rest-presenter:setup';

protected $description = 'Setup REST Presenter & prepare your API structure';

Expand All @@ -27,6 +30,12 @@ public function __construct(protected Filesystem $filesystem)

public function handle(): int
{
$this->components->info('Welcome to REST Presenter setup wizard');

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

if (! $this->firstTimeSetup()) {
$this->components->info('REST Presenter has already been setup. Now checking for updates...');
$this->checkForUpdates();
Expand All @@ -48,11 +57,12 @@ public function handle(): int

private function initialSetup(): void
{
$this->components->info('Welcome to REST Presenter setup wizard');

$this->publishingConfig();
$this->publishingDefaultResources();
$this->publishStarterKits();
$this->publishingDefaultResources();

if (confirm('Would you like to create your first resource?')) {
$this->call('rest-presenter:make-resource');
}
}

private function starGitHubRepo(): void
Expand Down Expand Up @@ -87,11 +97,6 @@ private function sponsorThisProject(): void
}
}

private function publishingConfig(): void
{
$this->call('vendor:publish', ['--tag' => 'rest-presenter-config']);
}

private function publishingDefaultResources(): void
{
collect($this->filesystem->directories(__DIR__.'/../Resources'))
Expand Down Expand Up @@ -120,17 +125,14 @@ private function publishStarterKits(): void
$starterKitsDirectory = __DIR__.'/../StarterKits';
$generatedKitsDirectory = config('rest-presenter.generator.path').'/StarterKits';

if (! app()->runningUnitTests()) {
$this->filesystem->ensureDirectoryExists($generatedKitsDirectory);
}

/** @var \Illuminate\Support\Collection<string, string> $unpublishedStarterKits */
$unpublishedStarterKits = collect($this->filesystem->allFiles($starterKitsDirectory))
->map(fn ($file): string => $file->getRelativePathname())
->filter(fn ($file): bool => ! $this->filesystem->exists($generatedKitsDirectory.'/'.$file))
->filter(fn ($file): bool => str_ends_with($file, 'ApiKitServiceProvider.php'))
->map(fn ($file): string => str_replace('ApiKitServiceProvider.php', '', basename($file)))
->filter(fn ($file): bool => str_ends_with($file, 'StarterKit.php'))
->map(fn ($file): string => str_replace('StarterKit.php', '', basename($file)))
->map(fn ($kit): string => $kit)
->filter(fn ($kit): bool => ! $this->filesystem->exists($generatedKitsDirectory.'/'.$kit))
->filter(fn ($kit): bool => $kit !== 'Filament')
->values();

if ($unpublishedStarterKits->isEmpty()) {
Expand All @@ -139,43 +141,33 @@ private function publishStarterKits(): void
return;
}

if ($this->option('starter-kit')) {
$starterKit = type($this->option('starter-kit'))->asString();

if (! $unpublishedStarterKits->contains($starterKit)) {
$this->components->warn(__('The starter kit :name is already installed', ['name' => $starterKit]));
exit;
}

if ($starterKit === 'Filament' && $this->filesystem->exists($generatedKitsDirectory.'/Filament')) {
$this->components->warn('Filament starter kit has already been installed');
exit;
}

$this->call('rest-presenter:xtend-starter-kit', ['name' => $starterKit]);

return;
}

$starterKits = multiselect(
label: 'Would you like to install any of these starter kits?',
options: $unpublishedStarterKits->toArray(), // @phpstan-ignore-line
default: ['Sanctum'],
hint: 'You can re-run this command to install more starter kits later',
);

if ($starterKits === []) {
return;
}

if (! app()->runningUnitTests()) {
$this->filesystem->ensureDirectoryExists($generatedKitsDirectory);
}

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

private function firstTimeSetup(): bool
{
return ! $this->filesystem->exists(
path: type(config('rest-presenter.generator.path'))->asString(),
path: type(config('rest-presenter.generator.path').'/Resources')->asString(),
);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Commands/XtendStarterKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ private function autoDiscoverResources(string $kitPath): void
};

if ($supportedKit === false) {
$this->components->warn(__('No supported kit was found for ":kit_namespace"', ['kit_namespace' => $kitNamespace]));

return;
}

Expand Down

0 comments on commit 82f268d

Please sign in to comment.