6.7.0 - Alpha6 - 模块化 Gradle 脚本, 将共享构建逻辑迁移至 buildSrc 并抽象为约定插件

This commit is contained in:
SuperMonster003
2025-09-24 16:31:06 +08:00
parent 038f4a5ad9
commit 4778794e0e
31 changed files with 1893 additions and 1235 deletions

View File

@@ -0,0 +1,76 @@
package org.autojs.build
import org.gradle.api.Project
import java.io.File
import java.io.FileNotFoundException
import java.util.*
class BuildProperties private constructor(
private val file: File,
private val props: Properties,
) {
val path: String get() = file.absolutePath
operator fun get(propertyName: String): String = when {
propertyName.contains("/") -> {
get(propertyName.split("/"))
}
else -> requireValue(propertyName)
}
operator fun get(propertyInfo: List<String>): String {
val (lib, body) = propertyInfo
return requireValue(lib, body)
}
private fun requireString(key: String, alternate: String): String {
return props.getProperty(key)
?: props.getProperty(alternate)
?: throw IllegalStateException("Property '$key' is missing in '${file.absolutePath}'")
}
fun requireString(key: String): String {
return props.getProperty(key)
?: throw IllegalStateException("Property '$key' is missing in '${file.absolutePath}'")
}
fun requireInt(key: String): Int = requireString(key).toInt()
fun getIntOrNull(key: String): Int? = props.getProperty(key)?.toIntOrNull()
private fun requireValue(name: String): String {
return when (val value = requireString("${name}_VERSION", name)) {
"PUBLIC" -> requireString("PUBLIC_${name}_VERSION", "PUBLIC_${name}")
else -> value
}
}
private fun requireValue(lib: String, body: String): String {
return when (val value = requireString("${lib}_${body}_VERSION", "${lib}_${body}")) {
"PUBLIC" -> requireString("PUBLIC_${body}_VERSION", "PUBLIC_${body}")
else -> value
}
}
companion object {
@JvmStatic
fun loadFrom(project: Project): BuildProperties {
return loadFrom("${project.rootDir}/version.properties")
}
@JvmStatic
fun loadFrom(filePath: String): BuildProperties {
val f = File(filePath)
if (!f.canRead()) {
throw FileNotFoundException("Cannot read file '$filePath'")
}
val p = Properties()
f.inputStream().use { p.load(it) }
return BuildProperties(f, p)
}
}
}