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
@@ -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