Skip to content

Commit

Permalink
feat(documentator): Command lara-asp-documentator:preprocess to pre…
Browse files Browse the repository at this point in the history
…process Markdown files.
  • Loading branch information
LastDragon-ru committed Aug 25, 2023
1 parent 4e67b4a commit 0fa1e6e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/documentator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"composer/semver": "^3.2",
"laravel/framework": "^9.21.0|^10.0.0",
"symfony/filesystem": "^6.3.0",
"symfony/finder": "^6.3.0",
"symfony/process": "^6.3.0",
"lastdragon-ru/lara-asp-core": "self.version",
"lastdragon-ru/lara-asp-serializer": "self.version"
Expand Down
55 changes: 55 additions & 0 deletions packages/documentator/src/Commands/Preprocess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Commands;

use Illuminate\Console\Command;
use LastDragon_ru\LaraASP\Core\Utils\Cast;
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Preprocessor;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Finder\Finder;

use function file_put_contents;
use function getcwd;

#[AsCommand(name: Preprocess::Name)]
class Preprocess extends Command {
public const Name = Package::Name.':preprocess';

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var string|null
*/
public $description = 'Preprocess Markdown files.';

/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
* @var string
*/
public $signature = self::Name.<<<'SIGNATURE'
{path? : directory to process}
SIGNATURE;

public function __invoke(Preprocessor $preprocessor): void {
$cwd = getcwd();
$path = Cast::toString($this->argument('path') ?? $cwd);
$finder = Finder::create()
->ignoreVCSIgnored(true)
->in($path)
->exclude('vendor')
->exclude('node_modules')
->files()
->name('*.md');

foreach ($finder as $file) {
$this->components->task($file->getPathname(), static function () use ($preprocessor, $file): bool {
$path = $file->getPath();
$content = $file->getContents();
$result = $preprocessor->process($path, $content);

return $content === $result
|| file_put_contents($file->getPathname(), $result) !== false;
});
}
}
}
2 changes: 2 additions & 0 deletions packages/documentator/src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithViews;
use LastDragon_ru\LaraASP\Documentator\Commands\Preprocess;
use LastDragon_ru\LaraASP\Documentator\Commands\Requirements;

class Provider extends ServiceProvider {
Expand All @@ -13,6 +14,7 @@ public function boot(): void {
$this->bootViews();
$this->commands(
Requirements::class,
Preprocess::class,
);
}

Expand Down

0 comments on commit 0fa1e6e

Please sign in to comment.