Skip to content

Commit a6a37e4

Browse files
committed
sanitize html and json directories: remove files that should not exist which will pollute the build
1 parent abc60e6 commit a6a37e4

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Command/CommandInitializerTrait.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private function initializeParseSubPath(InputInterface $input, string $sourceDir
105105

106106
private function initializeJsonOutputDir(string $outputDir): string
107107
{
108-
$jsonOutputDir = $this->getRealAbsolutePath($outputDir.'/json', $this->filesystem);
108+
$jsonOutputDir = $this->getRealAbsolutePath($outputDir.'/_json', $this->filesystem);
109109
if ($this->isCacheDisabled() && $this->filesystem->exists($jsonOutputDir)) {
110110
$this->filesystem->remove($jsonOutputDir);
111111
}
@@ -143,6 +143,8 @@ private function startBuild()
143143
->notName('*.rst.inc')
144144
->name('*.rst');
145145

146+
$this->sanitizeOutputDirs($this->finder);
147+
146148
$this->io->note(sprintf('Start parsing %d rst files', $this->finder->count()));
147149
$this->progressBar = new ProgressBar($this->output, $this->finder->count());
148150

@@ -152,6 +154,52 @@ private function startBuild()
152154
);
153155
}
154156

157+
/**
158+
* Removes all existing html files in the output dir that should not exist
159+
* because previous build in the same output directory has been executed on another version
160+
*/
161+
private function sanitizeOutputDirs(Finder $finder)
162+
{
163+
if (!$this->filesystem->exists($this->buildContext->getHtmlOutputDir())) {
164+
return;
165+
}
166+
167+
$rstFiles = array_map(
168+
function (string $rstFile) {
169+
return str_replace([$this->buildContext->getSourceDir(), '.rst'], '', $rstFile);
170+
},
171+
array_keys(iterator_to_array($finder))
172+
);
173+
174+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getHtmlOutputDir(), 'html');
175+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getJsonOutputDir(), 'json');
176+
}
177+
178+
private function sanitizeOutputDir(array $existingRstFiles, string $outputDir, string $format)
179+
{
180+
$htmlFinder = new Finder();
181+
$htmlFinder->in($outputDir)
182+
->name('*.html');
183+
184+
$htmlFiles = array_map(
185+
function (string $htmlFile) use ($outputDir, $format) {
186+
return str_replace([$outputDir, '.'.$format], '', $htmlFile);
187+
},
188+
array_keys(iterator_to_array($htmlFinder))
189+
);
190+
191+
$filesNotExistingInCurrentVersion = array_map(
192+
function ($file) use ($outputDir, $format) {
193+
return sprintf('%s%s.%s', $outputDir, $file, $format);
194+
},
195+
array_values(array_diff($htmlFiles, $existingRstFiles))
196+
);
197+
198+
foreach ($filesNotExistingInCurrentVersion as $file) {
199+
$this->filesystem->remove($file);
200+
}
201+
}
202+
155203
private function isCacheDisabled(): bool
156204
{
157205
return (bool) $this->input->getOption('disable-cache');

src/Generator/JsonGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function generateJson(
3333
$fs->remove($buildContext->getJsonOutputDir());
3434

3535
foreach ($finder as $file) {
36+
37+
3638
$crawler = new Crawler($file->getContents());
3739

3840
$parserFilename = $this->getParserFilename($file->getRealPath(), $buildContext->getHtmlOutputDir());

0 commit comments

Comments
 (0)