Skip to content

Commit 458f95c

Browse files
authored
Merge pull request #3 from CGNonofr/master
New command api
2 parents f3bf2a6 + e118669 commit 458f95c

File tree

6 files changed

+80
-181
lines changed

6 files changed

+80
-181
lines changed

pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111

1212
<dependencies>
1313
<dependency>
14-
<groupId>com.google.code.gson</groupId>
15-
<artifactId>gson</artifactId>
16-
<version>2.8.0</version>
17-
</dependency>
18-
<dependency>
19-
<groupId>org.apache.commons</groupId>
14+
<groupId>commons-io</groupId>
2015
<artifactId>commons-io</artifactId>
2116
<version>1.3.2</version>
2217
</dependency>
Lines changed: 73 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,102 @@
11
package com.codingame.codemachine.compiler.java;
22

3-
import com.codingame.codemachine.compiler.java.core.CompilationLogDto;
4-
import com.codingame.codemachine.compiler.java.core.CompilationLogKind;
5-
import com.codingame.codemachine.compiler.java.core.CompilationResult;
6-
import com.google.gson.Gson;
7-
import org.apache.commons.io.FileUtils;
3+
import java.io.IOException;
4+
import java.io.LineNumberReader;
5+
import java.io.StringReader;
6+
import java.util.ArrayList;
7+
import java.util.List;
88

99
import javax.tools.Diagnostic;
10-
import javax.tools.Diagnostic.Kind;
1110
import javax.tools.DiagnosticCollector;
1211
import javax.tools.JavaCompiler;
1312
import javax.tools.JavaFileObject;
1413
import javax.tools.StandardJavaFileManager;
1514
import javax.tools.ToolProvider;
16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.io.OutputStream;
19-
import java.io.PrintStream;
20-
import java.util.ArrayList;
21-
import java.util.List;
22-
23-
import static java.util.Collections.singletonList;
2415

