Skip to content

Commit f3bf2a6

Browse files
authored
Merge pull request #2 from CodinGame/update_implementation
feat: update java compiler according to the new constraints
2 parents 67efa47 + 872cdd7 commit f3bf2a6

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
<artifactId>gson</artifactId>
1616
<version>2.8.0</version>
1717
</dependency>
18+
<dependency>
19+
<groupId>org.apache.commons</groupId>
20+
<artifactId>commons-io</artifactId>
21+
<version>1.3.2</version>
22+
</dependency>
1823
</dependencies>
1924

2025
<build>

src/main/java/com/codingame/codemachine/compiler/java/CodinGameJavaCompiler.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.codingame.codemachine.compiler.java.core.CompilationLogKind;
55
import com.codingame.codemachine.compiler.java.core.CompilationResult;
66
import com.google.gson.Gson;
7+
import org.apache.commons.io.FileUtils;
78

89
import javax.tools.Diagnostic;
910
import javax.tools.Diagnostic.Kind;
@@ -12,6 +13,7 @@
1213
import javax.tools.JavaFileObject;
1314
import javax.tools.StandardJavaFileManager;
1415
import javax.tools.ToolProvider;
16+
import java.io.File;
1517
import java.io.IOException;
1618
import java.io.OutputStream;
1719
import java.io.PrintStream;
@@ -21,6 +23,7 @@
2123
import static java.util.Collections.singletonList;
2224

2325
public 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
}

src/main/resources/cgjavac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
java -jar /usr/src/codingame/java-compiler/java-compiler.jar "$@"
2+
java -Dcodingame.compiler.output=/project/results/compilation.json -jar /usr/src/codingame/java-compiler/java-compiler.jar "$@"

0 commit comments

Comments
 (0)