Skip to content

Commit 01d7e92

Browse files
authored
Merge pull request #22 from weaverryan/fix-cache-issue
Fix cache issue
2 parents 39aa707 + 0203250 commit 01d7e92

File tree

11 files changed

+254
-149
lines changed

11 files changed

+254
-149
lines changed

src/BuildContext.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class BuildContext
1515
private $htmlOutputDir;
1616
private $jsonOutputDir;
1717
private $parseSubPath;
18+
private $disableCache = false;
1819

1920
public function __construct(
2021
string $basePath,
@@ -30,12 +31,13 @@ public function __construct(
3031
$this->symfonyDocUrl = $symfonyDocUrl;
3132
}
3233

33-
public function initializeRuntimeConfig(string $sourceDir, string $htmlOutputDir, ?string $jsonOutputDir = null, ?string $parseSubPath = null)
34+
public function initializeRuntimeConfig(string $sourceDir, string $htmlOutputDir, ?string $jsonOutputDir = null, ?string $parseSubPath = null, bool $disableCache = false)
3435
{
3536
$this->sourceDir = $sourceDir;
3637
$this->htmlOutputDir = $htmlOutputDir;
3738
$this->jsonOutputDir = $jsonOutputDir;
3839
$this->parseSubPath = $parseSubPath;
40+
$this->disableCache = $disableCache;
3941
$this->runtimeInitialized = true;
4042
}
4143

@@ -92,6 +94,13 @@ public function getParseSubPath(): ?string
9294
return $this->parseSubPath;
9395
}
9496

97+
public function getDisableCache(): bool
98+
{
99+
$this->checkThatRuntimeConfigIsInitialized();
100+
101+
return $this->disableCache;
102+
}
103+
95104
private function checkThatRuntimeConfigIsInitialized()
96105
{
97106
if (false === $this->runtimeInitialized) {

src/Command/BuildDocsCommand.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace SymfonyDocsBuilder\Command;
44

55
use Doctrine\RST\Event\PostBuildRenderEvent;
6+
use Doctrine\RST\Meta\CachedMetasLoader;
7+
use Doctrine\RST\Meta\Metas;
68
use Symfony\Component\Console\Command\Command;
79
use Symfony\Component\Console\Helper\ProgressBar;
810
use Symfony\Component\Console\Input\InputArgument;
@@ -49,6 +51,12 @@ protected function configure()
4951
InputOption::VALUE_OPTIONAL,
5052
'Parse only given sub directory and combine it into a single file (directory relative from source-dir)',
5153
''
54+
)
55+
->addOption(
56+
'disable-cache',
57+
null,
58+
InputOption::VALUE_NONE,
59+
'If provided, caching meta will be disabled'
5260
);
5361
}
5462

@@ -74,29 +82,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
7482

7583
$this->missingFilesChecker->checkMissingFiles($this->io);
7684

85+
$metas = $this->getMetas();
7786
if (!$this->buildContext->getParseSubPath()) {
78-
$this->generateJson();
87+
$this->generateJson($metas);
7988
} else {
80-
$this->renderDocForPDF();
89+
$this->renderDocForPDF($metas);
8190
}
8291

8392
$this->io->newLine(2);
84-
$this->io->success('Parse process complete');
93+
94+
$successMessage = 'Parse process complete';
95+
96+
if (!$this->buildContext->getDisableCache()) {
97+
$successMessage = sprintf(
98+
'%s (%d files were loaded from cache)',
99+
$successMessage,
100+
$this->finder->count() - count($this->builder->getDocuments()->getAll())
101+
);
102+
}
103+
$this->io->success($successMessage);
85104
}
86105

87-
private function generateJson()
106+
private function generateJson(Metas $metas)
88107
{
89108
$this->io->note('Start exporting doc into json files');
90109
$this->progressBar = new ProgressBar($this->output, $this->finder->count());
91110

92-
$jsonGenerator = new JsonGenerator();
93-
$jsonGenerator->generateJson($this->builder->getDocuments()->getAll(), $this->buildContext, $this->progressBar);
111+
$jsonGenerator = new JsonGenerator($metas, $this->buildContext);
112+
$jsonGenerator->generateJson($this->progressBar);
94113
}
95114

96-
private function renderDocForPDF()
115+
private function renderDocForPDF(Metas $metas)
97116
{
98-
$htmlForPdfGenerator = new HtmlForPdfGenerator();
99-
$htmlForPdfGenerator->generateHtmlForPdf($this->builder->getDocuments()->getAll(), $this->buildContext);
117+
$htmlForPdfGenerator = new HtmlForPdfGenerator($metas, $this->buildContext);
118+
$htmlForPdfGenerator->generateHtmlForPdf();
100119
}
101120

