-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(documentator): Command
lara-asp-documentator:preprocess
to pre…
…process Markdown files.
- Loading branch information
1 parent
4e67b4a
commit 0fa1e6e
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters