44import com .codingame .codemachine .compiler .java .core .CompilationLogKind ;
55import com .codingame .codemachine .compiler .java .core .CompilationResult ;
66import com .google .gson .Gson ;
7+ import org .apache .commons .io .FileUtils ;
78
89import javax .tools .Diagnostic ;
910import javax .tools .Diagnostic .Kind ;
1213import javax .tools .JavaFileObject ;
1314import javax .tools .StandardJavaFileManager ;
1415import javax .tools .ToolProvider ;
16+ import java .io .File ;
1517import java .io .IOException ;
1618import java .io .OutputStream ;
1719import java .io .PrintStream ;
2123import static java .util .Collections .singletonList ;
2224
2325public class CodinGameJavaCompiler {
26+ private static final String DEFAULT_OUTPUT = "-" ;
2427
2528 private static class NullOutputStream extends OutputStream {
2629 @ Override
@@ -30,6 +33,7 @@ public void write(int b) throws IOException {
3033
3134 public static void main (String ... args ) throws IOException {
3235 PrintStream realOut = System .out ;
36+ PrintStream realErr = System .err ;
3337 System .setOut (new PrintStream (new NullOutputStream (), true ));
3438 System .setErr (new PrintStream (new NullOutputStream (), true ));
3539
@@ -41,7 +45,6 @@ public static void main(String... args) throws IOException {
4145 List <String > files = new ArrayList <>();
4246 List <String > options = new ArrayList <>();
4347
44-
4548 for (int i = 0 ; i < args .length ; ++i ) {
4649 String arg = args [i ];
4750
@@ -64,6 +67,8 @@ public static void main(String... args) throws IOException {
6467 }
6568 }
6669
70+ CompilationResult result = new CompilationResult ();
71+ int resultCode = 1 ;
6772 if (!files .isEmpty ()) {
6873 List <CompilationLogDto > logs = new ArrayList <>();
6974
@@ -89,21 +94,33 @@ public static void main(String... args) throws IOException {
8994 }
9095 fileManager .close ();
9196
92- CompilationResult result = new CompilationResult ();
9397 result .setSuccess (success );
9498 result .setLogs (logs );
95- realOut .println (new Gson ().toJson (result ));
96- System .exit (success ? 0 : 1 );
99+ resultCode = success ? 0 : 1 ;
97100 }
98101 else {
99- CompilationResult result = new CompilationResult ();
100102 result .setSuccess (false );
101103 CompilationLogDto log = new CompilationLogDto ();
102104 log .setKind (CompilationLogKind .ERROR );
103105 log .setMessage ("no source file" );
104106 result .setLogs (singletonList (log ));
105- realOut .println (new Gson ().toJson (result ));
106- System .exit (2 );
107+ resultCode = 2 ;
108+ }
109+
110+ String resultOutput = System .getProperty ("codingame.compiler.output" , DEFAULT_OUTPUT );
111+ String resultStr = new Gson ().toJson (result );
112+ if (DEFAULT_OUTPUT .equals (resultOutput )) {
113+ realOut .println (resultStr );
114+ }
115+ else {
116+ try {
117+ FileUtils .writeStringToFile (new File (resultOutput ), resultStr );
118+ }
119+ catch (IOException e ) {
120+ realErr .println (e .getMessage ());
121+ System .exit (3 );
122+ }
107123 }
124+ System .exit (resultCode );
108125 }
109126}
0 commit comments