@@ -24,6 +24,8 @@ trait CommandInitializerTrait
24
24
private $ io ;
25
25
/** @var OutputInterface */
26
26
private $ output ;
27
+ /** @var InputInterface */
28
+ private $ input ;
27
29
/** @var Builder */
28
30
private $ builder ;
29
31
/** @var Filesystem */
@@ -40,13 +42,15 @@ trait CommandInitializerTrait
40
42
private function doInitialize (InputInterface $ input , OutputInterface $ output , string $ sourceDir , string $ outputDir )
41
43
{
42
44
$ this ->io = new SymfonyStyle ($ input , $ output );
45
+ $ this ->input = $ input ;
43
46
$ this ->output = $ output ;
44
47
45
48
$ this ->buildContext ->initializeRuntimeConfig (
46
49
$ sourceDir ,
47
50
$ this ->initializeHtmlOutputDir ($ this ->filesystem , $ outputDir ),
48
51
$ this ->initializeJsonOutputDir ($ outputDir ),
49
- $ this ->initializeParseSubPath ($ input , $ sourceDir )
52
+ $ this ->initializeParseSubPath ($ input , $ sourceDir ),
53
+ $ this ->isCacheDisabled ()
50
54
);
51
55
52
56
$ this ->builder = new Builder (
@@ -70,7 +74,12 @@ private function initializeSourceDir(InputInterface $input, Filesystem $filesyst
70
74
71
75
private function initializeHtmlOutputDir (Filesystem $ filesystem , string $ path ): string
72
76
{
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 ;
74
83
}
75
84
76
85
private function initializeParseSubPath (InputInterface $ input , string $ sourceDir ): string
@@ -96,8 +105,8 @@ private function initializeParseSubPath(InputInterface $input, string $sourceDir
96
105
97
106
private function initializeJsonOutputDir (string $ outputDir ): string
98
107
{
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 )) {
101
110
$ this ->filesystem ->remove ($ jsonOutputDir );
102
111
}
103
112
@@ -134,6 +143,8 @@ private function startBuild()
134
143
->notName ('*.rst.inc ' )
135
144
->name ('*.rst ' );
136
145
146
+ $ this ->sanitizeOutputDirs ($ this ->finder );
147
+
137
148
$ this ->io ->note (sprintf ('Start parsing %d rst files ' , $ this ->finder ->count ()));
138
149
$ this ->progressBar = new ProgressBar ($ this ->output , $ this ->finder ->count ());
139
150
@@ -143,6 +154,57 @@ private function startBuild()
143
154
);
144
155
}
145
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
+ $ 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
+
146
208
public function postParseDocument (PostParseDocumentEvent $ postParseDocumentEvent ): void
147
209
{
148
210
$ file = $ postParseDocumentEvent ->getDocumentNode ()->getEnvironment ()->getCurrentFileName ();
0 commit comments