2516
public class CodinGameJavaCompiler {
26-
private static final String DEFAULT_OUTPUT = "-";
27-
28-
private static class NullOutputStream extends OutputStream {
29-
@Override
30-
public void write(int b) throws IOException {
31-
}
32-
}
3317

3418
public static void main(String... args) throws IOException {
35-
PrintStream realOut = System.out;
36-
PrintStream realErr = System.err;
37-
System.setOut(new PrintStream(new NullOutputStream(), true));
38-
System.setErr(new PrintStream(new NullOutputStream(), true));
39-
4019
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
4120
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
42-
StandardJavaFileManager fileManager =
43-
compiler.getStandardFileManager(diagnosticsCollector, null, null);
44-
45-
List<String> files = new ArrayList<>();
46-
List<String> options = new ArrayList<>();
47-
48-
for (int i = 0; i < args.length; ++i) {
49-
String arg = args[i];
21+
int resultCode = 1;
22+
try(StandardJavaFileManager fileManager =
23+
compiler.getStandardFileManager(diagnosticsCollector, null, null)) {
5024

51-
if (arg.startsWith("-")) {
52-
int paramCount = compiler.isSupportedOption(arg);
53-
if (paramCount < 0) {
54-
paramCount = fileManager.isSupportedOption(arg);
25+
List<String> files = new ArrayList<>();
26+
List<String> options = new ArrayList<>();
27+
28+
for (int i = 0; i < args.length; ++i) {
29+
String arg = args[i];
30+
31+
if (arg.startsWith("-")) {
32+
int paramCount = compiler.isSupportedOption(arg);
33+
if (paramCount < 0) {
34+
paramCount = fileManager.isSupportedOption(arg);
35+
}
36+
if (paramCount < 0) {
37+
// unsupported, let javacTask show the error
38+
paramCount = 0;
39+
}
40+
options.add(arg);
41+
for (int j = 0; j < paramCount; ++j, i++) {
42+
options.add(args[i + 1]);
43+
}
5544
}
56-
if (paramCount < 0) {
57-
// unsupported, let javacTask show the error
58-
paramCount = 0;
59-
}
60-
options.add(arg);
61-
for (int j = 0; j < paramCount; ++j, i++) {
62-
options.add(args[i + 1]);
45+
else {
46+
files.add(arg);
6347
}
6448
}
65-
else {
66-
files.add(arg);
67-
}
68-
}
69-
70-
CompilationResult result = new CompilationResult();
71-
int resultCode = 1;
72-
if (!files.isEmpty()) {
73-
List<CompilationLogDto> logs = new ArrayList<>();
74-
75-
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files);
76-
JavaCompiler.CompilationTask task =
77-
compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits);
78-
boolean success = task.call();
79-
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) {
80-
switch (diagnostic.getKind()) {
49+
50+
if (!files.isEmpty()) {
51+
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files);
52+
JavaCompiler.CompilationTask task =
53+
compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits);
54+
boolean success = task.call();
55+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) {
56+
57+
LineNumberReader reader = new LineNumberReader(new StringReader(diagnostic.getSource().getCharContent(true).toString()));
58+
String line = reader.lines().skip(diagnostic.getLineNumber() - 1).limit(1).findAny().get();
59+
60+
String type = null;
61+
switch (diagnostic.getKind()) {
8162
case ERROR:
63+
type = "ERROR";
64+
break;
8265
case WARNING:
83-
CompilationLogDto log = new CompilationLogDto();
84-
log.setFilename(diagnostic.getSource().getName());
85-
log.setLine(diagnostic.getLineNumber());
86-
log.setColumn(diagnostic.getColumnNumber());
87-
log.setMessage(diagnostic.getMessage(null));
88-
log.setKind(diagnostic.getKind() == Kind.ERROR ? CompilationLogKind.ERROR : CompilationLogKind.WARNING);
89-
logs.add(log);
66+
case MANDATORY_WARNING:
67+
type = "WARNING";
9068
break;
91-
default:
92-
// nothing
69+
case NOTE:
70+
type = "INFO";
71+
break;
72+
case OTHER:
73+
continue;
74+
}
75+
76+
System.err.println(String.format("%s:%d: %s: %s\n%s\n%"+diagnostic.getColumnNumber()+"s",
77+
diagnostic.getSource().getName(),
78+
diagnostic.getLineNumber(),
79+
diagnostic.getKind().name().toLowerCase(),
80+
diagnostic.getMessage(null),
81+
line,
82+
"^"
83+
));
84+
85+
System.out.println(String.format("CG> annotate --type \"%s\" --file \"%s\" --position \"%s\" --message \"%s\"",
86+
type,
87+
diagnostic.getSource().getName(),
88+
diagnostic.getLineNumber()+":"+diagnostic.getColumnNumber(),
89+
diagnostic.getMessage(null).replaceAll("\"","\\\"")
90+
));
9391
}
92+
resultCode = success ? 0 : 1;
9493
}
95-
fileManager.close();
96-
97-
result.setSuccess(success);
98-
result.setLogs(logs);
99-
resultCode = success ? 0 : 1;
100-
}
101-
else {
102-
result.setSuccess(false);
103-
CompilationLogDto log = new CompilationLogDto();
104-
log.setKind(CompilationLogKind.ERROR);
105-
log.setMessage("no source file");
106-
result.setLogs(singletonList(log));
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);
94+
else {
95+
System.err.println("no source file");
96+
resultCode = 2;
12297
}
12398
}
99+
124100
System.exit(resultCode);
125101
}
126102
}

src/main/java/com/codingame/codemachine/compiler/java/core/CompilationLogDto.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/main/java/com/codingame/codemachine/compiler/java/core/CompilationLogKind.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/java/com/codingame/codemachine/compiler/java/core/CompilationResult.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/resources/cgjavac

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
#!/bin/bash
2-
java -Dcodingame.compiler.output=/project/results/compilation.json -jar /usr/src/codingame/java-compiler/java-compiler.jar "$@"
2+
3+
echo "CG> redirect-streams --input err compilation"
4+
5+
java -Dcodingame.compiler.output=/project/results/compilation.json -jar /usr/src/codingame/java-compiler/java-compiler.jar "$@"
6+
7+
echo "CG> redirect-streams --reset --input err compilation"

0 commit comments

Comments
 (0)