From ddfd4c84269bd5c28aa5e5faafbe842f77cd2251 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 2 Jun 2024 21:30:12 +0900 Subject: [PATCH] add readme render --- composer.json | 16 ++++++------ src/Command/GenerateCommand.php | 44 +++++++++++++++++++++++++++++++-- src/ValueObject/Option.php | 5 ++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 6dba39c..9827d43 100644 --- a/composer.json +++ b/composer.json @@ -8,20 +8,20 @@ "require": { "php": ">=8.2", "symfony/console": "^6.4", - "nette/robot-loader": "^3.4|^4.0", + "nette/robot-loader": "^4.0", "symplify/rule-doc-generator-contracts": "^11.1", - "nette/utils": "^3.2|^4.0", - "sebastian/diff": "^5.1|^6.0", - "illuminate/container": "^10.39|^11.0", + "nette/utils": "^4.0", + "sebastian/diff": "^6.0", + "illuminate/container": "^11.0", "webmozart/assert": "^1.11", - "symfony/yaml": "^6.4", - "symfony/filesystem": "^6.4" + "symfony/yaml": "^7.0", + "symfony/filesystem": "^7.0" }, "require-dev": { "phpunit/phpunit": "^11.0", - "phpstan/phpstan": "^1.10.56", + "phpstan/phpstan": "^1.11", "symplify/easy-coding-standard": "^12.0", - "rector/rector": "dev-main", + "rector/rector": "^1.1", "tracy/tracy": "^2.10", "tomasvotruba/class-leak": "^0.2" }, diff --git a/src/Command/GenerateCommand.php b/src/Command/GenerateCommand.php index 75a6184..a34d087 100644 --- a/src/Command/GenerateCommand.php +++ b/src/Command/GenerateCommand.php @@ -13,9 +13,20 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symplify\RuleDocGenerator\DirectoryToMarkdownPrinter; use Symplify\RuleDocGenerator\ValueObject\Option; +use Webmozart\Assert\Assert; final class GenerateCommand extends Command { + /** + * @var string + */ + private const README_PLACEHOLDER_START = ''; + + /** + * @var string + */ + private const README_PLACEHOLDER_END = ''; + public function __construct( private readonly DirectoryToMarkdownPrinter $directoryToMarkdownPrinter, private readonly SymfonyStyle $symfonyStyle, @@ -45,6 +56,7 @@ protected function configure(): void $this->addOption(Option::CATEGORIZE, null, InputOption::VALUE_REQUIRED, 'Group rules by namespace position'); $this->addOption(Option::SKIP_TYPE, null, InputOption::VALUE_REQUIRED, 'Skip specific type in filter'); + $this->addOption(Option::README, null, InputOption::VALUE_REQUIRED, 'Render contents to README using placeholders'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -74,10 +86,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int $skipTypes ); - FileSystem::write($outputFilePath, $markdownFileContent); + $isReadme = (bool) $input->getOption(Option::README); - $this->symfonyStyle->success(sprintf('File "%s" was created', $outputFilePath)); + if ($isReadme) { + $this->renderToReadme($markdownFileContent); + } else { + FileSystem::write($outputFilePath, $markdownFileContent); + $this->symfonyStyle->success(\sprintf('File "%s" was created', $outputFilePath)); + } return self::SUCCESS; } + + private function renderToReadme(string $markdownFileContent): void + { + $readmeFilepath = getcwd() . '/README.md'; + Assert::fileExists($readmeFilepath); + + $readmeContents = FileSystem::read($readmeFilepath); + + Assert::contains($readmeContents, self::README_PLACEHOLDER_START); + Assert::contains($readmeContents, self::README_PLACEHOLDER_END); + + /** @var string $readmeContents */ + $readmeContents = preg_replace( + '#' . preg_quote(self::README_PLACEHOLDER_START, '#') . '(.*?)' . + preg_quote(self::README_PLACEHOLDER_END, '#') . '#s', + self::README_PLACEHOLDER_START . PHP_EOL . $markdownFileContent . PHP_EOL . self::README_PLACEHOLDER_END, + $readmeContents + ); + + FileSystem::write($readmeFilepath, $readmeContents); + $this->symfonyStyle->success('README.md was updated'); + $this->symfonyStyle->newLine(); + } } diff --git a/src/ValueObject/Option.php b/src/ValueObject/Option.php index 99cf134..37b943d 100644 --- a/src/ValueObject/Option.php +++ b/src/ValueObject/Option.php @@ -25,4 +25,9 @@ final class Option * @var string */ public const SKIP_TYPE = 'skip-type'; + + /** + * @var string + */ + public const README = 'readme'; }