-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathBuildDocsCommand.php
215 lines (182 loc) · 7.39 KB
/
BuildDocsCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <ryan@symfonycasts.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyDocsBuilder\Command;
use Doctrine\Common\EventManager;
use Doctrine\RST\Builder;
use Doctrine\RST\Configuration;
use Doctrine\RST\Meta\Metas;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use SymfonyDocsBuilder\BuildConfig;
use SymfonyDocsBuilder\CI\MissingFilesChecker;
use SymfonyDocsBuilder\ConfigFileParser;
use SymfonyDocsBuilder\Generator\HtmlForPdfGenerator;
use SymfonyDocsBuilder\Generator\JsonGenerator;
use SymfonyDocsBuilder\KernelFactory;
use SymfonyDocsBuilder\Listener\BuildProgressListener;
class BuildDocsCommand extends Command
{
protected static $defaultName = 'build:docs';
private $buildConfig;
private $missingFilesChecker;
/** @var SymfonyStyle */
private $io;
public function __construct(BuildConfig $buildConfig)
{
parent::__construct(self::$defaultName);
$this->buildConfig = $buildConfig;
$this->missingFilesChecker = new MissingFilesChecker($buildConfig);
}
protected function configure()
{
parent::configure();
$this
->addArgument('source-dir', InputArgument::OPTIONAL, 'RST files Source directory', getcwd())
->addArgument('output-dir', InputArgument::OPTIONAL, 'HTML files output directory')
->addOption(
'parse-sub-path',
null,
InputOption::VALUE_OPTIONAL,
'Parse only given sub directory and combine it into a single file (directory relative from source-dir)',
''
)
->addOption(
'output-json',
null,
InputOption::VALUE_NONE,
'If provided, .fjson metadata files will be written'
)
->addOption(
'disable-cache',
null,
InputOption::VALUE_NONE,
'If provided, caching meta will be disabled'
)
->addOption(
'save-errors',
null,
InputOption::VALUE_REQUIRED,
'Path where any errors should be saved'
)
->addOption(
'disable-parse-errors-on-stdout',
null,
InputOption::VALUE_NONE,
'Don\'t print errors to standard output'
)
->addOption(
'no-theme',
null,
InputOption::VALUE_NONE,
'Use the default theme instead of the styled one'
)
->addOption(
'fail-on-errors',
null,
InputOption::VALUE_NONE,
'Return a non-zero code if there are errors/warnings'
)
;
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$sourceDir = $input->getArgument('source-dir');
if (!file_exists($sourceDir)) {
throw new \InvalidArgumentException(sprintf('RST source directory "%s" does not exist', $sourceDir));
}
$this->buildConfig->setContentDir($sourceDir);
$filesystem = new Filesystem();
$htmlOutputDir = $input->getArgument('output-dir') ?? rtrim(getcwd(), '/').'/html';
if ($input->getOption('disable-cache') && $filesystem->exists($htmlOutputDir)) {
$filesystem->remove($htmlOutputDir);
}
$filesystem->mkdir($htmlOutputDir);
$this->buildConfig->setOutputDir($htmlOutputDir);
$parseSubPath = $input->getOption('parse-sub-path');
if ($parseSubPath && $input->getOption('output-json')) {
throw new \InvalidArgumentException('Cannot pass both --parse-sub-path and --output-json options.');
}
if (!file_exists($sourceDir.'/'.$parseSubPath)) {
throw new \InvalidArgumentException(sprintf('Given "parse-sub-path" directory "%s" does not exist', $parseSubPath));
}
$this->buildConfig->setSubdirectoryToBuild($parseSubPath);
if ($input->getOption('disable-cache')) {
$this->buildConfig->disableBuildCache();
}
$this->buildConfig->setTheme($input->getOption('no-theme') ? Configuration::THEME_DEFAULT : 'rtd');
$configFileParser = new ConfigFileParser($this->buildConfig, $output);
$configFileParser->processConfigFile($sourceDir);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$builder = new Builder(
KernelFactory::createKernel($this->buildConfig, $this->urlChecker ?? null)
);
$configuration = $builder->getConfiguration();
$configuration->silentOnError($input->getOption('disable-parse-errors-on-stdout'));
$this->addProgressListener($configuration->getEventManager());
$builder->build(
$this->buildConfig->getContentDir(),
$this->buildConfig->getOutputDir()
);
$buildErrors = $builder->getErrorManager()->getErrors();
$missingFiles = $this->missingFilesChecker->getMissingFiles();
foreach ($missingFiles as $missingFile) {
$message = sprintf('Missing file "%s"', $missingFile);
$buildErrors[] = $message;
$this->io->warning($message);
}
if ($logPath = $input->getOption('save-errors')) {
if (\count($buildErrors) > 0) {
array_unshift($buildErrors, sprintf('Build errors from "%s"', date('Y-m-d h:i:s')));
}
$filesystem = new Filesystem();
$filesystem->dumpFile($logPath, implode("\n", $buildErrors));
}
$metas = $builder->getMetas();
if ($this->buildConfig->getSubdirectoryToBuild()) {
$this->renderDocForPDF($metas);
} elseif ($input->getOption('output-json')) {
$this->generateJson($metas);
}
$this->io->newLine(2);
if (\count($buildErrors) > 0) {
$this->io->success('Build completed with warnings');
if ($input->getOption('fail-on-errors')) {
return 1;
}
} else {
$this->io->success('Build completed successfully!');
}
return Command::SUCCESS;
}
private function generateJson(Metas $metas)
{
$this->io->note('Start exporting doc into json files');
$jsonGenerator = new JsonGenerator($metas, $this->buildConfig);
$jsonGenerator->setOutput($this->io);
$jsonGenerator->generateJson();
}
private function renderDocForPDF(Metas $metas)
{
$htmlForPdfGenerator = new HtmlForPdfGenerator($metas, $this->buildConfig);
$htmlForPdfGenerator->generateHtmlForPdf();
}
private function addProgressListener(EventManager $eventManager)
{
$progressListener = new BuildProgressListener($this->io);
$progressListener->attachListeners($eventManager);
}
}