Skip to content

Commit 9360dcd

Browse files
committed
Include all required options for a successful symfony-docs build
1 parent 293c45d commit 9360dcd

File tree

3 files changed

+52
-68
lines changed

3 files changed

+52
-68
lines changed

bin/docs-builder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ foreach ($autoloadFiles as $autoloadFile) {
1616
use Symfony\Component\Console\Input\ArgvInput;
1717
use SymfonyDocsBuilder\Application;
1818

19-
$input = new ArgvInput();
19+
$input = new ArgvInput();
2020
$version = $input->getParameterOption(['--symfony-version'], false === getenv('SYMFONY_VERSION') ? 'master' : getenv('SYMFONY_VERSION'));
2121

2222
if (!$version) {

src/Command/BuildDocsCommand.php

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,93 +49,77 @@ public function __construct(BuildConfig $buildConfig)
4949

5050
protected function configure()
5151
{
52-
parent::configure();
53-
5452
$this
5553
->addArgument('source-dir', InputArgument::OPTIONAL, 'RST files Source directory', getcwd())
56-
->addArgument('output-dir', InputArgument::OPTIONAL, 'HTML files output directory')
57-
->addOption(
58-
'parse-sub-path',
59-
null,
60-
InputOption::VALUE_OPTIONAL,
61-
'Parse only given sub directory and combine it into a single file (directory relative from source-dir)',
62-
''
63-
)
64-
->addOption(
65-
'output-json',
66-
null,
67-
InputOption::VALUE_NONE,
68-
'If provided, .fjson metadata files will be written'
69-
)
70-
->addOption(
71-
'disable-cache',
72-
null,
73-
InputOption::VALUE_NONE,
74-
'If provided, caching meta will be disabled'
75-
)
76-
->addOption(
77-
'save-errors',
78-
null,
79-
InputOption::VALUE_REQUIRED,
80-
'Path where any errors should be saved'
81-
)
82-
->addOption(
83-
'error-output-format',
84-
null,
85-
InputOption::VALUE_REQUIRED,
86-
'The output format for errors on std out',
87-
Configuration::OUTPUT_FORMAT_CONSOLE
88-
)
89-
->addOption(
90-
'no-theme',
91-
null,
92-
InputOption::VALUE_NONE,
93-
'Use the default theme instead of the styled one'
94-
)
95-
->addOption(
96-
'fail-on-errors',
97-
null,
98-
InputOption::VALUE_NONE,
99-
'Return a non-zero code if there are errors/warnings'
100-
)
54+
->addArgument('output-dir', InputArgument::OPTIONAL, 'HTML files output directory', rtrim(getcwd(), '/').'/html')
55+
56+
->addOption('images-dir', null, InputOption::VALUE_REQUIRED, 'Images files output directory')
57+
->addOption('config', null, InputOption::VALUE_REQUIRED, 'Path to the config file')
58+
59+
->addOption('disable-cache', null, InputOption::VALUE_NONE, 'If provided, caching meta will be disabled')
60+
->addOption('parse-sub-path', null, InputOption::VALUE_REQUIRED, 'Parse only given sub directory and combine it into a single file (directory relative from source-dir)', '')
61+
->addOption('output-json', null, InputOption::VALUE_NONE, 'If provided, .fjson metadata files will be written')
62+
->addOption('no-theme', null, InputOption::VALUE_NONE, 'Use the default theme instead of the styled one')
63+
64+
->addOption('fail-on-errors', null, InputOption::VALUE_NONE, 'Return a non-zero code if there are errors/warnings')
65+
->addOption('save-errors', null, InputOption::VALUE_REQUIRED, 'Path where any errors should be saved')
66+
->addOption('error-output-format', null, InputOption::VALUE_REQUIRED, 'The output format for errors on std out', Configuration::OUTPUT_FORMAT_CONSOLE)
10167
;
10268
}
10369

10470
protected function initialize(InputInterface $input, OutputInterface $output)
10571
{
72+
$filesystem = new Filesystem();
10673
$this->io = new SymfonyStyle($input, $output);
10774

75+
if ($input->getOption('disable-cache')) {
76+
$this->buildConfig->disableBuildCache();
77+
}
78+
10879
$sourceDir = $input->getArgument('source-dir');
109-
if (!file_exists($sourceDir)) {
80+
if (!$filesystem->exists($sourceDir)) {
11081
throw new \InvalidArgumentException(sprintf('RST source directory "%s" does not exist', $sourceDir));
11182
}
11283
$this->buildConfig->setContentDir($sourceDir);
11384

114-
$filesystem = new Filesystem();
115-
$htmlOutputDir = $input->getArgument('output-dir') ?? rtrim(getcwd(), '/').'/html';
116-
if ($input->getOption('disable-cache') && $filesystem->exists($htmlOutputDir)) {
85+
$htmlOutputDir = $input->getArgument('output-dir');
86+
if (!$this->buildConfig->isBuildCacheEnabled() && $filesystem->exists($htmlOutputDir)) {
11787
$filesystem->remove($htmlOutputDir);
11888
}
11989
$filesystem->mkdir($htmlOutputDir);
12090
$this->buildConfig->setOutputDir($htmlOutputDir);
12191

92+
$imgOutputDir = $input->getOption('images-dir');
93+
if ($imgOutputDir) {
94+
if (!$this->buildConfig->isBuildCacheEnabled() && $filesystem->exists($imgOutputDir)) {
95+
$filesystem->remove($imgOutputDir);
96+
}
97+
$this->buildConfig->setImageDir($imgOutputDir);
98+
}
99+
122100
$parseSubPath = $input->getOption('parse-sub-path');
123101
if ($parseSubPath && $input->getOption('output-json')) {
124102
throw new \InvalidArgumentException('Cannot pass both --parse-sub-path and --output-json options.');
125103
}
126-
if (!file_exists($sourceDir.'/'.$parseSubPath)) {
104+
if (!$filesystem->exists($sourceDir.'/'.$parseSubPath)) {
127105
throw new \InvalidArgumentException(sprintf('Given "parse-sub-path" directory "%s" does not exist', $parseSubPath));
128106
}
129107
$this->buildConfig->setSubdirectoryToBuild($parseSubPath);
130108

131-
if ($input->getOption('disable-cache')) {
132-
$this->buildConfig->disableBuildCache();
109+
$this->buildConfig->setTheme($input->getOption('no-theme') ? Configuration::THEME_DEFAULT : 'rtd');
110+
111+
$configFileParser = new ConfigFileParser($this->buildConfig);
112+
$configPath = $input->getOption('config');
113+
if ($configPath && !file_exists($configPath)) {
114+
throw new \RuntimeException(sprintf('No config file present at <info>%s</info>', $configPath));
133115
}
134116

135-
$this->buildConfig->setTheme($input->getOption('no-theme') ? Configuration::THEME_DEFAULT : 'rtd');
117+
if (!$configPath && !file_exists($configPath = $sourceDir.'/docs.json')) {
118+
return;
119+
}
136120

137-
$configFileParser = new ConfigFileParser($this->buildConfig, $output);
138-
$configFileParser->processConfigFile($sourceDir);
121+
$this->io->writeln(sprintf('Loading config file: <info>%s</info>', $configPath));
122+
$configFileParser->processConfigFile($configPath);
139123
}
140124

141125
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -146,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
146130

147131
$configuration = $builder->getConfiguration();
148132
$configuration->setOutputFormat($input->getOption('error-output-format'));
133+
$configuration->silentOnError(false);
149134
$this->addProgressListener($configuration->getEventManager());
150135

151136
$builder->build(
@@ -184,7 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
184169
$this->io->success('Build completed with warnings');
185170

186171
if ($input->getOption('fail-on-errors')) {
187-
return 1;
172+
return Command::FAILURE;
188173
}
189174
} else {
190175
$this->io->success('Build completed successfully!');

src/ConfigFileParser.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@
22

33
namespace SymfonyDocsBuilder;
44

5-
use Symfony\Component\Console\Output\OutputInterface;
6-
75
/**
86
* Parses the docs.json config file
97
*/
108
class ConfigFileParser
119
{
1210
private $buildConfig;
13-
private $output;
1411

15-
public function __construct(BuildConfig $buildConfig, OutputInterface $output)
12+
public function __construct(BuildConfig $buildConfig)
1613
{
1714
$this->buildConfig = $buildConfig;
18-
$this->output = $output;
1915
}
2016

21-
public function processConfigFile(string $sourceDir): void
17+
public function processConfigFile(string $configPath): void
2218
{
23-
$configPath = $sourceDir.'/docs.json';
2419
if (!file_exists($configPath)) {
25-
$this->output->writeln(sprintf('No config file present at <info>%s</info>', $configPath));
20+
throw new \RuntimeException(sprintf('No config file present at <info>%s</info>', $configPath));
2621

2722
return;
2823
}
2924

30-
$this->output->writeln(sprintf('Loading config file: <info>%s</info>', $configPath));
3125
$configData = json_decode(file_get_contents($configPath), true);
3226

3327
$exclude = $configData['exclude'] ?? [];
3428
$this->buildConfig->setExcludedPaths($exclude);
3529
unset($configData['exclude']);
3630

31+
if ($sfVersion = $configData['symfony-version'] ?? false) {
32+
$this->buildConfig->setSymfonyVersion($sfVersion);
33+
}
34+
unset($configData['symfony-version']);
35+
3736
if (count($configData) > 0) {
3837
throw new \Exception(sprintf('Unsupported keys in docs.json: %s', implode(', ', array_keys($configData))));
3938
}

0 commit comments

Comments
 (0)