Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/laravel-prompts' into install-co…
Browse files Browse the repository at this point in the history
…llaboration-command
  • Loading branch information
duncanmcclean committed Mar 19, 2024
2 parents 94e5c91 + 5342049 commit 5f9c109
Show file tree
Hide file tree
Showing 32 changed files with 350 additions and 262 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"composer/composer": "^2.2.22",
"guzzlehttp/guzzle": "^6.3 || ^7.0",
"james-heinrich/getid3": "^1.9.21",
"laravel/framework": "^10.0 || ^11.0",
"laravel/framework": "^10.25.0 || ^11.0",
"laravel/helpers": "^1.1",
"laravel/prompts": "^0.1.16",
"league/commonmark": "^2.2",
"league/csv": "^9.0",
"league/glide": "^2.0",
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Commands/AddonsDiscover.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class AddonsDiscover extends Command
*/
public function handle(Manifest $manifest)
{
$this->newLine();
$manifest->build();

foreach (array_keys($manifest->manifest) as $package) {
$this->line("Discovered Addon: <info>{$package}</info>");
$this->components->task("Discovered Addon: <info>{$package}</info>");
}

$this->info('Addon manifest generated successfully.');
$this->components->info('Addon manifest generated successfully.');
}
}
64 changes: 36 additions & 28 deletions src/Console/Commands/AssetsGeneratePresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use Statamic\Jobs\GeneratePresetImageManipulation;
use Statamic\Support\Arr;

use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
use function Laravel\Prompts\note;
use function Laravel\Prompts\progress;

