4
4
import com .codingame .codemachine .compiler .java .core .CompilationLogKind ;
5
5
import com .codingame .codemachine .compiler .java .core .CompilationResult ;
6
6
import com .google .gson .Gson ;
7
+ import org .apache .commons .io .FileUtils ;
7
8
8
9
import javax .tools .Diagnostic ;
9
10
import javax .tools .Diagnostic .Kind ;
12
13
import javax .tools .JavaFileObject ;
13
14
import javax .tools .StandardJavaFileManager ;
14
15
import javax .tools .ToolProvider ;
16
+ import java .io .File ;
15
17
import java .io .IOException ;
16
18
import java .io .OutputStream ;
17
19
import java .io .PrintStream ;
21
23
import static java .util .Collections .singletonList ;
22
24
23
25
public class CodinGameJavaCompiler {
26
+ private static final String DEFAULT_OUTPUT = "-" ;
24
27
25
28
private static class NullOutputStream extends OutputStream {
26
29
@ Override
@@ -30,6 +33,7 @@ public void write(int b) throws IOException {
30
33
31
34
public static void main (String ... args ) throws IOException {
32
35
PrintStream realOut = System .out ;
36
+ PrintStream realErr = System .err ;
33
37
System .setOut (new PrintStream (new NullOutputStream (), true ));
34
38
System .setErr (new PrintStream (new NullOutputStream (), true ));
35
39
@@ -41,7 +45,6 @@ public static void main(String... args) throws IOException {
41
45
List <String > files = new ArrayList <>();
42
46
List <String > options = new ArrayList <>();
43
47
44
-
45
48
for (int i = 0 ; i < args .length ; ++i ) {
46
49
String arg = args [i ];
47
50
@@ -64,6 +67,8 @@ public static void main(String... args) throws IOException {
64
67
}
65
68
}
66
69
70
+ CompilationResult result = new CompilationResult ();
71
+ int resultCode = 1 ;
67
72
if (!files .isEmpty ()) {
68
73
List <CompilationLogDto > logs = new ArrayList <>();
69
74
@@ -89,21 +94,33 @@ public static void main(String... args) throws IOException {
89
94
}
90
95
fileManager .close ();
91
96
92
- CompilationResult result = new CompilationResult ();
93
97
result .setSuccess (success );
94
98
result .setLogs (logs );
95
- realOut .println (new Gson ().toJson (result ));
96
- System .exit (success ? 0 : 1 );
99
+ resultCode = success ? 0 : 1 ;
97
100
}
98
101
else {
99
- CompilationResult result = new CompilationResult ();
100
102
result .setSuccess (false );
101
103
CompilationLogDto log = new CompilationLogDto ();
102
104
log .setKind (CompilationLogKind .ERROR );
103
105
log .setMessage ("no source file" );
104
106
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
+ }
107
123
}
124
+ System .exit (resultCode );
108
125
}
109
126
}
0 commit comments