@@ -105,7 +105,7 @@ private function initializeParseSubPath(InputInterface $input, string $sourceDir
105
105
106
106
private function initializeJsonOutputDir (string $ outputDir ): string
107
107
{
108
- $ jsonOutputDir = $ this ->getRealAbsolutePath ($ outputDir .'/json ' , $ this ->filesystem );
108
+ $ jsonOutputDir = $ this ->getRealAbsolutePath ($ outputDir .'/_json ' , $ this ->filesystem );
109
109
if ($ this ->isCacheDisabled () && $ this ->filesystem ->exists ($ jsonOutputDir )) {
110
110
$ this ->filesystem ->remove ($ jsonOutputDir );
111
111
}
@@ -143,6 +143,8 @@ private function startBuild()
143
143
->notName ('*.rst.inc ' )
144
144
->name ('*.rst ' );
145
145
146
+ $ this ->sanitizeOutputDirs ($ this ->finder );
147
+
146
148
$ this ->io ->note (sprintf ('Start parsing %d rst files ' , $ this ->finder ->count ()));
147
149
$ this ->progressBar = new ProgressBar ($ this ->output , $ this ->finder ->count ());
148
150
@@ -152,6 +154,52 @@ private function startBuild()
152
154
);
153
155
}
154
156
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
+
155
203
private function isCacheDisabled (): bool
156
204
{
157
205
return (bool ) $ this ->input ->getOption ('disable-cache ' );
0 commit comments