26
26
import java .util .List ;
27
27
import java .util .Map ;
28
28
import java .util .Set ;
29
+ import java .util .concurrent .CompletableFuture ;
29
30
import java .util .concurrent .ExecutorService ;
30
31
import java .util .concurrent .Executors ;
31
32
import java .util .concurrent .TimeUnit ;
@@ -621,6 +622,13 @@ private void extractSource() throws IOException {
621
622
dependencyInstallationResult = this .preparePackagesAndDependencies (filesToExtract );
622
623
}
623
624
Set <Path > extractedFiles = new LinkedHashSet <>();
625
+
626
+ // Extract HTML files as they may contain TypeScript
627
+ CompletableFuture <?> htmlFuture = extractFiles (
628
+ filesToExtract , extractedFiles , extractors ,
629
+ f -> extractors .fileType (f ) == FileType .HTML );
630
+
631
+ htmlFuture .join (); // Wait for HTML extraction to be finished.
624
632
625
633
// extract TypeScript projects and files
626
634
extractTypeScript (filesToExtract , extractedFiles ,
@@ -634,21 +642,23 @@ private void extractSource() throws IOException {
634
642
f -> !(hasTypeScriptFiles && isFileDerivedFromTypeScriptFile (f , extractedFiles )));
635
643
}
636
644
637
- private void extractFiles (
645
+ private CompletableFuture <?> extractFiles (
638
646
Set <Path > filesToExtract ,
639
647
Set <Path > extractedFiles ,
640
648
FileExtractors extractors ,
641
649
Predicate <Path > shouldExtract ) {
642
650
651
+ List <CompletableFuture <?>> futures = new ArrayList <>();
643
652
for (Path f : filesToExtract ) {
644
653
if (extractedFiles .contains (f ))
645
654
continue ;
646
655
if (!shouldExtract .test (f )) {
647
656
continue ;
648
657
}
649
658
extractedFiles .add (f );
650
- extract (extractors .forFile (f ), f , null );
659
+ futures . add ( extract (extractors .forFile (f ), f , null ) );
651
660
}
661
+ return CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ]));
652
662
}
653
663
654
664
/**
@@ -1164,10 +1174,13 @@ private SourceType getSourceType() {
1164
1174
* <p>If the state is {@code null}, the extraction job will be submitted to the {@link
1165
1175
* #threadPool}, otherwise extraction will happen on the main thread.
1166
1176
*/
1167
- protected void extract (FileExtractor extractor , Path file , ExtractorState state ) {
1168
- if (state == null && threadPool != null )
1169
- threadPool .submit (() -> doExtract (extractor , file , state ));
1170
- else doExtract (extractor , file , state );
1177
+ protected CompletableFuture <?> extract (FileExtractor extractor , Path file , ExtractorState state ) {
1178
+ if (state == null && threadPool != null ) {
1179
+ return CompletableFuture .runAsync (() -> doExtract (extractor , file , state ), threadPool );
1180
+ } else {
1181
+ doExtract (extractor , file , state );
1182
+ return CompletableFuture .completedFuture (null );
1183
+ }
1171
1184
}
1172
1185
1173
1186
private void doExtract (FileExtractor extractor , Path file , ExtractorState state ) {
0 commit comments