102121
public function preBuildRender()
@@ -105,4 +124,17 @@ public function preBuildRender()
105124

106125
$this->io->note('Start rendering in HTML...');
107126
}
127+
128+
private function getMetas(): Metas
129+
{
130+
/*
131+
* TODO - get this from the Builder when it is exposed
132+
* https://github.com/doctrine/rst-parser/pull/97
133+
*/
134+
$metas = new Metas();
135+
$cachedMetasLoader = new CachedMetasLoader();
136+
$cachedMetasLoader->loadCachedMetaEntries($this->buildContext->getHtmlOutputDir(), $metas);
137+
138+
return $metas;
139+
}
108140
}

src/Command/CommandInitializerTrait.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ trait CommandInitializerTrait
2424
private $io;
2525
/** @var OutputInterface */
2626
private $output;
27+
/** @var InputInterface */
28+
private $input;
2729
/** @var Builder */
2830
private $builder;
2931
/** @var Filesystem */
@@ -40,13 +42,15 @@ trait CommandInitializerTrait
4042
private function doInitialize(InputInterface $input, OutputInterface $output, string $sourceDir, string $outputDir)
4143
{
4244
$this->io = new SymfonyStyle($input, $output);
45+
$this->input = $input;
4346
$this->output = $output;
4447

4548
$this->buildContext->initializeRuntimeConfig(
4649
$sourceDir,
4750
$this->initializeHtmlOutputDir($this->filesystem, $outputDir),
4851
$this->initializeJsonOutputDir($outputDir),
49-
$this->initializeParseSubPath($input, $sourceDir)
52+
$this->initializeParseSubPath($input, $sourceDir),
53+
$this->isCacheDisabled()
5054
);
5155

5256
$this->builder = new Builder(
@@ -70,7 +74,12 @@ private function initializeSourceDir(InputInterface $input, Filesystem $filesyst
7074

7175
private function initializeHtmlOutputDir(Filesystem $filesystem, string $path): string
7276
{
73-
return rtrim($this->getRealAbsolutePath($path, $filesystem), '/');
77+
$htmlOutputDir = rtrim($this->getRealAbsolutePath($path, $filesystem), '/');
78+
if ($this->isCacheDisabled() && $filesystem->exists($htmlOutputDir)) {
79+
$filesystem->remove($htmlOutputDir);
80+
}
81+
82+
return $htmlOutputDir;
7483
}
7584

7685
private function initializeParseSubPath(InputInterface $input, string $sourceDir): string
@@ -96,8 +105,8 @@ private function initializeParseSubPath(InputInterface $input, string $sourceDir
96105

97106
private function initializeJsonOutputDir(string $outputDir): string
98107
{
99-
$jsonOutputDir = $this->getRealAbsolutePath($outputDir.'/json', $this->filesystem);
100-
if ($this->filesystem->exists($jsonOutputDir)) {
108+
$jsonOutputDir = $this->getRealAbsolutePath($outputDir.'/_json', $this->filesystem);
109+
if ($this->isCacheDisabled() && $this->filesystem->exists($jsonOutputDir)) {
101110
$this->filesystem->remove($jsonOutputDir);
102111
}
103112

@@ -134,6 +143,8 @@ private function startBuild()
134143
->notName('*.rst.inc')
135144
->name('*.rst');
136145

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

@@ -143,6 +154,57 @@ private function startBuild()
143154
);
144155
}
145156

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+
$rstFiles = array_map(
164+
function (string $rstFile) {
165+
return str_replace([$this->buildContext->getSourceDir(), '.rst'], '', $rstFile);
166+
},
167+
array_keys(iterator_to_array($finder))
168+
);
169+
170+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getHtmlOutputDir(), 'html');
171+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getJsonOutputDir(), 'json');
172+
}
173+
174+
private function sanitizeOutputDir(array $existingRstFiles, string $outputDir, string $format)
175+
{
176+
if (!$this->filesystem->exists($outputDir)) {
177+
return;
178+
}
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+
203+
private function isCacheDisabled(): bool
204+
{
205+
return $this->input->hasOption('disable-cache') && (bool) $this->input->getOption('disable-cache');
206+
}
207+
146208
public function postParseDocument(PostParseDocumentEvent $postParseDocumentEvent): void
147209
{
148210
$file = $postParseDocumentEvent->getDocumentNode()->getEnvironment()->getCurrentFileName();

src/Generator/GeneratorTrait.php

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

0 commit comments

Comments
 (0)