package com.goeuro.coverage;

import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.html.HTMLFormatter;

import java.io.File;
import java.io.IOException;

public class CoverageReporter {
    private static void create(String outDir, String[] execFiles, String[] classDirs, String[] srcDirs)  throws IOException {
        HTMLFormatter htmlFormatter = new HTMLFormatter();
        IReportVisitor visitor = htmlFormatter
                .createVisitor(new FileMultiReportOutput(new File(outDir, "report")));
        CoverageBuilder coverageBuilder = new CoverageBuilder();
        ExecFileLoader execFileLoader = new ExecFileLoader();
        for (String execFile: execFiles) {
            execFileLoader.load(new File(execFile));
        }
        Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);

       for (String classDir: classDirs) {
           analyzer.analyzeAll(new File(classDir));
       }

       visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(),
               execFileLoader.getExecutionDataStore().getContents());


        IBundleCoverage bundle = coverageBuilder.getBundle("report");
        for (String srcDir: srcDirs) {
            visitor.visitBundle(bundle, new DirectorySourceFileLocator(
                    new File(srcDir), "utf-8", 4));
        }

        visitor.visitEnd();
    }

    public static void main(String... args) {
        if (args.length != 4) {
            System.out.println("usage OUTDIR EXECS CLASSES SOURCES");
            return;
        }
        try {
            create(
                    args[0],
                    args[1].split(";"),
                    args[2].split(";"),
                    args[3].split(";")
            );
        } catch (IOException e) {
            System.out.print("Unable to generate report: ");
            System.out.print(e);
        }
    }
}

@@ -237,34 +237,29 @@
 ort.
-DirectorySourceFileLocator
+FileMultiReportOutput
 ;%0Aim
@@ -281,37 +281,30 @@
 .report.
-FileMultiReportOutput
+IReportVisitor
 ;%0Aimport
@@ -319,34 +319,48 @@
 oco.report.I
-ReportVisi
+nputStreamSourceFileLoca
 tor;%0Aimport 
@@ -434,27 +434,87 @@
 java.io.
-IOException
+FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0A%0Apubli
@@ -549,22 +549,21 @@
    p
-rivate
+ublic
  static 
 void
@@ -562,173 +562,877 @@
 tic 
-void create(String outDir, String%5B%5D execFiles, String%5B%5D classDirs, String%5B%5D srcDirs)  throws IOException %7B%0A        HTMLFormatter htmlFormatter = new HTMLFormatter();
+class DirectoriesSourceFileLocator extends InputStreamSourceFileLocator %7B%0A%0A        private final String%5B%5D directories;%0A%0A        public DirectoriesSourceFileLocator(final String%5B%5D directories,%0A        final String encoding, final int tabWidth) %7B%0A            super(encoding, tabWidth);%0A            this.directories = directories;%0A        %7D%0A%0A        @Override%0A        protected InputStream getSourceStream(final String path) throws IOException %7B%0A            for (String directory: directories) %7B%0A                final File file = new File(directory, path);%0A                if (file.exists()) %7B%0A                    return new FileInputStream(file);%0A                %7D%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A    private static void create(String outDir, String%5B%5D execFiles, String%5B%5D classDirs, String%5B%5D srcDirs, HTMLFormatter formatter)  throws IOException %7B
 %0A   
@@ -1465,13 +1465,9 @@
 r = 
-htmlF
+f
 orma
@@ -1471,24 +1471,16 @@
 rmatter%0A
-        
         
@@ -1886,24 +1886,25 @@
 r);%0A%0A       
+ 
 for (String 
@@ -1918,32 +1918,33 @@
 r: classDirs) %7B%0A
+ 
            analy
@@ -1975,24 +1975,25 @@
 classDir));%0A
+ 
        %7D%0A%0A  
@@ -1993,24 +1993,25 @@
   %7D%0A%0A       
+ 
 visitor.visi
@@ -2069,31 +2069,24 @@
 (),%0A        
-     
-  
 execFileLoad
@@ -2204,51 +2204,8 @@
 %22);%0A
-        for (String srcDir: srcDirs) %7B%0A    
     
@@ -2248,17 +2248,19 @@
 Director
-y
+ies
 SourceFi
@@ -2273,45 +2273,15 @@
 tor(
-%0A                    new File(
 srcDir
-)
+s
 , %22u
@@ -2291,26 +2291,16 @@
 8%22, 4));
-%0A        %7D
 %0A%0A      
@@ -2548,32 +2548,24 @@
             
-        
 args%5B0%5D,%0A   
@@ -2569,32 +2569,24 @@
             
-      
-  
 args%5B1%5D.spli
@@ -2601,32 +2601,24 @@
             
-     
-   
 args%5B2%5D.spli
@@ -2633,32 +2633,24 @@
             
-   
-     
 args%5B3%5D.spli
@@ -2655,16 +2655,37 @@
 lit(%22;%22)
+, new HTMLFormatter()
 %0A       

fix multi project not displaying source