Skip to content

Commit 348c776

Browse files
committed
create nice symfony console command
1 parent 1543845 commit 348c776

File tree

6 files changed

+220
-46
lines changed

6 files changed

+220
-46
lines changed

_build/bin/console

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require __DIR__.'/../vendor/autoload.php';
5+
6+
use Symfony\Component\Console\Application;
7+
use SymfonyDocs\Command\ParseDoc;
8+
9+
$application = new Application();
10+
11+
$application->add(new ParseDoc());
12+
13+
$application->run();

_build/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"symfony/phpunit-bridge": "^4.1",
1616
"symfony/filesystem": "^4.1",
1717
"symfony/dom-crawler": "^4.1",
18-
"symfony/css-selector": "^4.1"
18+
"symfony/css-selector": "^4.1",
19+
"symfony/console": "^4.1"
1920
},
2021
"repositories": [
2122
{

_build/composer.lock

Lines changed: 73 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_build/parse.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

_build/src/Command/ParseDoc.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

_build/src/JsonGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\RST\HTML\Document;
66
use Doctrine\RST\HTML\Environment;
77
use Doctrine\RST\MetaEntry;
8+
use Symfony\Component\Console\Helper\ProgressBar;
89
use Symfony\Component\DomCrawler\Crawler;
910
use Symfony\Component\Filesystem\Filesystem;
1011
use Symfony\Component\Finder\Finder;
@@ -27,7 +28,7 @@ function (Document $document) {
2728
);
2829
}
2930

30-
public function generateJson(string $inputDir, string $outputDir)
31+
public function generateJson(string $inputDir, string $outputDir, ProgressBar $progressBar)
3132
{
3233
$finder = new Finder();
3334
$finder->in($inputDir)
@@ -60,7 +61,11 @@ public function generateJson(string $inputDir, string $outputDir)
6061
str_replace([$inputDir, '.html'], [$outputDir, '.json'], $file->getRealPath()),
6162
json_encode($data, JSON_PRETTY_PRINT)
6263
);
64+
65+
$progressBar->advance();
6366
}
67+
68+
$progressBar->finish();
6469
}
6570

6671
private function getParserFilename(string $filePath, string $inputDir): string

0 commit comments

Comments
 (0)