6.7.0 - Alpha6 - Gradle 构建脚本支持自动生成 VersionCodesList 类所需数据以降低脚本启动延迟
This commit is contained in:
2
build-logic/.gitignore
vendored
Normal file
2
build-logic/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
/build/
|
||||
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)) }
|
||||
}
|
||||
6
build-logic/convention/.gitignore
vendored
6
build-logic/convention/.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
/build
|
||||
/.gradle
|
||||
/.kotlin
|
||||
/.gradle/
|
||||
/.kotlin/
|
||||
/build/
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import java.util.Properties
|
||||
|
||||
plugins {
|
||||
`kotlin-dsl` /* kotlin("jvm") */
|
||||
`java-gradle-plugin`
|
||||
@@ -7,6 +5,7 @@ plugins {
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
google()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -19,28 +18,28 @@ dependencies {
|
||||
gradlePlugin {
|
||||
plugins {
|
||||
register("utils") {
|
||||
id = "org.autojs.build.utils"
|
||||
implementationClass = "org.autojs.build.UtilsPlugin"
|
||||
displayName = "AutoJs6 Build Utils Plugin"
|
||||
description = "Provides utilities for downloading, extracting archives, and version helpers."
|
||||
id = "org.autojs.build.utils"
|
||||
implementationClass = "org.autojs.build.UtilsPlugin"
|
||||
displayName = "AutoJs6 Build Utils Plugin"
|
||||
description = "Provides utilities for downloading, extracting archives, and version helpers."
|
||||
}
|
||||
register("versions") {
|
||||
id = "org.autojs.build.versions"
|
||||
implementationClass = "org.autojs.build.VersionsPlugin"
|
||||
displayName = "AutoJs6 Versions Plugin"
|
||||
description = "Provides version helpers."
|
||||
id = "org.autojs.build.versions"
|
||||
implementationClass = "org.autojs.build.VersionsPlugin"
|
||||
displayName = "AutoJs6 Versions Plugin"
|
||||
description = "Provides version helpers."
|
||||
}
|
||||
register("signs") {
|
||||
id = "org.autojs.build.signs"
|
||||
implementationClass = "org.autojs.build.SignsPlugin"
|
||||
displayName = "AutoJs6 Signs Plugin"
|
||||
description = "Provides signing helpers."
|
||||
id = "org.autojs.build.signs"
|
||||
implementationClass = "org.autojs.build.SignsPlugin"
|
||||
displayName = "AutoJs6 Signs Plugin"
|
||||
description = "Provides signing helpers."
|
||||
}
|
||||
register("properties") {
|
||||
id = "org.autojs.build.properties"
|
||||
implementationClass = "org.autojs.build.PropertiesPlugin"
|
||||
displayName = "AutoJs6 Properties Plugin"
|
||||
description = "Provides properties helpers."
|
||||
id = "org.autojs.build.properties"
|
||||
implementationClass = "org.autojs.build.PropertiesPlugin"
|
||||
displayName = "AutoJs6 Properties Plugin"
|
||||
description = "Provides properties helpers."
|
||||
}
|
||||
register("jvmConvention") {
|
||||
id = "org.autojs.build.jvm-convention"
|
||||
@@ -51,48 +50,7 @@ gradlePlugin {
|
||||
}
|
||||
}
|
||||
|
||||
run determineBuildSrcJdk@{
|
||||
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]")
|
||||
}
|
||||
}.let { jdk ->
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(jdk)
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain.languageVersion.set(JavaLanguageVersion.of(jdk))
|
||||
}
|
||||
|
||||
(gradle.extra["jdk"] as Int).let { jdk ->
|
||||
kotlin { jvmToolchain(jdk) }
|
||||
java { toolchain.languageVersion.set(JavaLanguageVersion.of(jdk)) }
|
||||
}
|
||||
|
||||
@@ -66,14 +66,22 @@ object Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一 "版本信息打印 + 部署 + 清理" 生命周期钩子.
|
||||
* Unified lifecycle hooks for "version info printing + deployment + cleanup".
|
||||
*
|
||||
* @param project 当前模块 Project
|
||||
* @param projectDisplayName 用于打印的项目展示名 (默认用模块名)
|
||||
* @param versionLines 版本信息行 (如 ["OpenCV: 4.2.0", "NDK: 21.1.6352462"])
|
||||
* @param libsToDeploy 需要部署/清理的 LibDeployer 列表
|
||||
* @param cleanupFlagKey gradle.ext 的布尔开关键 (如 "isCleanupPaddleOcr"), null 表示不参与清理逻辑
|
||||
* @param extraFilesToDeleteOnClean clean 时额外需删除的相对路径
|
||||
* zh-CN: 统一 "版本信息打印 + 部署 + 清理" 生命周期钩子.
|
||||
*
|
||||
* @param project Current module Project.<br>
|
||||
* zh-CN: 当前模块 Project.
|
||||
* @param projectDisplayName Project display name for printing (defaults to module name).<br>
|
||||
* zh-CN: 用于打印的项目展示名 (默认用模块名).
|
||||
* @param versionLines Version information lines (e.g. ["OpenCV: 4.2.0", "NDK: 21.1.6352462"]).<br>
|
||||
* zh-CN: 版本信息行 (如 ["OpenCV: 4.2.0", "NDK: 21.1.6352462"]).
|
||||
* @param libsToDeploy List of LibDeployer objects that need to be deployed/cleaned.<br>
|
||||
* zh-CN: 需要部署/清理的 LibDeployer 列表.
|
||||
* @param cleanupFlagKey Boolean switch key in gradle.ext (e.g. "isCleanupPaddleOcr"), null means not participating in cleanup logic.<br>
|
||||
* zh-CN: gradle.ext 的布尔开关键 (如 "isCleanupPaddleOcr"), null 表示不参与清理逻辑.
|
||||
* @param extraFilesToDeleteOnClean Additional relative paths to delete during clean.<br>
|
||||
* zh-CN: clean 时额外需删除的相对路径.
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun configureLibraryLifecycleHooks(
|
||||
@@ -87,10 +95,12 @@ object Utils {
|
||||
val gradle = project.gradle
|
||||
val onlyClean = AtomicBoolean(false)
|
||||
|
||||
// 单一监听器: 既判断 "是否纯 clean", 也负责非 clean 流程的打印与部署
|
||||
// Single listener: both determines "is pure clean"
|
||||
// and handles non-clean flow printing and deployment.
|
||||
// zh-CN: 单一监听器: 既判断 "是否纯 clean", 也负责非 clean 流程的打印与部署.
|
||||
gradle.taskGraph.addTaskExecutionGraphListener { graph ->
|
||||
val all = graph.allTasks
|
||||
val isOnlyClean = all.isNotEmpty() && all.all { it.name.contains("clean", ignoreCase = true) }
|
||||
val tasks = graph.allTasks
|
||||
val isOnlyClean = tasks.isNotEmpty() && tasks.all { it.name.contains("clean", ignoreCase = true) }
|
||||
onlyClean.set(isOnlyClean)
|
||||
|
||||
if (!isOnlyClean) {
|
||||
@@ -103,7 +113,8 @@ object Utils {
|
||||
}
|
||||
}
|
||||
|
||||
// clean 钩子 (显式 Java SAM, 避免 Kotlin/Groovy 重载歧义)
|
||||
// "clean" hooks (explicit Java SAM to avoid Kotlin/Groovy overload ambiguity).
|
||||
// zh-CN: "clean" 钩子 (显式 Java SAM, 避免 Kotlin/Groovy 重载歧义).
|
||||
@Suppress("ObjectLiteralToLambda")
|
||||
project.tasks.named("clean").configure(object : Action<Task> {
|
||||
override fun execute(cleanTask: Task) {
|
||||
@@ -113,7 +124,8 @@ object Utils {
|
||||
project.delete(project.file(rel))
|
||||
}
|
||||
|
||||
// 未提供开关键则直接跳过清理逻辑
|
||||
// Skip the cleanup logic if no cleanup flag key is provided.
|
||||
// zh-CN: 未提供开关键则直接跳过清理逻辑.
|
||||
val key = cleanupFlagKey ?: return@doFirst
|
||||
val cleanupEnabled = gradle.extra.require<Boolean>(key)
|
||||
if (cleanupEnabled && onlyClean.get()) {
|
||||
@@ -128,14 +140,22 @@ object Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册模板 APK 拷贝: 在指定 assemble 任务完成后, 将 universal APK 拷贝为 assets 模板.
|
||||
* Register template APK copying: After the specified assemble task is completed, copy universal APK as assets template.
|
||||
*
|
||||
* @param project 当前模块 Project
|
||||
* @param taskName 构建任务名 (如 "assembleInrtRelease")
|
||||
* @param srcDir APK 输出目录 (如 "build/outputs/apk/inrt/release")
|
||||
* @param destDir 目标目录 (如 "src/main/assets-app")
|
||||
* @param templateApkName 模板文件名 (如 "template.apk")
|
||||
* @param universalNameFn 从版本名得出源 APK 名的函数 (默认 inrt-v<ver>-universal.apk)
|
||||
* zh-CN: 注册模板 APK 拷贝: 在指定 assemble 任务完成后, 将 universal APK 拷贝为 assets 模板.
|
||||
*
|
||||
* @param project Current module Project.<br>
|
||||
* zh-CN: 当前模块 Project.
|
||||
* @param taskName Build task name (like "assembleInrtRelease").<br>
|
||||
* zh-CN: 构建任务名 (如 "assembleInrtRelease").
|
||||
* @param srcDir APK output directory (like "build/outputs/apk/inrt/release").<br>
|
||||
* zh-CN: APK 输出目录 (如 "build/outputs/apk/inrt/release").
|
||||
* @param destDir Target directory (like "src/main/assets-app").<br>
|
||||
* zh-CN: 目标目录 (如 "src/main/assets-app").
|
||||
* @param templateApkName Template file name (like "template.apk").<br>
|
||||
* zh-CN: 模板文件名 (如 "template.apk").
|
||||
* @param universalNameFn Function to derive source APK name from version name (default inrt-v<ver>-universal.apk).<br>
|
||||
* zh-CN: 从版本名得出源 APK 名的函数 (默认 inrt-v<ver>-universal.apk).
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun registerTemplateApkCopy(
|
||||
@@ -149,7 +169,8 @@ object Utils {
|
||||
val versions = newVersions(project)
|
||||
val versionName = versions.appVersionName
|
||||
|
||||
// 待所有项目评估完成后再定位并配置任务, 避免早期查找不到任务
|
||||
// Wait for all projects to be evaluated before locating and configuring tasks to avoid early lookup failure.
|
||||
// zh-CN: 待所有项目评估完成后再定位并配置任务, 避免早期查找不到任务.
|
||||
project.gradle.projectsEvaluated {
|
||||
val assembleTask = project.tasks.findByName(taskName)
|
||||
if (assembleTask == null) {
|
||||
@@ -207,15 +228,21 @@ object Utils {
|
||||
|
||||
private fun capitalize(s: String) = "${s[0].uppercase(Locale.getDefault())}${s.substring(1)}"
|
||||
|
||||
/**
|
||||
* 统一为 Android 模块配置 Java/Kotlin 的目标版本:
|
||||
* - Android.compileOptions.sourceCompatibility/targetCompatibility = versions.javaVersion
|
||||
* - Kotlin 编译任务的 jvmTarget = versions.javaVersion 对应级别
|
||||
*
|
||||
* 注意:
|
||||
* - Android 扩展在 projectsEvaluated 后稳定可得
|
||||
* - Kotlin 任务用 tasks.configureEach 动态配置, 任务何时创建都能命中
|
||||
*/
|
||||
// Configure Java/Kotlin target version uniformly for Android modules:
|
||||
// - Android.compileOptions.sourceCompatibility/targetCompatibility = versions.javaVersion
|
||||
// - Kotlin compile tasks jvmTarget = versions.javaVersion level
|
||||
// Note:
|
||||
// - Android extension is stably available after projectsEvaluated
|
||||
// - Kotlin tasks use tasks.configureEach for dynamic configuration to catch whenever tasks are created
|
||||
//
|
||||
// zh-CN:
|
||||
//
|
||||
// 统一为 Android 模块配置 Java/Kotlin 的目标版本:
|
||||
// - Android.compileOptions.sourceCompatibility/targetCompatibility = versions.javaVersion
|
||||
// - Kotlin 编译任务的 jvmTarget = versions.javaVersion 对应级别
|
||||
// 注意:
|
||||
// - Android 扩展在 projectsEvaluated 后稳定可得
|
||||
// - Kotlin 任务用 tasks.configureEach 动态配置, 任务何时创建都能命中
|
||||
@JvmStatic
|
||||
fun configureJvmForAndroidModule(project: Project) {
|
||||
val versions = newVersions(project)
|
||||
@@ -224,11 +251,15 @@ object Utils {
|
||||
val installer = {
|
||||
project.logInfo("[JvmConv] Detected Android plugin in module='${project.path}', installing configuration")
|
||||
|
||||
// A) Java: 使用 Toolchain (模块级 + 任务级), 不要设置 --release
|
||||
// Java: Use Toolchain (module-level + task-level), do not set --release.
|
||||
// zh-CN: Java: 使用 Toolchain (模块级 + 任务级), 不要设置 --release.
|
||||
|
||||
configureJavaToolchainLanguageLevel(project, versions.javaVersionInt)
|
||||
configureJavaToolchainForAllJavaCompile(project, versions.javaVersionInt)
|
||||
|
||||
// B) Kotlin: 继续懒配置设置 jvmTarget (你之前已验证成功)
|
||||
// Kotlin: Continue lazy configuration to set jvmTarget.
|
||||
// zh-CN: Kotlin: 继续懒配置设置 jvmTarget.
|
||||
|
||||
configureKotlinJvmTargetLazily(project, versions)
|
||||
|
||||
project.logInfo("[JvmConv] Installed Java toolchain (module+tasks) and Kotlin jvmTarget for '${project.path}'")
|
||||
@@ -238,7 +269,8 @@ object Utils {
|
||||
project.plugins.withId("com.android.library") { installer() }
|
||||
}
|
||||
|
||||
// 模块级 Toolchain: 让 AGP/Gradle 知道本模块应使用的 JDK 语言级别
|
||||
// Module-level toolchain: Let AGP/Gradle know which JDK language level should be used for this module.
|
||||
// zh-CN: 模块级 Toolchain: 让 AGP/Gradle 知道本模块应使用的 JDK 语言级别.
|
||||
private fun configureJavaToolchainLanguageLevel(project: Project, target: Int) {
|
||||
val javaExt = project.extensions.findByType(JavaPluginExtension::class.java)
|
||||
if (javaExt == null) {
|
||||
@@ -254,7 +286,8 @@ object Utils {
|
||||
}
|
||||
}
|
||||
|
||||
// 任务级 Toolchain: 对所有 JavaCompile 指定 javaCompiler, 且不要设置 --release (AGP 禁止)
|
||||
// Task-level Toolchain: Specify javaCompiler for all JavaCompile tasks, without setting --release (prohibited by AGP).
|
||||
// zh-CN: 任务级 Toolchain: 对所有 JavaCompile 指定 javaCompiler, 且不要设置 --release (AGP 禁止).
|
||||
private fun configureJavaToolchainForAllJavaCompile(project: Project, target: Int) {
|
||||
val toolchains = runCatching {
|
||||
project.extensions.getByType(JavaToolchainService::class.java)
|
||||
@@ -272,17 +305,20 @@ object Utils {
|
||||
}.onFailure {
|
||||
project.logError("[JvmConv] '${t.path}' set javaCompiler(toolchain) failed: ${it.message}", it)
|
||||
}
|
||||
|
||||
// 不要设置 t.options.release, AGP 会报错阻止
|
||||
// 也不强制改写 sourceCompatibility/targetCompatibility, 交给 AGP + toolchain 统一管理
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 为 KotlinCompile 任务设置 jvmTarget: 使用 configureEach, 任务实现时自动应用; 兼容新旧 API
|
||||
// Configure jvmTarget for KotlinCompile tasks:
|
||||
// Use configureEach for lazy configuration on task creation;
|
||||
// Compatible with old and new APIs.
|
||||
// zh-CN:
|
||||
// 为 KotlinCompile 任务设置 jvmTarget:
|
||||
// 使用 configureEach, 任务实现时自动应用;
|
||||
// 兼容新旧 API.
|
||||
private fun configureKotlinJvmTargetLazily(project: Project, versions: Versions) {
|
||||
val desiredStr = versions.javaVersionString // 例如 "22"
|
||||
val desiredEnumName = "JVM_${desiredStr}" // 例如 "JVM_22"
|
||||
val desiredStr = versions.javaVersionString // e.g. "22"
|
||||
val desiredEnumName = "JVM_${desiredStr}" // e.g. "JVM_22"
|
||||
project.logInfo("[JvmConv] Will configure Kotlin jvmTarget lazily to '$desiredEnumName' in '${project.path}'")
|
||||
|
||||
project.tasks.configureEach(object : Action<Task> {
|
||||
@@ -291,7 +327,8 @@ object Utils {
|
||||
|
||||
project.logInfo("[JvmConv] <KotlinTask> '${task.path}' class='${task.javaClass.name}'")
|
||||
|
||||
// 优先尝试 Kotlin 2.x: compilerOptions.jvmTarget(Property<JvmTarget>)
|
||||
// Try Kotlin 2.x first: compilerOptions.jvmTarget(Property<JvmTarget>).
|
||||
// zh-CN: 优先尝试 Kotlin 2.x: compilerOptions.jvmTarget(Property<JvmTarget>).
|
||||
val compilerOptions = runCatching {
|
||||
task.javaClass.methods.firstOrNull { it.name == "getCompilerOptions" && it.parameterTypes.isEmpty() }
|
||||
?.also { project.logInfo("[JvmConv] '${task.path}' found method: ${it.toGenericString()}") }
|
||||
@@ -344,7 +381,8 @@ object Utils {
|
||||
project.logWarn("[JvmConv] '${task.path}' compilerOptions not found, trying legacy kotlinOptions")
|
||||
}
|
||||
|
||||
// 兼容旧 API: kotlinOptions.setJvmTarget(String)
|
||||
// Fallback to legacy API: kotlinOptions.setJvmTarget(String).
|
||||
// zh-CN: 兼容旧 API: kotlinOptions.setJvmTarget(String).
|
||||
val kotlinOptions = runCatching {
|
||||
task.javaClass.methods.firstOrNull { it.name == "getKotlinOptions" && it.parameterTypes.isEmpty() }
|
||||
?.also { project.logInfo("[JvmConv] '${task.path}' found method: ${it.toGenericString()}") }
|
||||
|
||||
2
build-logic/ksp-version-codes-processor/.gitignore
vendored
Normal file
2
build-logic/ksp-version-codes-processor/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
/build/
|
||||
20
build-logic/ksp-version-codes-processor/build.gradle.kts
Normal file
20
build-logic/ksp-version-codes-processor/build.gradle.kts
Normal file
@@ -0,0 +1,20 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
google()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(gradleApi())
|
||||
implementation(kotlin("stdlib"))
|
||||
implementation("com.google.devtools.ksp:symbol-processing-api:${System.getProperty("com.google.devtools.ksp")}")
|
||||
implementation(libs.kotlin.csv.jvm)
|
||||
}
|
||||
|
||||
(gradle.extra["jdk"] as Int).let { jdk ->
|
||||
kotlin { jvmToolchain(jdk) }
|
||||
java { toolchain.languageVersion.set(JavaLanguageVersion.of(jdk)) }
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
@file:Suppress("SameParameterValue", "AssignedValueIsNeverRead")
|
||||
|
||||
package org.autojs.autojs.ksp
|
||||
|
||||
import com.google.devtools.ksp.processing.Dependencies
|
||||
import com.google.devtools.ksp.processing.Resolver
|
||||
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
|
||||
import com.google.devtools.ksp.processing.SymbolProcessorProvider
|
||||
import com.google.devtools.ksp.symbol.KSAnnotated
|
||||
import java.io.BufferedReader
|
||||
|
||||
class VersionCodesProcessorProvider : SymbolProcessorProvider {
|
||||
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
|
||||
return VersionCodesProcessor(environment)
|
||||
}
|
||||
}
|
||||
|
||||
class VersionCodesProcessor(private val env: SymbolProcessorEnvironment) : SymbolProcessor {
|
||||
|
||||
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||
val rows = readFromResource("version-codes.csv")
|
||||
if (rows.isEmpty()) return emptyList()
|
||||
|
||||
val sorted = rows.sortedByDescending { it.apiLevel.toIntOrNull() ?: Int.MIN_VALUE }
|
||||
|
||||
val pkg = "org.autojs.autojs.runtime.api.augment.util"
|
||||
val fileName = "VersionCodesInfoGenerated"
|
||||
val fileExtension = "kt"
|
||||
val content = buildString {
|
||||
appendLine("// Generated by KSP. DO NOT EDIT.")
|
||||
appendLine("@file:Suppress(\"unused\",\"MemberVisibilityCanBePrivate\",\"RedundantVisibilityModifier\")")
|
||||
appendLine("package $pkg")
|
||||
appendLine()
|
||||
appendLine("import org.autojs.autojs.runtime.api.augment.util.VersionCodes")
|
||||
appendLine("import org.autojs.autojs.util.RhinoUtils.newNativeObject")
|
||||
appendLine("import org.mozilla.javascript.ScriptableObject.PERMANENT")
|
||||
appendLine("import org.mozilla.javascript.ScriptableObject.READONLY")
|
||||
appendLine()
|
||||
appendLine("internal object $fileName {")
|
||||
appendLine(" @JvmField")
|
||||
appendLine(" val list: List<VersionCodes.Info> = listOf(")
|
||||
sorted.forEachIndexed { idx, r ->
|
||||
val comma = if (idx < sorted.lastIndex) "," else ""
|
||||
appendLine(
|
||||
" VersionCodes.Info(${q(r.versionCode)}, ${q(r.releaseName)}, ${q(r.internalCodename)}, ${q(r.platformVersion)}, ${q(r.apiLevel)}, ${q(r.releaseDate)})$comma"
|
||||
)
|
||||
}
|
||||
appendLine(" )")
|
||||
appendLine()
|
||||
appendLine(" @JvmField")
|
||||
appendLine(" val obj = newNativeObject().apply {")
|
||||
sorted.forEach { r ->
|
||||
appendLine(" run {")
|
||||
appendLine(" val info = VersionCodes.Info(${q(r.versionCode)}, ${q(r.releaseName)}, ${q(r.internalCodename)}, ${q(r.platformVersion)}, ${q(r.apiLevel)}, ${q(r.releaseDate)})")
|
||||
appendLine(" defineProperty(${q(r.versionCode)}, { info.toNativeObject() }, null, READONLY or PERMANENT)")
|
||||
appendLine(" }")
|
||||
}
|
||||
appendLine(" }")
|
||||
appendLine("}")
|
||||
}
|
||||
val bytes = content.toByteArray()
|
||||
try {
|
||||
resolver.getNewFiles().find {
|
||||
it.fileName == "${fileName}.${fileExtension}" && it.packageName.asString() == pkg
|
||||
} ?: env.codeGenerator.createNewFile(
|
||||
dependencies = Dependencies(aggregating = false),
|
||||
packageName = pkg,
|
||||
fileName = fileName,
|
||||
extensionName = fileExtension,
|
||||
).use { it.write(bytes) }
|
||||
} catch (_: FileAlreadyExistsException) {
|
||||
// Already written in the same/previous round, skip directly.
|
||||
// zh-CN: 已被同一/前一轮写入, 直接跳过.
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
data class Row(
|
||||
val versionCode: String,
|
||||
val releaseName: String,
|
||||
val internalCodename: String,
|
||||
val platformVersion: String,
|
||||
val apiLevel: String,
|
||||
val releaseDate: String,
|
||||
)
|
||||
|
||||
private fun readFromResource(name: String): List<Row> {
|
||||
val url = javaClass.classLoader.getResource(name) ?: throw RuntimeException("Resource not found: $name")
|
||||
return url.openStream().bufferedReader().use { br -> parseCsv(br) }
|
||||
}
|
||||
|
||||
private fun parseCsv(br: BufferedReader): List<Row> {
|
||||
val rows = mutableListOf<Row>()
|
||||
var isHeader = true
|
||||
br.forEachLine { raw ->
|
||||
val line = raw.trim()
|
||||
if (line.isEmpty() || line.startsWith("#")) return@forEachLine
|
||||
if (isHeader) {
|
||||
isHeader = false; return@forEachLine
|
||||
}
|
||||
val cells = splitCsv(line)
|
||||
if (cells.size < 6) throw RuntimeException("Invalid line: $raw")
|
||||
rows += Row(
|
||||
versionCode = cells[0],
|
||||
releaseName = cells[1],
|
||||
internalCodename = cells[2],
|
||||
platformVersion = cells[3],
|
||||
apiLevel = cells[4],
|
||||
releaseDate = cells[5],
|
||||
)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Simple CSV implementation with minimal quote handling.
|
||||
// zh-CN: 简单 CSV, 此处实现了最小引号处理.
|
||||
private fun splitCsv(line: String): List<String> {
|
||||
val out = mutableListOf<String>()
|
||||
val sb = StringBuilder()
|
||||
var inQuotes = false
|
||||
var i = 0
|
||||
while (i < line.length) {
|
||||
val c = line[i]
|
||||
when {
|
||||
c == '"' -> {
|
||||
if (inQuotes && i + 1 < line.length && line[i + 1] == '"') {
|
||||
sb.append('"'); i++
|
||||
} else {
|
||||
inQuotes = !inQuotes
|
||||
}
|
||||
}
|
||||
c == ',' && !inQuotes -> {
|
||||
out += sb.toString(); sb.setLength(0)
|
||||
}
|
||||
else -> sb.append(c)
|
||||
}
|
||||
i++
|
||||
}
|
||||
out += sb.toString()
|
||||
return out.map { it.trim().removeSurrounding("\"") }
|
||||
}
|
||||
|
||||
private fun q(s: String) = "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.autojs.autojs.ksp.VersionCodesProcessorProvider
|
||||
@@ -0,0 +1,37 @@
|
||||
VERSION_CODE,RELEASE_NAME,INTERNAL_CODENAME,PLATFORM_VERSION,API_LEVEL,RELEASE_DATE
|
||||
BAKLAVA,Android 16,Baklava,16,36,"June 10, 2025"
|
||||
VANILLA_ICE_CREAM,Android 15,Vanilla Ice Cream,15,35,"September 3, 2024"
|
||||
UPSIDE_DOWN_CAKE,Android 14,Upside Down Cake,14,34,"October 4, 2023"
|
||||
TIRAMISU,Android 13,Tiramisu,13,33,"August 15, 2022"
|
||||
S_V2,Android 12L,Snow Cone v2,12.1,32,"March 7, 2022"
|
||||
S,Android 12,Snow Cone,12,31,"October 4, 2021"
|
||||
R,Android 11,Red Velvet Cake,11,30,"September 8, 2020"
|
||||
Q,Android 10,Quince Tart,10,29,"September 3, 2019"
|
||||
P,Android Pie,Pistachio Ice Cream,9,28,"August 6, 2018"
|
||||
O_MR1,Android Oreo,Oatmeal Cookie,8.1,27,"December 5, 2017"
|
||||
O,Android Oreo,Oatmeal Cookie,8.0,26,"August 21, 2017"
|
||||
N_MR1,Android Nougat,New York Cheesecake,7.1-7.1.2,25,"October 4, 2016"
|
||||
N,Android Nougat,New York Cheesecake,7.0,24,"August 22, 2016"
|
||||
M,Android Marshmallow,Macadamia Nut Cookie,6.0-6.0.1,23,"September 29, 2015"
|
||||
LOLLIPOP_MR1,Android Lollipop,Lemon Meringue Pie,5.1-5.1.1,22,"March 2, 2015"
|
||||
LOLLIPOP,Android Lollipop,Lemon Meringue Pie,5.0-5.0.2,21,"November 4, 2014"
|
||||
KITKAT_WATCH,Android KitKat,Key Lime Pie,4.4W-4.4W.2,20,"June 25, 2014"
|
||||
KITKAT,Android KitKat,Key Lime Pie,4.4-4.4.4,19,"October 31, 2013"
|
||||
JELLY_BEAN_MR2,Android Jelly Bean,Jelly Bean,4.3-4.3.1,18,"July 24, 2013"
|
||||
JELLY_BEAN_MR1,Android Jelly Bean,Jelly Bean,4.2-4.2.2,17,"November 13, 2012"
|
||||
JELLY_BEAN,Android Jelly Bean,Jelly Bean,4.1-4.1.2,16,"July 9, 2012"
|
||||
ICE_CREAM_SANDWICH_MR1,Android Ice Cream Sandwich,Ice Cream Sandwich,4.0.3-4.0.4,15,"December 16, 2011"
|
||||
ICE_CREAM_SANDWICH,Android Ice Cream Sandwich,Ice Cream Sandwich,4.0-4.0.2,14,"October 18, 2011"
|
||||
HONEYCOMB_MR2,Android Honeycomb,Honeycomb,3.2-3.2.6,13,"July 15, 2011"
|
||||
HONEYCOMB_MR1,Android Honeycomb,Honeycomb,3.1,12,"May 10, 2011"
|
||||
HONEYCOMB,Android Honeycomb,Honeycomb,3.0,11,"February 22, 2011"
|
||||
GINGERBREAD_MR1,Android Gingerbread,Gingerbread,2.3.3-2.3.7,10,"February 9, 2011"
|
||||
GINGERBREAD,Android Gingerbread,Gingerbread,2.3-2.3.2,9,"December 6, 2010"
|
||||
FROYO,Android Froyo,Froyo,2.2-2.2.3,8,"May 20, 2010"
|
||||
ECLAIR_MR1,Android Eclair,Eclair,2.1,7,"January 11, 2010"
|
||||
ECLAIR_0_1,Android Eclair,Eclair,2.0.1,6,"December 3, 2009"
|
||||
ECLAIR,Android Eclair,Eclair,2.0,5,"October 27, 2009"
|
||||
DONUT,Android Donut,Donut,1.6,4,"September 15, 2009"
|
||||
CUPCAKE,Android Cupcake,Cupcake,1.5,3,"April 27, 2009"
|
||||
BASE_1_1,Android 1.1,Petit Four,1.1,2,"February 9, 2009"
|
||||
BASE,Android 1.0,"",1.0,1,"September 23, 2008"
|
||||
|
@@ -1,5 +1,10 @@
|
||||
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
|
||||
|
||||
rootProject.name = "logic"
|
||||
|
||||
include(":convention")
|
||||
include(":ksp-version-codes-processor")
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -12,6 +17,20 @@ dependencyResolutionManagement {
|
||||
}
|
||||
}
|
||||
|
||||
include(":convention")
|
||||
pluginManagement {
|
||||
repositories {
|
||||
gradlePluginPortal()
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
plugins {
|
||||
id("org.gradle.toolchains.foojay-resolver-convention") version System.getProperty("org.gradle.toolchains.foojay-resolver-convention")
|
||||
}
|
||||
}
|
||||
|
||||
rootProject.name = "build-logic"
|
||||
plugins {
|
||||
// @Hint by SuperMonster003 on Oct 6, 2025.
|
||||
// ! Enable JDK auto-resolution/download capability for build modules.
|
||||
// ! zh-CN: 让构建模块具备 JDK 自动解析/下载能力.
|
||||
id("org.gradle.toolchains.foojay-resolver-convention")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user