Skip to content

Commit

Permalink
[5.x] Improvements to the install:eloquent-driver command (#10910)
Browse files Browse the repository at this point in the history
* Add `--everything` option

This will allow us to select all repositories in the CLI, without the user needing to see the prompt.

* Add `--import` option

* Rename option to `all`

* tweak

* Add `--without-messages` option to hide info messages
  • Loading branch information
duncanmcclean authored Oct 8, 2024
1 parent 3956ab1 commit 9a24295
Showing 1 changed file with 86 additions and 60 deletions.
146 changes: 86 additions & 60 deletions src/Console/Commands/InstallEloquentDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class InstallEloquentDriver extends Command
*
* @var string
*/
protected $signature = 'statamic:install:eloquent-driver';
protected $signature = 'statamic:install:eloquent-driver
{ --all : Configures all repositories to use the database }
{ --import : Whether existing data should be imported }
{ --without-messages : Disables output messages }';

/**
* The console command description.
Expand All @@ -50,12 +53,12 @@ public function handle()
message: 'Installing the statamic/eloquent-driver package...'
);

$this->components->info('Installed statamic/eloquent-driver package');
$this->infoMessage('Installed statamic/eloquent-driver package');
}

if (! File::exists(config_path('statamic/eloquent-driver.php'))) {
$this->runArtisanCommand('vendor:publish --tag=statamic-eloquent-config');
$this->components->info('Config file [config/statamic/eloquent-driver.php] published successfully.');
$this->infoMessage('Config file [config/statamic/eloquent-driver.php] published successfully.');
}

try {
Expand All @@ -69,21 +72,30 @@ public function handle()
return $this->components->warn("No repositories left to migrate. You're already using the Eloquent Driver for all repositories.");
}

$repositories = multiselect(
label: 'Which repositories would you like to migrate?',
hint: 'You can always import other repositories later.',
options: $this->availableRepositories()->all(),
validate: fn (array $values) => count($values) === 0
? 'You must select at least one repository to migrate.'
: null
);
$repositories = $this->repositories();

foreach ($repositories as $repository) {
$method = 'migrate'.Str::studly($repository);
$this->$method();
}
}

protected function repositories(): array
{
if ($this->option('all')) {
return $this->availableRepositories()->keys()->all();
}

return multiselect(
label: 'Which repositories would you like to migrate?',
options: $this->availableRepositories()->all(),
validate: fn (array $values) => count($values) === 0
? 'You must select at least one repository to migrate.'
: null,
hint: 'You can always import other repositories later.'
);
}

protected function availableRepositories(): Collection
{
return collect([
Expand Down Expand Up @@ -177,15 +189,15 @@ protected function migrateAssetContainers(): void
message: 'Migrating asset containers...'
);

$this->components->info('Configured asset containers');
$this->infoMessage('Configured asset containers');

if (confirm('Would you like to import existing asset containers?')) {
if ($this->shouldImport('asset containers')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-assets --force --only-asset-containers'),
message: 'Importing existing asset containers...'
);

$this->components->info('Imported existing asset containers');
$this->infoMessage('Imported existing asset containers');
}
}

Expand All @@ -201,15 +213,15 @@ protected function migrateAssets(): void
message: 'Migrating assets...'
);

$this->components->info('Configured assets');
$this->infoMessage('Configured assets');

if (confirm('Would you like to import existing assets?')) {
if ($this->shouldImport('assets')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-assets --force --only-assets'),
message: 'Importing existing assets...'
);

$this->components->info('Imported existing assets');
$this->infoMessage('Imported existing assets');
}
}

Expand All @@ -225,15 +237,15 @@ protected function migrateBlueprints(): void
message: 'Migrating blueprints...'
);

$this->components->info('Configured blueprints');
$this->infoMessage('Configured blueprints');

if (confirm('Would you like to import existing blueprints?')) {
if ($this->shouldImport('blueprints')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-blueprints --force --only-blueprints'),
message: 'Importing existing blueprints...'
);

$this->components->info('Imported existing blueprints');
$this->infoMessage('Imported existing blueprints');
}
}

Expand All @@ -249,15 +261,15 @@ protected function migrateCollections(): void
message: 'Migrating collections...'
);

$this->components->info('Configured collections');
$this->infoMessage('Configured collections');

if (confirm('Would you like to import existing collections?')) {
if ($this->shouldImport('collections')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-collections --force --only-collections'),
message: 'Importing existing collections...'
);

$this->components->info('Imported existing collections');
$this->infoMessage('Imported existing collections');
}
}

Expand All @@ -273,21 +285,21 @@ protected function migrateCollectionTrees(): void
message: 'Migrating collection trees...'
);

$this->components->info('Configured collection trees');
$this->infoMessage('Configured collection trees');

if (confirm('Would you like to import existing collection trees?')) {
if ($this->shouldImport('collection trees')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-collections --force --only-collection-trees'),
message: 'Importing existing collections...'
);

$this->components->info('Imported existing collection trees');
$this->infoMessage('Imported existing collection trees');
}
}

protected function migrateEntries(): void
{
$shouldImportEntries = confirm('Would you like to import existing entries?');
$shouldImportEntries = $this->shouldImport('entries');

spin(
callback: function () use ($shouldImportEntries) {
Expand Down Expand Up @@ -325,7 +337,7 @@ protected function migrateEntries(): void
: 'Migrating and importing entries...'
);

$this->components->info(
$this->infoMessage(
$shouldImportEntries
? 'Configured & imported existing entries'
: 'Configured entries'
Expand All @@ -344,15 +356,15 @@ protected function migrateFieldsets(): void
message: 'Migrating fieldsets...'
);

$this->components->info('Configured fieldsets');
$this->infoMessage('Configured fieldsets');

if (confirm('Would you like to import existing fieldsets?')) {
if ($this->shouldImport('fieldsets')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-blueprints --force --only-fieldsets'),
message: 'Importing existing fieldsets...'
);

$this->components->info('Imported existing fieldsets');
$this->infoMessage('Imported existing fieldsets');
}
}

Expand All @@ -368,15 +380,15 @@ protected function migrateForms(): void
message: 'Migrating forms...'
);

$this->components->info('Configured forms');
$this->infoMessage('Configured forms');

if (confirm('Would you like to import existing forms?')) {
if ($this->shouldImport('forms')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-forms --only-forms'),
message: 'Importing existing forms...'
);

$this->components->info('Imported existing forms');
$this->infoMessage('Imported existing forms');
}
}

Expand All @@ -392,15 +404,15 @@ protected function migrateFormSubmissions(): void
message: 'Migrating form submissions...'
);

$this->components->info('Configured form submissions');
$this->infoMessage('Configured form submissions');

if (confirm('Would you like to import existing form submissions?')) {
if ($this->shouldImport('form submissions')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-forms --only-form-submissions'),
message: 'Importing existing form submissions...'
);

$this->components->info('Imported existing form submissions');
$this->infoMessage('Imported existing form submissions');
}
}

Expand All @@ -416,15 +428,15 @@ protected function migrateGlobals(): void
message: 'Migrating globals...'
);

$this->components->info('Configured globals');
$this->infoMessage('Configured globals');

if (confirm('Would you like to import existing globals?')) {
if ($this->shouldImport('globals')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-globals --only-global-sets'),
message: 'Importing existing globals...'
);

$this->components->info('Imported existing globals');
$this->infoMessage('Imported existing globals');
}
}

Expand All @@ -440,15 +452,15 @@ protected function migrateGlobalVariables(): void
message: 'Migrating global variables...'
);

$this->components->info('Configured global variables');
$this->infoMessage('Configured global variables');

if (confirm('Would you like to import existing global variables?')) {
if ($this->shouldImport('global variables')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-globals --only-global-variables'),
message: 'Importing existing global variables...'
);

$this->components->info('Imported existing global variables');
$this->infoMessage('Imported existing global variables');
}
}

Expand All @@ -464,15 +476,15 @@ protected function migrateNavs(): void
message: 'Migrating navs...'
);

$this->components->info('Configured navs');
$this->infoMessage('Configured navs');

if (confirm('Would you like to import existing navs?')) {
if ($this->shouldImport('navs')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-navs --force --only-navs'),
message: 'Importing existing navs...'
);

$this->components->info('Imported existing navs');
$this->infoMessage('Imported existing navs');
}
}

Expand All @@ -488,15 +500,15 @@ protected function migrateNavTrees(): void
message: 'Migrating nav trees...'
);

$this->components->info('Configured nav trees');
$this->infoMessage('Configured nav trees');

if (confirm('Would you like to import existing nav trees?')) {
if ($this->shouldImport('nav trees')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-navs --force --only-nav-trees'),
message: 'Importing existing nav trees...'
);

$this->components->info('Imported existing navs trees');
$this->infoMessage('Imported existing navs trees');
}
}

Expand All @@ -511,15 +523,15 @@ protected function migrateRevisions(): void
},
);

$this->components->info('Configured revisions');
$this->infoMessage('Configured revisions');

if (confirm('Would you like to import existing revisions?')) {
if ($this->shouldImport('revisions')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-revisions'),
message: 'Importing existing revisions...'
);

$this->components->info('Imported existing revisions');
$this->infoMessage('Imported existing revisions');
}
}

Expand All @@ -537,7 +549,7 @@ protected function migrateSites(): void
message: 'Migrating sites...'
);

$this->components->info('Configured & imported sites');
$this->infoMessage('Configured & imported sites');
}

protected function migrateTaxonomies(): void
Expand All @@ -552,15 +564,15 @@ protected function migrateTaxonomies(): void
message: 'Migrating taxonomies...'
);

$this->components->info('Configured taxonomies');
$this->infoMessage('Configured taxonomies');

if (confirm('Would you like to import existing taxonomies?')) {
if ($this->shouldImport('taxonomies')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-taxonomies --force --only-taxonomies'),
message: 'Importing existing taxonomies...'
);

$this->components->info('Imported existing taxonomies');
$this->infoMessage('Imported existing taxonomies');
}
}

Expand All @@ -576,15 +588,15 @@ protected function migrateTerms(): void
message: 'Migrating terms...'
);

$this->components->info('Configured terms');
$this->infoMessage('Configured terms');

if (confirm('Would you like to import existing terms?')) {
if ($this->shouldImport('terms')) {
spin(
callback: fn () => $this->runArtisanCommand('statamic:eloquent:import-taxonomies --force --only-terms'),
message: 'Importing existing terms...'
);

$this->components->info('Imported existing terms');
$this->infoMessage('Imported existing terms');
}
}

Expand All @@ -600,7 +612,21 @@ protected function migrateTokens(): void
message: 'Migrating tokens...'
);

$this->components->info('Configured tokens');
$this->infoMessage('Configured tokens');
}

private function shouldImport(string $repository): bool
{
return $this->option('import') || confirm("Would you like to import existing {$repository}?");
}

private function infoMessage(string $message): void
{
if ($this->option('without-messages')) {
return;
}

$this->components->info('Configured asset containers');
}

private function switchToEloquentDriver(string $repository): void
Expand Down

0 comments on commit 9a24295

Please sign in to comment.