|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace SymfonyDocs\Command; |
| 4 | + |
| 5 | +use Doctrine\RST\Builder; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Helper\ProgressBar; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Input\InputOption; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 12 | +use Symfony\Component\Filesystem\Filesystem; |
| 13 | +use Symfony\Component\Finder\Finder; |
| 14 | +use SymfonyDocs\HtmlKernel; |
| 15 | +use SymfonyDocs\JsonGenerator; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class ParseDoc |
| 19 | + */ |
| 20 | +class ParseDoc extends Command |
| 21 | +{ |
| 22 | + protected static $defaultName = 'symfony-docs:parse'; |
| 23 | + |
| 24 | + /** @var SymfonyStyle */ |
| 25 | + private $io; |
| 26 | + private $filesystem; |
| 27 | + private $finder; |
| 28 | + /** @var ProgressBar */ |
| 29 | + private $progressBar; |
| 30 | + private $sourceDir; |
| 31 | + private $htmlOutputDir; |
| 32 | + private $jsonOutputDir; |
| 33 | + |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + parent::__construct(self::$defaultName); |
| 37 | + |
| 38 | + $this->filesystem = new Filesystem(); |
| 39 | + $this->finder = new Finder(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + parent::configure(); |
| 45 | + |
| 46 | + $this |
| 47 | + ->addOption('source-dir', null, InputOption::VALUE_REQUIRED, 'RST files Source directory', __DIR__.'/../../..') |
| 48 | + ->addOption('html-output-dir', null, InputOption::VALUE_REQUIRED, 'HTML files output directory', __DIR__.'/../../html') |
| 49 | + ->addOption('json-output-dir', null, InputOption::VALUE_REQUIRED, 'JSON files output directory', __DIR__.'/../../json') |
| 50 | + ; |
| 51 | + } |
| 52 | + |
| 53 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 54 | + { |
| 55 | + $this->io = new SymfonyStyle($input, $output); |
| 56 | + |
| 57 | + $this->sourceDir = $this->getRealAbsolutePath($input->getOption('source-dir')); |
| 58 | + if (!$this->filesystem->exists($this->sourceDir)) { |
| 59 | + throw new \InvalidArgumentException(sprintf('RST source directory "%s" does not exist', $this->sourceDir)); |
| 60 | + } |
| 61 | + |
| 62 | + $this->htmlOutputDir = $this->getRealAbsolutePath($input->getOption('html-output-dir')); |
| 63 | + if ($this->filesystem->exists($this->htmlOutputDir)) { |
| 64 | + $this->filesystem->remove($this->htmlOutputDir); |
| 65 | + } |
| 66 | + |
| 67 | + $this->jsonOutputDir = $this->getRealAbsolutePath($input->getOption('json-output-dir')); |
| 68 | + if ($this->filesystem->exists($this->jsonOutputDir)) { |
| 69 | + $this->filesystem->remove($this->jsonOutputDir); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 74 | + { |
| 75 | + $kernel = new HtmlKernel(); |
| 76 | + $builder = new Builder($kernel); |
| 77 | + $builder->addHook([$this, 'handleProgressBar']); |
| 78 | + |
| 79 | + $this->finder->in($input->getOption('source-dir')) |
| 80 | + ->exclude(['_build', '.github', '.platform', '_images']) |
| 81 | + ->notName('*.rst.inc') |
| 82 | + ->name('*.rst'); |
| 83 | + |
| 84 | + $this->io->note(sprintf('Start parsing into html %d rst files', $this->finder->count())); |
| 85 | + $this->progressBar = new ProgressBar($output, $this->finder->count()); |
| 86 | + |
| 87 | + $builder->build( |
| 88 | + $this->sourceDir, |
| 89 | + $this->htmlOutputDir |
| 90 | + ); |
| 91 | + |
| 92 | + $this->progressBar->finish(); |
| 93 | + $this->io->newLine(2); |
| 94 | + $this->io->success('Parse into html complete'); |
| 95 | + |
| 96 | + foreach ($this->finder as $file) { |
| 97 | + $htmlFile = str_replace([$this->sourceDir, '.rst'], [$this->htmlOutputDir, '.html'], $file->getRealPath()); |
| 98 | + if (!$this->filesystem->exists($htmlFile)) { |
| 99 | + $this->io->warning(sprintf('Missing file "%s"', $htmlFile)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $this->io->note('Start transforming doc into json files'); |
| 104 | + $this->progressBar = new ProgressBar($output, $this->finder->count()); |
| 105 | + $jsonGenerator = new JsonGenerator($builder->getDocuments()->getAll()); |
| 106 | + $jsonGenerator->generateJson($this->htmlOutputDir, $this->jsonOutputDir, $this->progressBar); |
| 107 | + $this->io->newLine(2); |
| 108 | + $this->io->success('Parse process complete'); |
| 109 | + } |
| 110 | + |
| 111 | + public function handleProgressBar() |
| 112 | + { |
| 113 | + $this->progressBar->advance(); |
| 114 | + } |
| 115 | + |
| 116 | + private function getRealAbsolutePath(string $path): string |
| 117 | + { |
| 118 | + return sprintf( |
| 119 | + '/%s', |
| 120 | + rtrim( |
| 121 | + $this->filesystem->makePathRelative($path, '/'), |
| 122 | + '/' |
| 123 | + ) |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
0 commit comments