Read KotlinCompilerVersion from resource file

Writing build number into a public constant field leads to poor gradle
cache reuse between different builds. Public constant value is a part of
public api and its changes affect inputs of dependent modules.
Extracting build number to resource file allows to ignore it from
runtime classpath which fixes same problem for KotlinCompile tasks
This commit is contained in:
Vyacheslav Gerasimov
2020-02-12 21:41:13 +03:00
parent f8437743c5
commit c2457cae60
6 changed files with 49 additions and 24 deletions
+1
View File
@@ -393,6 +393,7 @@ allprojects {
normalization {
runtimeClasspath {
ignore("META-INF/MANIFEST.MF")
ignore("META-INF/compiler.version")
}
}
+9 -2
View File
@@ -1,3 +1,4 @@
import org.apache.tools.ant.filters.ReplaceTokens
plugins {
java
@@ -8,6 +9,8 @@ plugins {
jvmTarget = "1.6"
javaHome = rootProject.extra["JDK_16"] as String
val kotlinVersion: String by rootProject.extra
dependencies {
compileOnly(kotlinStdlib())
}
@@ -22,5 +25,9 @@ tasks.withType<JavaCompile> {
targetCompatibility = "1.6"
}
if (project.hasProperty("teamcity"))
tasks["compileJava"].dependsOn(":prepare:build.version:writeCompilerVersion")
tasks.named<ProcessResources>("processResources") {
inputs.property("compilerVersion", kotlinVersion)
filesMatching("META-INF/compiler.version") {
filter<ReplaceTokens>("tokens" to mapOf("snapshot" to kotlinVersion))
}
}
@@ -0,0 +1 @@
@snapshot@
@@ -18,10 +18,13 @@ package org.jetbrains.kotlin.config;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KotlinCompilerVersion {
// The value of this constant is generated by the build script
// DON'T MODIFY IT
public static final String VERSION = "@snapshot@";
public static final String VERSION_FILE_PATH = "/META-INF/compiler.version";
public static final String VERSION;
// True if the latest stable language version supported by this compiler has not yet been released.
// Binaries produced by this compiler with that language version (or any future language version) are going to be marked
@@ -45,12 +48,27 @@ public class KotlinCompilerVersion {
*/
@Nullable
public static String getVersion() {
//noinspection ConstantConditions
return VERSION.equals("@snapshot@") ? null : VERSION;
}
private static String loadKotlinCompilerVersion() throws IOException {
BufferedReader versionReader = new BufferedReader(
new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH)));
try {
return versionReader.readLine();
} finally {
versionReader.close();
}
}
static {
//noinspection ConstantConditions
try {
VERSION = loadKotlinCompilerVersion();
}
catch (IOException e) {
throw new IllegalStateException("Failed to read compiler version from " + VERSION_FILE_PATH);
}
if (!VERSION.equals("@snapshot@") && !VERSION.contains("-") && IS_PRE_RELEASE) {
throw new IllegalStateException(
"IS_PRE_RELEASE cannot be true for a compiler without '-' in its version.\n" +
@@ -52,12 +52,22 @@ internal fun loadCompilerVersion(compilerClasspath: List<File>): String {
for (cpFile in compilerClasspath) {
if (cpFile.isFile && cpFile.extension.toLowerCase() == "jar") {
ZipFile(cpFile).use { jar ->
val bytes = jar.getInputStream(jar.getEntry(versionClassFileName)).use { it.readBytes() }
checkVersion(bytes)
val versionFileEntry = jar.getEntry(KotlinCompilerVersion.VERSION_FILE_PATH)
if (versionFileEntry != null) {
result = jar.getInputStream(versionFileEntry).bufferedReader().use { it.readText() }
} else {
val bytes = jar.getInputStream(jar.getEntry(versionClassFileName)).use { it.readBytes() }
checkVersion(bytes)
}
}
} else if (cpFile.isDirectory) {
File(cpFile, versionClassFileName).takeIf { it.isFile }?.let {
checkVersion(it.readBytes())
val versionFile = File(cpFile, KotlinCompilerVersion.VERSION_FILE_PATH)
if (versionFile.isFile) {
result = versionFile.readText()
} else {
File(cpFile, versionClassFileName).takeIf { it.isFile }?.let {
checkVersion(it.readBytes())
}
}
}
if (result != null) break
+1 -13
View File
@@ -44,18 +44,6 @@ val writeStdlibVersion by tasks.registering {
}
}
val writeCompilerVersion by tasks.registering {
val versionFile = rootDir.resolve("core/util.runtime/src/org/jetbrains/kotlin/config/KotlinCompilerVersion.java")
inputs.property("version", kotlinVersion)
outputs.file(versionFile)
doLast {
replaceVersion(versionFile, """public static final String VERSION = "([^"]+)"""") {
logger.lifecycle("Writing new compiler version: $kotlinVersion")
kotlinVersion
}
}
}
val writePluginVersion by tasks.registering {
val versionFile = project(":idea").projectDir.resolve("resources/META-INF/plugin.xml")
val pluginVersion = rootProject.findProperty("pluginVersion") as String?
@@ -71,5 +59,5 @@ val writePluginVersion by tasks.registering {
}
val writeVersions by tasks.registering {
dependsOn(writeBuildNumber, writeStdlibVersion, writeCompilerVersion)
dependsOn(writeBuildNumber, writeStdlibVersion)
}