Minor, merge two KotlinCoreEnvironment-creating functions

This commit is contained in:
Alexander Udalov
2017-09-22 13:36:47 +03:00
parent 613297ad60
commit 129fad6ade
@@ -151,7 +151,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
KotlinToJVMBytecodeCompiler.configureSourceRoots(configuration, moduleChunk.modules, buildFile) KotlinToJVMBytecodeCompiler.configureSourceRoots(configuration, moduleChunk.modules, buildFile)
val environment = createEnvironmentWithScriptingSupport(rootDisposable, configuration, arguments, messageCollector) val environment = createCoreEnvironment(rootDisposable, configuration, arguments, messageCollector)
?: return COMPILATION_ERROR ?: return COMPILATION_ERROR
registerJavacIfNeeded(environment, arguments).let { registerJavacIfNeeded(environment, arguments).let {
@@ -166,7 +166,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
configuration.put(JVMConfigurationKeys.RETAIN_OUTPUT_IN_MEMORY, true) configuration.put(JVMConfigurationKeys.RETAIN_OUTPUT_IN_MEMORY, true)
val environment = createEnvironmentWithScriptingSupport(rootDisposable, configuration, arguments, messageCollector) val environment = createCoreEnvironment(rootDisposable, configuration, arguments, messageCollector)
?: return COMPILATION_ERROR ?: return COMPILATION_ERROR
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(environment.project) val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(environment.project)
@@ -192,7 +192,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
} }
} }
val environment = createEnvironmentWithScriptingSupport(rootDisposable, configuration, arguments, messageCollector) val environment = createCoreEnvironment(rootDisposable, configuration, arguments, messageCollector)
?: return COMPILATION_ERROR ?: return COMPILATION_ERROR
registerJavacIfNeeded(environment, arguments).let { registerJavacIfNeeded(environment, arguments).let {
@@ -263,33 +263,30 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
return true return true
} }
private fun createEnvironmentWithScriptingSupport(rootDisposable: Disposable, private fun createCoreEnvironment(
configuration: CompilerConfiguration, rootDisposable: Disposable,
arguments: K2JVMCompilerArguments, configuration: CompilerConfiguration,
messageCollector: MessageCollector arguments: K2JVMCompilerArguments,
messageCollector: MessageCollector
): KotlinCoreEnvironment? { ): KotlinCoreEnvironment? {
val scriptResolverEnv = createScriptResolverEnvironment(arguments, messageCollector) ?: return null val scriptResolverEnv = createScriptResolverEnvironment(arguments, messageCollector) ?: return null
configureScriptDefinitions(arguments.scriptTemplates, configuration, messageCollector, scriptResolverEnv) configureScriptDefinitions(arguments.scriptTemplates, configuration, messageCollector, scriptResolverEnv)
if (!messageCollector.hasErrors()) { if (messageCollector.hasErrors()) return null
val environment = createCoreEnvironment(rootDisposable, configuration)
if (!messageCollector.hasErrors()) {
scriptResolverEnv.put("projectRoot", environment.project.run { basePath ?: baseDir?.canonicalPath }?.let(::File))
return environment
}
}
return null
}
private fun createCoreEnvironment(rootDisposable: Disposable, configuration: CompilerConfiguration): KotlinCoreEnvironment { val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
val result = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
if (initStartNanos != 0L) { if (initStartNanos != 0L) {
val initNanos = System.nanoTime() - initStartNanos val initNanos = System.nanoTime() - initStartNanos
reportPerf(configuration, "INIT: Compiler initialized in " + TimeUnit.NANOSECONDS.toMillis(initNanos) + " ms") reportPerf(configuration, "INIT: Compiler initialized in " + TimeUnit.NANOSECONDS.toMillis(initNanos) + " ms")
initStartNanos = 0L initStartNanos = 0L
} }
return result
if (!messageCollector.hasErrors()) {
scriptResolverEnv.put("projectRoot", environment.project.run { basePath ?: baseDir?.canonicalPath }?.let(::File))
return environment
}
return null
} }
override fun setupPlatformSpecificArgumentsAndServices( override fun setupPlatformSpecificArgumentsAndServices(