1
0

Refactoring of bundles related commands

Closes #5
This commit is contained in:
Richard Dern 2024-05-13 23:08:13 +02:00
parent d738581512
commit 409b9905bf
8 changed files with 136 additions and 208 deletions

View File

@ -3,11 +3,11 @@
namespace App\Console\Commands\Bundle; namespace App\Console\Commands\Bundle;
use App\Classes\Bundle; use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Process; use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use function Laravel\Prompts\confirm; use function Laravel\Prompts\confirm;
@ -15,6 +15,9 @@
class Render extends Command class Render extends Command
{ {
use ReadsBundles;
use SelectsDisks;
/** /**
* The console command description. * The console command description.
* *
@ -22,10 +25,6 @@ class Render extends Command
*/ */
protected $description = 'Render bundles'; protected $description = 'Render bundles';
protected Filesystem $sourceDisk;
protected Filesystem $targetDisk;
public function __construct() public function __construct()
{ {
$this->signature = 'bundle:render $this->signature = 'bundle:render
@ -50,11 +49,12 @@ public function __construct()
public function handle() public function handle()
{ {
$this->showDisclaimer() $this->showDisclaimer()
->selectDisk() ->selectDisk(true, true)
->renderAssets() ->renderAssets()
->clearCache() ->clearCache()
->validate() ->validate()
->renderFeed() ->renderFeed()
->selectBundles()
->render() ->render()
->deploy(); ->deploy();
} }
@ -75,28 +75,6 @@ private function showDisclaimer(): self
return $this; return $this;
} }
/**
* Select the disk we will be working on
*/
private function selectDisk(): self
{
$sourceDisk = $this->option('source-disk') ?? env('CONTENT_DISK');
$targetDisk = $this->option('target-disk') ?? env('DIST_DISK');
$this->sourceDisk = Storage::disk($sourceDisk);
$this->targetDisk = Storage::disk($targetDisk);
$this->line(
sprintf(
'Using <info>%s</info> as source disk and <info>%s</info> as target disk for generated content',
$sourceDisk,
$targetDisk
)
);
return $this;
}
/** /**
* Build assets, and clear cache afterwise * Build assets, and clear cache afterwise
*/ */
@ -147,7 +125,13 @@ private function validate(): self
return $this; return $this;
} }
$result = $this->call('bundle:validate', ['--recursive' => true]); $options = [
'--recursive' => $this->option('recursive'),
'--source-disk' => $this->option('source-disk'),
'path' => $this->argument('path') ?? '/',
];
$result = $this->call('bundle:validate', $options);
if (!empty($result)) { if (!empty($result)) {
if (confirm('Validation errors have occurred. Cancel process?')) { if (confirm('Validation errors have occurred. Cancel process?')) {
@ -158,32 +142,6 @@ private function validate(): self
return $this; return $this;
} }
/**
* Collect a list of bundles to render
*/
private function getBundles()
{
$path = $this->argument('path') ?? '/';
$comment = sprintf('Rendering <info>%s</info>', $path);
if ($this->option('recursive')) {
$comment .= ' and <info>all sub-bundles</info>';
}
$this->line($comment);
$this->output->write('Collecting bundles... ');
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($this->sourceDisk, $path, true, false);
} else {
$bundles = [new Bundle($path, $this->sourceDisk)];
}
$this->info('OK');
return $bundles;
}
private function renderFeed(): self private function renderFeed(): self
{ {
$result = Bundle::renderFeed($this->sourceDisk); $result = Bundle::renderFeed($this->sourceDisk);
@ -218,7 +176,7 @@ private function renderFeed(): self
*/ */
private function render(): self private function render(): self
{ {
$bundles = $this->getBundles(); $bundles = $this->bundles;
progress( progress(
label: 'Rendering... ', label: 'Rendering... ',

View File

@ -3,56 +3,56 @@
namespace App\Console\Commands\Bundle; namespace App\Console\Commands\Bundle;
use App\Classes\Bundle; use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use function Laravel\Prompts\progress;
class Repair extends Command class Repair extends Command
{ {
/** use ReadsBundles;
* The name and signature of the console command. use SelectsDisks;
*
* @var string
*/
protected $signature = 'bundle:repair { --r|recursive : Also repair sub-bundles } { path? : Specific bundle to repair }';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Repair articles'; protected $description = 'Repair bundles';
public function __construct()
{
$this->signature = 'bundle:repair
{ --r|recursive : Also repair sub-bundles }
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to repair - Default to <info>/</info> }
';
parent::__construct();
}
/** /**
* Execute the console command. * Execute the console command.
*/ */
public function handle() public function handle()
{ {
$disk = Storage::disk(env('CONTENT_DISK')); $this->selectDisk()
$path = $this->argument('path') ?? '/'; ->selectBundles()
->repair();
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($disk, $path, true);
} else {
$bundles = [new Bundle($path, $disk)];
}
foreach ($bundles as $bundle) {
$this->output->write(sprintf('Repairing %s... ', $bundle->getPath()));
$bundle->repair();
$this->info('OK');
}
foreach (['blog', 'liens-interessants'] as $section) { foreach (['blog', 'liens-interessants'] as $section) {
$this->createIntermediateBundles($section); $this->createIntermediateBundles($section);
} }
} }
protected function createIntermediateBundles($parent) /**
* Create intermediary bundles
*/
private function createIntermediateBundles($parent)
{ {
$disk = Storage::disk(env('CONTENT_DISK')); $disk = $this->sourceDisk;
foreach ($disk->directories($parent) as $dir) { foreach ($disk->directories($parent) as $dir) {
if (basename($dir) === 'data') { if (basename($dir) === 'data') {
@ -93,11 +93,29 @@ protected function createIntermediateBundles($parent)
} }
$bundle->save(); $bundle->save();
$this->line(sprintf('Created intermediary bundle %s', $bundle->getPath()));
} }
$this->createIntermediateBundles($bundle->getPath()); $this->createIntermediateBundles($bundle->getPath());
} }
} }
/**
* Repair bundles
*/
private function repair()
{
progress('Repairing bundles...', $this->bundles, function (Bundle $bundle, $progress) {
$this->handleBundle($bundle, $progress);
});
}
/**
* Repair specific bundle
*/
private function handleBundle(Bundle $bundle, $progress)
{
$bundle->repair();
$progress->label(sprintf('Repaired %s', $bundle->getPath()));
}
} }

View File

@ -1,34 +0,0 @@
<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class Touch extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:touch { path : Path to the bundle }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update a bundle "lastModified" metadata';
/**
* Execute the console command.
*/
public function handle()
{
$bundle = new Bundle($this->argument('path'), Storage::disk(env('CONTENT_DISK')));
$bundle->touch();
}
}

View File

@ -14,7 +14,7 @@ trait ReadsBundles
protected function selectBundles(): self protected function selectBundles(): self
{ {
$path = $this->argument('path') ?? '/'; $path = $this->argument('path') ?? '/';
$comment = sprintf('Validating <info>%s</info>', $path); $comment = sprintf('Reading <info>%s</info>', $path);
if ($this->option('recursive')) { if ($this->option('recursive')) {
$comment .= ' and <info>all sub-bundles</info>'; $comment .= ' and <info>all sub-bundles</info>';

View File

@ -9,10 +9,25 @@ trait SelectsDisks
{ {
protected Filesystem $sourceDisk; protected Filesystem $sourceDisk;
protected Filesystem $targetDisk;
/** /**
* Select the disk we will be working on * Select the disk we will be working on
*/ */
private function selectDisk(): self private function selectDisk(?bool $source = true, ?bool $target = false): self
{
if ($source) {
$this->selectSourceDisk();
}
if ($target) {
$this->selectTargetDisk();
}
return $this;
}
private function selectSourceDisk()
{ {
$sourceDisk = $this->option('source-disk') ?? env('CONTENT_DISK'); $sourceDisk = $this->option('source-disk') ?? env('CONTENT_DISK');
@ -27,4 +42,20 @@ private function selectDisk(): self
return $this; return $this;
} }
private function selectTargetDisk()
{
$targetDisk = $this->option('target-disk') ?? env('DIST_DISK');
$this->targetDisk = Storage::disk($targetDisk);
$this->line(
sprintf(
'Using <info>%s</info> as target disk',
$targetDisk
)
);
return $this;
}
} }

View File

@ -33,9 +33,9 @@ class Update extends Command
public function __construct() public function __construct()
{ {
$this->signature = 'bundle:update $this->signature = 'bundle:update
{ --r|recursive : Also validate sub-bundles } { --r|recursive : Also update sub-bundles }
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> } { --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to validate - Default to <info>/</info> } { path? : Path to a specific bundle to update - Default to <info>/</info> }
'; ';
parent::__construct(); parent::__construct();
@ -55,7 +55,7 @@ public function handle()
/** /**
* Updates specific bundle * Updates specific bundle
*/ */
protected function handleBundle(Bundle $bundle, $progress) private function handleBundle(Bundle $bundle, $progress)
{ {
$progress->hint(sprintf('Updated %s', $bundle->getPath())); $progress->hint(sprintf('Updated %s', $bundle->getPath()));

View File

@ -3,17 +3,16 @@
namespace App\Console\Commands\Bundle; namespace App\Console\Commands\Bundle;
use App\Classes\Bundle; use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use function Laravel\Prompts\progress;
class Upgrade extends Command class Upgrade extends Command
{ {
/** use ReadsBundles;
* The name and signature of the console command. use SelectsDisks;
*
* @var string
*/
protected $signature = 'bundle:upgrade { path? : Specific bundle to upgrade }';
/** /**
* The console command description. * The console command description.
@ -22,30 +21,36 @@ class Upgrade extends Command
*/ */
protected $description = 'Upgrade bundles from previous CMS version'; protected $description = 'Upgrade bundles from previous CMS version';
public function __construct()
{
$this->signature = 'bundle:upgrade
{ --r|recursive : Also upgrade sub-bundles }
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to upgrade - Default to <info>/</info> }
';
parent::__construct();
}
/** /**
* Execute the console command. * Execute the console command.
*/ */
public function handle() public function handle()
{ {
$disk = Storage::disk(env('CONTENT_DISK')); $this->selectDisk()
$path = $this->argument('path') ?? '/'; ->selectBundles()
$bundles = Bundle::findBundles($disk, $path, true); ->upgrade();
}
foreach ($bundles as $bundle) { private function upgrade()
$this->output->write(sprintf('Upgrading %s... ', $bundle->getPath())); {
progress('Upgrading bundles...', $this->bundles, function (Bundle $bundle, $progress) {
$this->handleBundle($bundle, $progress);
});
}
$bundle->metadata()->remove('lastModified'); private function handleBundle(Bundle $bundle, $progress)
$bundle->metadata()->remove('lastRendered'); {
//
$bundle->metadata('links')->clear();
$bundle->metadata('links')->save();
$bundle->metadata('rebrickable')->clear();
$bundle->metadata('rebrickable')->save();
$bundle->save();
$this->info('OK');
}
} }
} }

View File

@ -3,11 +3,11 @@
namespace App\Console\Commands\Bundle; namespace App\Console\Commands\Bundle;
use App\Classes\Bundle; use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use App\Services\HtmlValidator; use App\Services\HtmlValidator;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Vite; use Illuminate\Support\Facades\Vite;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use JsonSchema\Validator; use JsonSchema\Validator;
@ -16,6 +16,9 @@
class Validate extends Command class Validate extends Command
{ {
use ReadsBundles;
use SelectsDisks;
/** /**
* The console command description. * The console command description.
* *
@ -23,16 +26,12 @@ class Validate extends Command
*/ */
protected $description = 'Validate bundles metadata'; protected $description = 'Validate bundles metadata';
protected Filesystem $sourceDisk;
protected array $invalidJson = []; protected array $invalidJson = [];
protected array $invalidHtml = []; protected array $invalidHtml = [];
protected array $invalidCss = []; protected array $invalidCss = [];
protected $bundles;
public function __construct() public function __construct()
{ {
$this->signature = 'bundle:validate $this->signature = 'bundle:validate
@ -68,53 +67,6 @@ public function handle()
} }
} }
/**
* Select the disk we will be working on
*/
private function selectDisk(): self
{
$sourceDisk = $this->option('source-disk') ?? env('CONTENT_DISK');
$this->sourceDisk = Storage::disk($sourceDisk);
$this->line(
sprintf(
'Using <info>%s</info> as source disk',
$sourceDisk
)
);
return $this;
}
/**
* Collect a list of bundles to validate
*/
private function selectBundles(): self
{
$path = $this->argument('path') ?? '/';
$comment = sprintf('Validating <info>%s</info>', $path);
if ($this->option('recursive')) {
$comment .= ' and <info>all sub-bundles</info>';
}
$this->line($comment);
$this->output->write('Collecting bundles... ');
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($this->sourceDisk, $path, true);
} else {
$bundles = [new Bundle($path, $this->sourceDisk)];
}
$this->bundles = $bundles;
$this->info('OK');
return $this;
}
/** /**
* Return an associative array containing available bundles metadata files * Return an associative array containing available bundles metadata files
* as keys and json-decoded validation schemas as values * as keys and json-decoded validation schemas as values
@ -154,11 +106,7 @@ private function validateJson(): self
*/ */
private function handleBundleJsonValidation(Bundle $bundle, $progress, $validators) private function handleBundleJsonValidation(Bundle $bundle, $progress, $validators)
{ {
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
foreach ($validators as $metadataFilename => $schema) { foreach ($validators as $metadataFilename => $schema) {
$progress->hint(sprintf('Validating %s ...', $metadataFilename));
$filepath = $bundle->metadata($metadataFilename)->getFilename(); $filepath = $bundle->metadata($metadataFilename)->getFilename();
if (!$this->sourceDisk->exists($filepath)) { if (!$this->sourceDisk->exists($filepath)) {
@ -178,6 +126,8 @@ private function handleBundleJsonValidation(Bundle $bundle, $progress, $validato
]; ];
} }
} }
$progress->label(sprintf('Validated %s', $bundle->getPath()));
} }
private function validateCss(): self private function validateCss(): self
@ -234,8 +184,6 @@ private function validateHtml(): self
private function handleBundleHtmlValidation(Bundle $bundle, $progress) private function handleBundleHtmlValidation(Bundle $bundle, $progress)
{ {
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
$rendered = $bundle->render(); $rendered = $bundle->render();
$changed = false; $changed = false;
@ -259,6 +207,8 @@ private function handleBundleHtmlValidation(Bundle $bundle, $progress)
if ($changed) { if ($changed) {
$bundle->touch(); $bundle->touch();
} }
$progress->label(sprintf('Validated %s', $bundle->getPath()));
} }
/** /**