Skip to content

Commit

Permalink
Add --readme option to render to README (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 2, 2024
1 parent 30d9afa commit 60fba77
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
44 changes: 42 additions & 2 deletions src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<!-- ruledoc-start -->';

/**
* @var string
*/
private const README_PLACEHOLDER_END = '<!-- ruledoc-end -->';

public function __construct(
private readonly DirectoryToMarkdownPrinter $directoryToMarkdownPrinter,
private readonly SymfonyStyle $symfonyStyle,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/ValueObject/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ final class Option
* @var string
*/
public const SKIP_TYPE = 'skip-type';

/**
* @var string
*/
public const README = 'readme';
}

0 comments on commit 60fba77

Please sign in to comment.