package io.bazel.rulesscala.scalac;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import scala.tools.nsc.reporters.Reporter;
import scala.tools.nsc.Settings;
import scala.tools.nsc.reporters.ConsoleReporter;
import scala.tools.nsc.MainClass;
import scala.tools.nsc.Global;


public class ReportableMainClass extends MainClass{
    private Global compiler;
    private final CompileOptions ops;

    public ReportableMainClass(CompileOptions ops){
        this.ops = ops;
    }

    @Override
    public Global newCompiler() {
        if (compiler == null) {
            Settings settings = super.settings();
            ConsoleReporter reporter;
            Path path = Paths.get(ops.diagnosticsFile);
            try {
                Files.deleteIfExists(path);
                Files.createFile(path);
            } catch (IOException e) {
                throw new RuntimeException("Could not delete/make diagnostics proto file", e);
            }
            if (!ops.enableDiagnosticsReport) {
                reporter = new ConsoleReporter(settings);
            } else {
                reporter = new ProtoReporter(settings);
            }
            compiler = new Global(settings, reporter);
        }
        return compiler;
    }
}


@@ -145,51 +145,8 @@
 hs;%0A
-import scala.tools.nsc.reporters.Reporter;%0A
 impo
@@ -290,17 +290,16 @@
 lobal;%0A%0A
-%0A
 public c
@@ -340,16 +340,17 @@
 ainClass
+ 
 %7B%0A    pr
@@ -461,16 +461,17 @@
 ons ops)
+ 
 %7B%0A      
@@ -559,120 +559,457 @@
 if (
-compiler == null) %7B%0A            Settings settings = super.settings();%0A            ConsoleReporter reporter;%0A    
+!ops.enableDiagnosticsReport) %7B%0A            createDiagnosticsFile();%0A            return super.newCompiler();%0A        %7D%0A%0A        if (compiler == null) %7B%0A            createDiagnosticsFile();%0A%0A            Settings settings = super.settings();%0A            ConsoleReporter reporter = new ProtoReporter(settings);%0A%0A            compiler = new Global(settings, reporter);%0A        %7D%0A        return compiler;%0A    %7D%0A%0A    private void createDiagnosticsFile() %7B%0A
     
@@ -1052,28 +1052,24 @@
 sticsFile);%0A
-    
         try 
@@ -1078,28 +1078,24 @@
             
-    
 Files.delete
@@ -1118,28 +1118,24 @@
             
- 
-   
 Files.create
@@ -1154,20 +1154,16 @@
         
-   
- 
 %7D catch 
@@ -1176,28 +1176,24 @@
 eption e) %7B%0A
-    
             
@@ -1283,306 +1283,14 @@
     
-    
 %7D%0A    
-        if (!ops.enableDiagnosticsReport) %7B%0A                reporter = new ConsoleReporter(settings);%0A            %7D else %7B%0A                reporter = new ProtoReporter(settings);%0A            %7D%0A            compiler = new Global(settings, reporter);%0A        %7D%0A        return compiler;%0A    %7D%0A
 %7D%0A
+%7D
 %0A

Fix failed build reporting for Scala 2.11 (#1159)