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;
use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use function Laravel\Prompts\confirm;
@ -15,6 +15,9 @@
class Render extends Command
{
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
*
@ -22,10 +25,6 @@ class Render extends Command
*/
protected $description = 'Render bundles';
protected Filesystem $sourceDisk;
protected Filesystem $targetDisk;
public function __construct()
{
$this->signature = 'bundle:render
@ -50,11 +49,12 @@ public function __construct()
public function handle()
{
$this->showDisclaimer()
->selectDisk()
->selectDisk(true, true)
->renderAssets()
->clearCache()
->validate()
->renderFeed()
->selectBundles()
->render()
->deploy();
}
@ -75,28 +75,6 @@ private function showDisclaimer(): self
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
*/
@ -147,7 +125,13 @@ private function validate(): self
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 (confirm('Validation errors have occurred. Cancel process?')) {
@ -158,32 +142,6 @@ private function validate(): self
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
{
$result = Bundle::renderFeed($this->sourceDisk);
@ -218,7 +176,7 @@ private function renderFeed(): self
*/
private function render(): self
{
$bundles = $this->getBundles();
$bundles = $this->bundles;
progress(
label: 'Rendering... ',

View File

@ -3,56 +3,56 @@
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use function Laravel\Prompts\progress;
class Repair extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:repair { --r|recursive : Also repair sub-bundles } { path? : Specific bundle to repair }';
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
*
* @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.
*/
public function handle()
{
$disk = Storage::disk(env('CONTENT_DISK'));
$path = $this->argument('path') ?? '/';
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');
}
$this->selectDisk()
->selectBundles()
->repair();
foreach (['blog', 'liens-interessants'] as $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) {
if (basename($dir) === 'data') {
@ -93,11 +93,29 @@ protected function createIntermediateBundles($parent)
}
$bundle->save();
$this->line(sprintf('Created intermediary bundle %s', $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
{
$path = $this->argument('path') ?? '/';
$comment = sprintf('Validating <info>%s</info>', $path);
$comment = sprintf('Reading <info>%s</info>', $path);
if ($this->option('recursive')) {
$comment .= ' and <info>all sub-bundles</info>';

View File

@ -9,10 +9,25 @@ trait SelectsDisks
{
protected Filesystem $sourceDisk;
protected Filesystem $targetDisk;
/**
* 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');
@ -27,4 +42,20 @@ private function selectDisk(): self
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()
{
$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> }
{ 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();
@ -55,7 +55,7 @@ public function handle()
/**
* Updates specific bundle
*/
protected function handleBundle(Bundle $bundle, $progress)
private function handleBundle(Bundle $bundle, $progress)
{
$progress->hint(sprintf('Updated %s', $bundle->getPath()));

View File

@ -3,17 +3,16 @@
namespace App\Console\Commands\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\Support\Facades\Storage;
use function Laravel\Prompts\progress;
class Upgrade extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:upgrade { path? : Specific bundle to upgrade }';
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
@ -22,30 +21,36 @@ class Upgrade extends Command
*/
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.
*/
public function handle()
{
$disk = Storage::disk(env('CONTENT_DISK'));
$path = $this->argument('path') ?? '/';
$bundles = Bundle::findBundles($disk, $path, true);
$this->selectDisk()
->selectBundles()
->upgrade();
}
foreach ($bundles as $bundle) {
$this->output->write(sprintf('Upgrading %s... ', $bundle->getPath()));
private function upgrade()
{
progress('Upgrading bundles...', $this->bundles, function (Bundle $bundle, $progress) {
$this->handleBundle($bundle, $progress);
});
}
$bundle->metadata()->remove('lastModified');
$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');
}
private function handleBundle(Bundle $bundle, $progress)
{
//
}
}

View File

@ -3,11 +3,11 @@
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use App\Services\HtmlValidator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\Str;
use JsonSchema\Validator;
@ -16,6 +16,9 @@
class Validate extends Command
{
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
*
@ -23,16 +26,12 @@ class Validate extends Command
*/
protected $description = 'Validate bundles metadata';
protected Filesystem $sourceDisk;
protected array $invalidJson = [];
protected array $invalidHtml = [];
protected array $invalidCss = [];
protected $bundles;
public function __construct()
{
$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
* as keys and json-decoded validation schemas as values
@ -154,11 +106,7 @@ private function validateJson(): self
*/
private function handleBundleJsonValidation(Bundle $bundle, $progress, $validators)
{
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
foreach ($validators as $metadataFilename => $schema) {
$progress->hint(sprintf('Validating %s ...', $metadataFilename));
$filepath = $bundle->metadata($metadataFilename)->getFilename();
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
@ -234,8 +184,6 @@ private function validateHtml(): self
private function handleBundleHtmlValidation(Bundle $bundle, $progress)
{
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
$rendered = $bundle->render();
$changed = false;
@ -259,6 +207,8 @@ private function handleBundleHtmlValidation(Bundle $bundle, $progress)
if ($changed) {
$bundle->touch();
}
$progress->label(sprintf('Validated %s', $bundle->getPath()));
}
/**