6.7.0 - Alpha6 - Gradle 构建脚本支持自动生成 VersionCodesList 类所需数据以降低脚本启动延迟
This commit is contained in:
49
build-logic/build.gradle.kts
Normal file
49
build-logic/build.gradle.kts
Normal file
@@ -0,0 +1,49 @@
|
||||
import java.util.*
|
||||
|
||||
plugins {
|
||||
`kotlin-dsl` /* kotlin("jvm") */
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
google()
|
||||
}
|
||||
|
||||
gradle.extra["jdk"] = run determineBuildLogicJdk@{
|
||||
val propsFile: File = rootDir.parentFile.resolve("version.properties")
|
||||
val minSupported: Int = Properties().let { props ->
|
||||
require(propsFile.isFile) {
|
||||
"version.properties not found in root directory"
|
||||
}
|
||||
propsFile.inputStream().use { props.load(it) }
|
||||
val minSupportedVersion = props.getProperty("JAVA_VERSION_MIN_SUPPORTED")
|
||||
require(minSupportedVersion != null) {
|
||||
"version.properties does not contain \"JAVA_VERSION_MIN_SUPPORTED\""
|
||||
}
|
||||
minSupportedVersion.toInt()
|
||||
}
|
||||
val current = JavaVersion.current().majorVersion.toIntOrNull() ?: minSupported
|
||||
|
||||
fun tryAdjustByKotlinJvmTarget(sourceVersion: Int): Int {
|
||||
return runCatching {
|
||||
val cls = Class.forName("org.jetbrains.kotlin.gradle.dsl.JvmTarget")
|
||||
val enumConstants = cls.enumConstants ?: return sourceVersion
|
||||
var tmpVersion = sourceVersion
|
||||
while (tmpVersion > minSupported) {
|
||||
val wanted = "JVM_$tmpVersion"
|
||||
if (enumConstants.any { it?.toString().equals(wanted, ignoreCase = true) }) {
|
||||
return tmpVersion
|
||||
}
|
||||
tmpVersion -= 1
|
||||
}
|
||||
return@runCatching sourceVersion
|
||||
}.getOrDefault(sourceVersion)
|
||||
}
|
||||
|
||||
tryAdjustByKotlinJvmTarget(maxOf(current, minSupported)).also {
|
||||
println("Toolchain: selected [$it] / current [$current] / min [$minSupported]")
|
||||
}
|
||||
}.also { jdk ->
|
||||
kotlin { jvmToolchain(jdk) }
|
||||
java { toolchain.languageVersion.set(JavaLanguageVersion.of(jdk)) }
|
||||
}
|
||||
Reference in New Issue
Block a user