Simplify KotlinCompilerRunner and CompilerRunnerUtil
This commit is contained in:
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.compiler.runner;
|
package org.jetbrains.jet.compiler.runner;
|
||||||
|
|
||||||
import com.intellij.util.Function;
|
|
||||||
import kotlin.Function1;
|
import kotlin.Function1;
|
||||||
import kotlin.KotlinPackage;
|
import kotlin.KotlinPackage;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -27,7 +26,9 @@ import org.jetbrains.jet.preloading.ClassPreloadingUtils;
|
|||||||
import org.jetbrains.jet.utils.KotlinPaths;
|
import org.jetbrains.jet.utils.KotlinPaths;
|
||||||
import org.jetbrains.jet.utils.UtilsPackage;
|
import org.jetbrains.jet.utils.UtilsPackage;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@@ -114,12 +115,6 @@ public class CompilerRunnerUtil {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleProcessTermination(int exitCode, @NotNull MessageCollector messageCollector) {
|
|
||||||
if (exitCode != 0 && exitCode != 1) {
|
|
||||||
messageCollector.report(ERROR, "Compiler terminated with exit code: " + exitCode, NO_LOCATION);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getReturnCodeFromObject(@Nullable Object rc) throws Exception {
|
public static int getReturnCodeFromObject(@Nullable Object rc) throws Exception {
|
||||||
if (rc == null) {
|
if (rc == null) {
|
||||||
return /* ExitCode.INTERNAL_ERROR */ 2;
|
return /* ExitCode.INTERNAL_ERROR */ 2;
|
||||||
@@ -155,19 +150,4 @@ public class CompilerRunnerUtil {
|
|||||||
|
|
||||||
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
|
return exec.invoke(kompiler.newInstance(), out, environment.getServices(), arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void outputCompilerMessagesAndHandleExitCode(
|
|
||||||
@NotNull MessageCollector messageCollector,
|
|
||||||
@NotNull OutputItemsCollector outputItemsCollector,
|
|
||||||
@NotNull Function<PrintStream, Integer> compilerRun
|
|
||||||
) {
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
PrintStream out = new PrintStream(outputStream);
|
|
||||||
|
|
||||||
int exitCode = compilerRun.fun(out);
|
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new StringReader(outputStream.toString()));
|
|
||||||
CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, outputItemsCollector);
|
|
||||||
handleProcessTermination(exitCode, messageCollector);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-12
@@ -31,11 +31,13 @@ import org.jetbrains.jet.cli.common.messages.MessageCollector;
|
|||||||
import org.jetbrains.jet.cli.common.messages.MessageCollectorUtil;
|
import org.jetbrains.jet.cli.common.messages.MessageCollectorUtil;
|
||||||
import org.jetbrains.jet.compiler.CompilerSettings;
|
import org.jetbrains.jet.compiler.CompilerSettings;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.jetbrains.jet.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
|
||||||
|
import static org.jetbrains.jet.cli.common.messages.CompilerMessageSeverity.ERROR;
|
||||||
|
|
||||||
public class KotlinCompilerRunner {
|
public class KotlinCompilerRunner {
|
||||||
private static final String K2JVM_COMPILER = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
|
private static final String K2JVM_COMPILER = "org.jetbrains.jet.cli.jvm.K2JVMCompiler";
|
||||||
private static final String K2JS_COMPILER = "org.jetbrains.jet.cli.js.K2JSCompiler";
|
private static final String K2JS_COMPILER = "org.jetbrains.jet.cli.js.K2JSCompiler";
|
||||||
@@ -77,23 +79,28 @@ public class KotlinCompilerRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void runCompiler(
|
private static void runCompiler(
|
||||||
final String compilerClassName,
|
String compilerClassName,
|
||||||
CommonCompilerArguments arguments,
|
CommonCompilerArguments arguments,
|
||||||
String additionalArguments,
|
String additionalArguments,
|
||||||
CommonCompilerArguments defaultArguments,
|
CommonCompilerArguments defaultArguments,
|
||||||
final MessageCollector messageCollector,
|
MessageCollector messageCollector,
|
||||||
OutputItemsCollector collector,
|
OutputItemsCollector collector,
|
||||||
final CompilerEnvironment environment
|
CompilerEnvironment environment
|
||||||
) {
|
) {
|
||||||
final List<String> argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments, defaultArguments);
|
List<String> argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments, defaultArguments);
|
||||||
argumentsList.addAll(StringUtil.split(additionalArguments, " "));
|
argumentsList.addAll(StringUtil.split(additionalArguments, " "));
|
||||||
|
|
||||||
CompilerRunnerUtil.outputCompilerMessagesAndHandleExitCode(messageCollector, collector, new Function<PrintStream, Integer>() {
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
@Override
|
PrintStream out = new PrintStream(stream);
|
||||||
public Integer fun(PrintStream stream) {
|
|
||||||
return execCompiler(compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, stream, messageCollector);
|
int exitCode = execCompiler(compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, out, messageCollector);
|
||||||
}
|
|
||||||
});
|
BufferedReader reader = new BufferedReader(new StringReader(stream.toString()));
|
||||||
|
CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, collector);
|
||||||
|
|
||||||
|
if (exitCode != 0 && exitCode != 1) {
|
||||||
|
messageCollector.report(ERROR, "Compiler terminated with internal error", NO_LOCATION);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int execCompiler(
|
private static int execCompiler(
|
||||||
|
|||||||
Reference in New Issue
Block a user