From e79fdbe5e87f1f1ec582e308ba6fa0b234703fbf Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 26 Apr 2017 21:11:45 +0300 Subject: [PATCH] Gradle: print version loaded from environment's compiler jar And not from the KotlinCompilerVersion class that is accessible in the current version of kotlin-gradle-plugin, because the version of the compiler might be different --- .../GradleKotlinCompilerRunner.kt | 3 +- .../jetbrains/kotlin/compilerRunner/utils.kt | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/utils.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt index ffc7eda357c..a2ac5a98b03 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments import org.jetbrains.kotlin.com.intellij.openapi.util.io.FileUtil -import org.jetbrains.kotlin.config.KotlinCompilerVersion import org.jetbrains.kotlin.config.Services import org.jetbrains.kotlin.daemon.client.CompileServiceSession import org.jetbrains.kotlin.daemon.common.* @@ -102,7 +101,7 @@ internal class GradleCompilerRunner(private val project: Project) : KotlinCompil environment: GradleCompilerEnvironment ): ExitCode { if (compilerArgs.version) { - project.logger.lifecycle("Kotlin version " + KotlinCompilerVersion.VERSION + + project.logger.lifecycle("Kotlin version " + loadCompilerVersion(environment.compilerJar) + " (JRE " + System.getProperty("java.runtime.version") + ")") compilerArgs.version = false } diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/utils.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/utils.kt new file mode 100644 index 00000000000..df30739afcd --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/utils.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.compilerRunner + +import org.jetbrains.kotlin.config.KotlinCompilerVersion +import org.jetbrains.org.objectweb.asm.ClassReader +import org.jetbrains.org.objectweb.asm.ClassVisitor +import org.jetbrains.org.objectweb.asm.FieldVisitor +import org.jetbrains.org.objectweb.asm.Opcodes +import java.io.File +import java.util.zip.ZipFile + +internal fun loadCompilerVersion(compilerJar: File): String { + var result = "" + + try { + ZipFile(compilerJar).use { file -> + val fileName = KotlinCompilerVersion::class.java.name.replace('.', '/') + ".class" + file.getInputStream(file.getEntry(fileName)).use { inputStream -> + ClassReader(inputStream).accept(object : ClassVisitor(Opcodes.ASM5) { + override fun visitField(access: Int, name: String, desc: String, signature: String?, value: Any?): FieldVisitor { + if (name == KotlinCompilerVersion::VERSION.name && value is String) { + result = value + } + return super.visitField(access, name, desc, signature, value) + } + }, ClassReader.SKIP_CODE or ClassReader.SKIP_FRAMES or ClassReader.SKIP_DEBUG) + } + } + } + catch (e: Throwable) {} + + return result +}