6.7.0 - Alpha6 - Gradle 构建脚本支持自动生成 VersionCodesList 类所需数据以降低脚本启动延迟

This commit is contained in:
SuperMonster003
2025-10-09 17:12:26 +08:00
parent aa603aac44
commit 0ff16b8ec0
20 changed files with 412 additions and 182 deletions

View File

@@ -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("\"", "\\\"") + "\""
}

View File

@@ -0,0 +1 @@
org.autojs.autojs.ksp.VersionCodesProcessorProvider

View File

@@ -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 VERSION_CODE RELEASE_NAME INTERNAL_CODENAME PLATFORM_VERSION API_LEVEL RELEASE_DATE
2 BAKLAVA Android 16 Baklava 16 36 June 10, 2025
3 VANILLA_ICE_CREAM Android 15 Vanilla Ice Cream 15 35 September 3, 2024
4 UPSIDE_DOWN_CAKE Android 14 Upside Down Cake 14 34 October 4, 2023
5 TIRAMISU Android 13 Tiramisu 13 33 August 15, 2022
6 S_V2 Android 12L Snow Cone v2 12.1 32 March 7, 2022
7 S Android 12 Snow Cone 12 31 October 4, 2021
8 R Android 11 Red Velvet Cake 11 30 September 8, 2020
9 Q Android 10 Quince Tart 10 29 September 3, 2019
10 P Android Pie Pistachio Ice Cream 9 28 August 6, 2018
11 O_MR1 Android Oreo Oatmeal Cookie 8.1 27 December 5, 2017
12 O Android Oreo Oatmeal Cookie 8.0 26 August 21, 2017
13 N_MR1 Android Nougat New York Cheesecake 7.1-7.1.2 25 October 4, 2016
14 N Android Nougat New York Cheesecake 7.0 24 August 22, 2016
15 M Android Marshmallow Macadamia Nut Cookie 6.0-6.0.1 23 September 29, 2015
16 LOLLIPOP_MR1 Android Lollipop Lemon Meringue Pie 5.1-5.1.1 22 March 2, 2015
17 LOLLIPOP Android Lollipop Lemon Meringue Pie 5.0-5.0.2 21 November 4, 2014
18 KITKAT_WATCH Android KitKat Key Lime Pie 4.4W-4.4W.2 20 June 25, 2014
19 KITKAT Android KitKat Key Lime Pie 4.4-4.4.4 19 October 31, 2013
20 JELLY_BEAN_MR2 Android Jelly Bean Jelly Bean 4.3-4.3.1 18 July 24, 2013
21 JELLY_BEAN_MR1 Android Jelly Bean Jelly Bean 4.2-4.2.2 17 November 13, 2012
22 JELLY_BEAN Android Jelly Bean Jelly Bean 4.1-4.1.2 16 July 9, 2012
23 ICE_CREAM_SANDWICH_MR1 Android Ice Cream Sandwich Ice Cream Sandwich 4.0.3-4.0.4 15 December 16, 2011
24 ICE_CREAM_SANDWICH Android Ice Cream Sandwich Ice Cream Sandwich 4.0-4.0.2 14 October 18, 2011
25 HONEYCOMB_MR2 Android Honeycomb Honeycomb 3.2-3.2.6 13 July 15, 2011
26 HONEYCOMB_MR1 Android Honeycomb Honeycomb 3.1 12 May 10, 2011
27 HONEYCOMB Android Honeycomb Honeycomb 3.0 11 February 22, 2011
28 GINGERBREAD_MR1 Android Gingerbread Gingerbread 2.3.3-2.3.7 10 February 9, 2011
29 GINGERBREAD Android Gingerbread Gingerbread 2.3-2.3.2 9 December 6, 2010
30 FROYO Android Froyo Froyo 2.2-2.2.3 8 May 20, 2010
31 ECLAIR_MR1 Android Eclair Eclair 2.1 7 January 11, 2010
32 ECLAIR_0_1 Android Eclair Eclair 2.0.1 6 December 3, 2009
33 ECLAIR Android Eclair Eclair 2.0 5 October 27, 2009
34 DONUT Android Donut Donut 1.6 4 September 15, 2009
35 CUPCAKE Android Cupcake Cupcake 1.5 3 April 27, 2009
36 BASE_1_1 Android 1.1 Petit Four 1.1 2 February 9, 2009
37 BASE Android 1.0 1.0 1 September 23, 2008