Refactor JPS daemon client

This commit is contained in:
Alexey Tsvetkov
2017-01-10 23:50:33 +03:00
parent 73f7b76b5d
commit 99c72b6dff
10 changed files with 188 additions and 76 deletions
@@ -17,10 +17,24 @@
package org.jetbrains.kotlin.cli.common.arguments
import com.intellij.util.xmlb.XmlSerializerUtil
import com.sampullara.cli.Args
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import java.util.*
fun <A : CommonCompilerArguments> parseArguments(args: Array<String>, arguments: A) {
val unparsedArgs = Args.parse(arguments, args, false).partition { it.startsWith("-X") }
arguments.unknownExtraFlags = unparsedArgs.first
arguments.freeArgs = unparsedArgs.second
for (argument in arguments.freeArgs) {
if (argument.startsWith("-")) {
throw IllegalArgumentException("Invalid argument: " + argument)
}
}
}
fun <T : Any> copyBean(bean: T) = copyFields(bean, bean.javaClass.newInstance(), true)
fun <From : Any, To : From> mergeBeans(from: From, to: To): To {
@@ -30,6 +30,7 @@ import kotlin.jvm.functions.Function1;
import org.fusesource.jansi.AnsiConsole;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.arguments.ArgumentUtilsKt;
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments;
import org.jetbrains.kotlin.cli.common.messages.*;
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler;
@@ -103,22 +104,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> {
@SuppressWarnings("WeakerAccess") // Used in maven (see KotlinCompileMojoBase.java)
public void parseArguments(@NotNull String[] args, @NotNull A arguments) {
Pair<List<String>, List<String>> unparsedArgs =
CollectionsKt.partition(Args.parse(arguments, args, false), new Function1<String, Boolean>() {
@Override
public Boolean invoke(String s) {
return s.startsWith("-X");
}
});
arguments.unknownExtraFlags = unparsedArgs.getFirst();
arguments.freeArgs = unparsedArgs.getSecond();
for (String argument : arguments.freeArgs) {
if (argument.startsWith("-")) {
throw new IllegalArgumentException("Invalid argument: " + argument);
}
}
ArgumentUtilsKt.parseArguments(args, arguments);
}
@NotNull
@@ -110,15 +110,10 @@ abstract class KotlinCompilerRunner<in Env : CompilerEnvironment> {
protected fun runCompiler(
compilerClassName: String,
arguments: CommonCompilerArguments,
additionalArguments: String,
compilerArgs: CommonCompilerArguments,
environment: Env): ExitCode {
return try {
val argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments)
argumentsList.addAll(additionalArguments.split(" "))
val argsArray = argumentsList.toTypedArray()
doRunCompiler(compilerClassName, argsArray, environment)
compileWithDaemonOrFallback(compilerClassName, compilerArgs, environment)
}
catch (e: Throwable) {
MessageCollectorUtil.reportException(environment.messageCollector, e)
@@ -126,43 +121,20 @@ abstract class KotlinCompilerRunner<in Env : CompilerEnvironment> {
}
}
protected abstract fun doRunCompiler(
protected abstract fun compileWithDaemonOrFallback(
compilerClassName: String,
argsArray: Array<String>,
compilerArgs: CommonCompilerArguments,
environment: Env
): ExitCode
/**
* Returns null if could not connect to daemon
*/
protected open fun compileWithDaemon(
protected abstract fun compileWithDaemon(
compilerClassName: String,
argsArray: Array<String>,
compilerArgs: CommonCompilerArguments,
environment: Env
): ExitCode? {
val compilerOut = ByteArrayOutputStream()
val daemonOut = ByteArrayOutputStream()
val services = CompilationServices(
incrementalCompilationComponents = environment.services.get(IncrementalCompilationComponents::class.java),
compilationCanceledStatus = environment.services.get(CompilationCanceledStatus::class.java))
val targetPlatform = when (compilerClassName) {
K2JVM_COMPILER -> CompileService.TargetPlatform.JVM
K2JS_COMPILER -> CompileService.TargetPlatform.JS
K2METADATA_COMPILER -> CompileService.TargetPlatform.METADATA
else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName")
}
val res: Int = withDaemon(environment, retryOnConnectionError = true) { daemon, sessionId ->
KotlinCompilerClient.incrementalCompile(daemon, sessionId, targetPlatform, argsArray, services, compilerOut, daemonOut)
} ?: return null
val exitCode = exitCodeFromProcessExitCode(res)
processCompilerOutput(environment, compilerOut, exitCode)
BufferedReader(StringReader(daemonOut.toString())).forEachLine {
environment.messageCollector.report(CompilerMessageSeverity.INFO, it, CompilerMessageLocation.NO_LOCATION)
}
return exitCode
}
): ExitCode?
protected fun <T> withDaemon(environment: Env, retryOnConnectionError: Boolean, fn: (CompileService, sessionId: Int)->T): T? {
fun retryOrFalse(e: Exception): T? {
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.progress.CompilationCanceledStatus
import java.rmi.server.UnicastRemoteObject
class CompilerCallbackServicesFacadeServer(
open class CompilerCallbackServicesFacadeServer(
val incrementalCompilationComponents: IncrementalCompilationComponents? = null,
val compilationCanceledStatus: CompilationCanceledStatus? = null,
port: Int = SOCKET_ANY_FREE_PORT
@@ -38,7 +38,8 @@ interface CompileService : Remote {
enum class CompilerMode : Serializable {
NON_INCREMENTAL_COMPILER,
INCREMENTAL_COMPILER
INCREMENTAL_COMPILER,
JPS_COMPILER
}
companion object {
@@ -49,4 +49,6 @@ interface IncrementalCompilerServicesFacade : CompilerServicesFacadeBase {
@Throws(RemoteException::class)
fun getChanges(artifact: File, sinceTS: Long): Iterable<SimpleDirtyData>?
}
}
interface JpsCompilerServicesFacade : CompilerServicesFacadeBase, CompilerCallbackServicesFacade
@@ -334,6 +334,14 @@ class CompileServiceImpl(
val serviceReporter = CompileServiceReporterImpl(servicesFacade, additionalCompilerArguments)
return when (compilerMode) {
CompileService.CompilerMode.JPS_COMPILER -> {
val jpsServicesFacade = servicesFacade as JpsCompilerServicesFacade
doCompile(sessionId, serviceReporter, operationsTracer) { eventManger, profiler ->
val services = createCompileServices(jpsServicesFacade, eventManger, profiler)
execCompiler(targetPlatform, services, compilerArguments, messageCollector)
}
}
CompileService.CompilerMode.NON_INCREMENTAL_COMPILER -> {
doCompile(sessionId, serviceReporter, operationsTracer) { eventManger, profiler ->
execCompiler(targetPlatform, Services.EMPTY, compilerArguments, messageCollector)