class AssetsGeneratePresets extends Command
{
use RunsInPlease;
Expand Down Expand Up @@ -46,12 +51,12 @@ public function handle()
$this->shouldQueue = $this->option('queue');

if ($this->shouldQueue && config('queue.default') === 'sync') {
$this->error('The queue connection is set to "sync". Queueing will be disabled.');
error('The queue connection is set to "sync". Queueing will be disabled.');
$this->shouldQueue = false;
}

AssetContainer::all()->sortBy('title')->each(function ($container) {
$this->line('Generating presets for <comment>'.$container->title().'</comment>...');
note('Generating presets for <comment>'.$container->title().'</comment>...');
$this->generatePresets($container);
$this->newLine();
});
Expand All @@ -73,37 +78,40 @@ private function generatePresets($container)
$cpPresets = config('statamic.cp.enabled') ? 1 : 0;

$steps = (count($container->warmPresets()) + $cpPresets) * count($assets);
$bar = $this->output->createProgressBar($steps);

foreach ($assets as $asset) {
$verb = $this->shouldQueue ? 'Queueing' : 'Generating';
$bar->setFormat("[%current%/%max%] $verb %filename% <comment>%preset%</comment>... ");

foreach ($asset->warmPresets() as $preset) {
$counts[$preset] = ($counts[$preset] ?? 0) + 1;
$bar->setMessage($preset, 'preset');
$bar->setMessage($asset->basename(), 'filename');
$bar->display();

$dispatchMethod = $this->shouldQueue
? 'dispatch'
: (method_exists(Dispatcher::class, 'dispatchSync') ? 'dispatchSync' : 'dispatchNow');

try {
GeneratePresetImageManipulation::$dispatchMethod($asset, $preset);
} catch (\Exception $e) {
Log::debug($e);
$counts['errors'] = ($counts['errors'] ?? 0) + 1;
}

$bar->advance();
if ($steps > 0) {
$progress = progress(
label: $this->shouldQueue ? 'Queueing...' : 'Generating...',
steps: $steps
);

$progress->start();

foreach ($assets as $asset) {
foreach ($asset->warmPresets() as $preset) {
$counts[$preset] = ($counts[$preset] ?? 0) + 1;
$progress->label("Generating $preset for {$asset->basename()}...");

$dispatchMethod = $this->shouldQueue
? 'dispatch'
: (method_exists(Dispatcher::class, 'dispatchSync') ? 'dispatchSync' : 'dispatchNow');

try {
GeneratePresetImageManipulation::$dispatchMethod($asset, $preset);
} catch (\Exception $e) {
Log::debug($e);
$counts['errors'] = ($counts['errors'] ?? 0) + 1;
}

$progress->advance();
}
}

$progress->finish();
}

$verb = $this->shouldQueue ? 'queued' : 'generated';
$bar->setFormat(sprintf("<info>[✔]</info> %s images $verb for %s assets.", $steps, count($assets)));
$bar->finish();
$this->newLine(2);
info(sprintf("<info>[✔]</info> %s images $verb for %s assets.", $steps, count($assets)));

if (property_exists($this, 'components')) {
$errors = Arr::pull($counts, 'errors');
Expand Down
28 changes: 17 additions & 11 deletions src/Console/Commands/AssetsMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Statamic\Facades\Asset;
use Statamic\Facades\AssetContainer;

use function Laravel\Prompts\progress;

class AssetsMeta extends Command
{
use RunsInPlease;
Expand All @@ -19,18 +21,22 @@ public function handle()
{
$assets = $this->getAssets();

$bar = $this->output->createProgressBar($assets->count());

$assets->each(function ($asset) use ($bar) {
$asset->hydrate();
$asset->save();
$bar->advance();
});

$bar->finish();
if ($assets->isEmpty()) {
return $this->components->warn("There's no metadata to generate. You don't have any assets.");
}

$this->line('');
$this->info('Asset metadata generated');
progress(
label: 'Generating asset metadata...',
steps: $assets,
callback: function ($asset, $progress) {
$asset->hydrate();
$asset->save();
$progress->advance();
},
hint: 'This may take a while if you have a lot of assets.'
);

$this->components->info("Generated metadata for {$assets->count()} ".str_plural('asset', $assets->count()).'.');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/AuthMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle()

File::put($to, $contents);

$this->line("<info>Created Auth Migration:</info> {$file}");
$this->components->info("Migration [$file] created successfully.");

$this->createGroupsTable();
$this->createRolesTable();
Expand All @@ -67,7 +67,7 @@ private function createGroupsTable()

File::put($to, $contents);

$this->line("<info>Created Groups Migration:</info> {$file}");
$this->components->info("Migration [$file] created successfully.");
}

private function createRolesTable()
Expand All @@ -86,6 +86,6 @@ private function createRolesTable()

File::put($to, $contents);

$this->line("<info>Created Roles Migration:</info> {$file}");
$this->components->info("Migration [$file] created successfully.");
}
}
10 changes: 1 addition & 9 deletions src/Console/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@ public function handle()
: $addon;
}

if (parent::handle() === false) {
return false;
}

$relativePath = $this->getRelativePath($this->getPath($this->qualifyClass($this->getNameInput())));

if (! $addon) {
$this->line("Your {$this->typeLower} class awaits: <comment>{$relativePath}</comment>");
}
return parent::handle();
}

/**
Expand Down
26 changes: 16 additions & 10 deletions src/Console/Commands/ImportGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Statamic\Contracts\Auth\UserGroupRepository as GroupRepositoryContract;
use Statamic\Facades\UserGroup;

use function Laravel\Prompts\error;
use function Laravel\Prompts\progress;

class ImportGroups extends Command
{
use RunsInPlease;
Expand All @@ -39,7 +42,7 @@ class ImportGroups extends Command
public function handle()
{
if (! config('statamic.users.tables.groups', false)) {
$this->error('You do not have eloquent driven groups enabled');
error('You do not have eloquent driven groups enabled');

return;
}
Expand All @@ -65,17 +68,20 @@ private function importGroups()
Facade::clearResolvedInstance(GroupContract::class);
Facade::clearResolvedInstance(GroupRepositoryContract::class);

$this->withProgressBar($groups, function ($group) {
$eloquentGroup = UserGroup::make()
->handle($group->handle())
->title($group->title())
->roles($group->roles())
->data($group->data()->except(['title', 'roles']));
progress(
label: 'Importing groups...',
steps: $groups,
callback: function ($group, $progress) {
$eloquentGroup = UserGroup::make()
->handle($group->handle())
->title($group->title())
->roles($group->roles())
->data($group->data()->except(['title', 'roles']));

$eloquentGroup->save();
});
$eloquentGroup->save();
}
);

$this->newLine();
$this->info('Groups imported');
}
}
24 changes: 15 additions & 9 deletions src/Console/Commands/ImportRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Statamic\Contracts\Auth\RoleRepository as RoleRepositoryContract;
use Statamic\Facades\Role;

use function Laravel\Prompts\error;
use function Laravel\Prompts\progress;

class ImportRoles extends Command
{
use RunsInPlease;
Expand All @@ -39,7 +42,7 @@ class ImportRoles extends Command
public function handle()
{
if (! config('statamic.users.tables.roles', false)) {
$this->error('You do not have eloquent driven roles enabled');
error('You do not have eloquent driven roles enabled');

return;
}
Expand All @@ -65,16 +68,19 @@ private function importRoles()
Facade::clearResolvedInstance(RoleContract::class);
Facade::clearResolvedInstance(RoleRepositoryContract::class);

$this->withProgressBar($roles, function ($role) {
$eloquentRole = Role::make($role->handle())
->title($role->title())
->permissions($role->permissions())
->preferences($role->preferences());
progress(
label: 'Importing roles...',
steps: $roles,
callback: function ($role) {
$eloquentRole = Role::make($role->handle())
->title($role->title())
->permissions($role->permissions())
->preferences($role->preferences());

$eloquentRole->save();
});
$eloquentRole->save();
}
);

$this->newLine();
$this->info('Roles imported');
}
}
55 changes: 31 additions & 24 deletions src/Console/Commands/ImportUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Statamic\Stache\Repositories\UserRepository as FileRepository;
use Statamic\Stache\Stores\UsersStore;

use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
use function Laravel\Prompts\progress;

class ImportUsers extends Command
{
use RunsInPlease;
Expand All @@ -41,7 +45,7 @@ class ImportUsers extends Command
public function handle()
{
if (config('statamic.users.repository') !== 'eloquent') {
$this->error('Your site is not using the eloquent user repository.');
error('Your site is not using the eloquent user repository.');

return 0;
}
Expand All @@ -58,7 +62,7 @@ private function importUsers()
$model = config("auth.providers.$provider.model");

if (! in_array(HasUuids::class, class_uses_recursive($model))) {
$this->error('Your user model must use the HasUuids trait for this migration to run');
error('Please add the HasUuids trait to your '.$model.' model in order to run this importer.');

return;
}
Expand All @@ -75,34 +79,37 @@ private function importUsers()

$eloquentRepository = app(UserRepositoryManager::class)->createEloquentDriver([]);

$this->withProgressBar($users, function ($user) use ($eloquentRepository) {
$data = $user->data();
progress(
label: 'Importing users...',
steps: $users,
callback: function ($user, $progress) use ($eloquentRepository) {
$data = $user->data();

$eloquentUser = $eloquentRepository->make()
->email($user->email())
->preferences($user->preferences())
->data($data->except(['groups', 'roles'])->merge(['name' => $user->name()]))
->id($user->id());
$eloquentUser = $eloquentRepository->make()
->email($user->email())
->preferences($user->preferences())
->data($data->except(['groups', 'roles'])->merge(['name' => $user->name()]))
->id($user->id());

if ($user->isSuper()) {
$eloquentUser->makeSuper();
}
if ($user->isSuper()) {
$eloquentUser->makeSuper();
}

if (count($data->get('groups', [])) > 0) {
$eloquentUser->groups($data->get('groups'));
}
if (count($data->get('groups', [])) > 0) {
$eloquentUser->groups($data->get('groups'));
}

if (count($data->get('roles', [])) > 0) {
$eloquentUser->roles($data->get('roles'));
}
if (count($data->get('roles', [])) > 0) {
$eloquentUser->roles($data->get('roles'));
}

$eloquentUser->saveToDatabase();
$eloquentUser->saveToDatabase();

$eloquentUser->model()->forceFill(['password' => $user->password()]);
$eloquentUser->model()->saveQuietly();
});
$eloquentUser->model()->forceFill(['password' => $user->password()]);
$eloquentUser->model()->saveQuietly();
}
);

$this->newLine();
$this->info('Users imported');
info('Users imported');
}
}
Loading

0 comments on commit 5f9c109

Please sign in to comment.