Skip to content

Commit ae6491b

Browse files
authored
Improve reproducibility of BC-jda-ktx migrations (#240)
1 parent def6ef4 commit ae6491b

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

BotCommands-jda-ktx/src/test/kotlin/dev/freya02/botcommands/jda/ktx/MigrationSnapshotTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MigrationSnapshotTest {
2121
while (zipStream.nextEntry.also { entry = it } != null) {
2222
if (entry!!.isDirectory) continue
2323
val frozenBytes = zipStream.readAllBytes()
24-
val generatedBytes = kspOutput.resolve("${entry.name}").readBytes()
24+
val generatedBytes = kspOutput.resolve(entry.name).readBytes()
2525

2626
if (!(frozenBytes contentEquals generatedBytes)) {
2727
diffFiles += entry.name
165 Bytes
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dev.freya02.botcommands.jda.ktx.deprecation
2+
3+
import com.google.devtools.ksp.containingFile
4+
import com.google.devtools.ksp.symbol.KSAnnotated
5+
import com.google.devtools.ksp.symbol.KSFile
6+
7+
class FindReplacePairs {
8+
9+
private val _processedFiles = mutableListOf<KSFile>()
10+
val processedFiles: List<KSFile> get() = _processedFiles
11+
12+
private val _pairs = sortedMapOf<String, MutableSet<String>>()
13+
val pairs: Map<String, Set<String>> get() = _pairs
14+
15+
fun add(symbol: KSAnnotated, old: String, new: String) {
16+
_processedFiles.add(symbol.containingFile!!)
17+
_pairs.computeIfAbsent(old) { sortedSetOf() }.add(new)
18+
}
19+
20+
fun add(pair: RewriteFindReplace) {
21+
_processedFiles.add(pair.processedFile)
22+
_pairs.computeIfAbsent(pair.old) { sortedSetOf() }.add(pair.new)
23+
}
24+
}

jda-ktx-deprecation-processor/src/main/kotlin/dev/freya02/botcommands/jda/ktx/deprecation/processor/bc/DeprecationProcessor.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.google.devtools.ksp.symbol.KSClassDeclaration
1010
import com.google.devtools.ksp.symbol.KSFile
1111
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
1212
import dev.freya02.botcommands.jda.ktx.deprecation.CompatSourceFile
13-
import dev.freya02.botcommands.jda.ktx.deprecation.RewriteFindReplace
13+
import dev.freya02.botcommands.jda.ktx.deprecation.FindReplacePairs
1414
import dev.freya02.botcommands.jda.ktx.deprecation.render.*
1515
import dev.freya02.botcommands.jda.ktx.deprecation.utils.KaAccessor
1616
import dev.freya02.botcommands.jda.ktx.deprecation.utils.createFindAndReplaceRecipe
@@ -28,7 +28,7 @@ class DeprecationProcessor(
2828
) : SymbolProcessor {
2929

3030
private val sourceFiles = arrayListOf<CompatSourceFile>()
31-
private val findReplacePairs = linkedSetOf<RewriteFindReplace>()
31+
private val findReplacePairs = FindReplacePairs()
3232

3333
override fun process(resolver: Resolver): List<KSAnnotated> {
3434
// @DeprecatedInBcCore -> Create accessors and find&replace
@@ -85,11 +85,11 @@ class DeprecationProcessor(
8585
}
8686
}
8787

88-
require(findReplacePairs.isNotEmpty())
88+
require(findReplacePairs.pairs.isNotEmpty())
8989
codeGenerator.createNewFile(
9090
Dependencies(
9191
aggregating = true,
92-
sources = findReplacePairs.map { it.processedFile }.toTypedArray()
92+
sources = findReplacePairs.processedFiles.toTypedArray()
9393
),
9494
"META-INF/rewrite",
9595
"bc-core-to-bc-jdk-ktx",
@@ -142,11 +142,11 @@ class DeprecationProcessor(
142142
}.trimEnd('\n') + '\n'
143143

144144
@OptIn(KaExperimentalApi::class)
145-
context(imports: MutableSet<String>, findReplacePairs: MutableCollection<RewriteFindReplace>)
145+
context(imports: MutableSet<String>, findReplacePairs: FindReplacePairs)
146146
private fun createFunctionAccessor(function: KSFunctionDeclaration): String {
147147
val fullOldFunction = "$UTILS_PACKAGE.${function.simpleName.asString()}"
148148
val fullNewFunction = function.qualifiedName!!.asString()
149-
findReplacePairs += RewriteFindReplace.Companion.from(function, fullOldFunction, fullNewFunction)
149+
findReplacePairs.add(function, fullOldFunction, fullNewFunction)
150150

151151
val functionSymbol = KaAccessor.getKaFunctionSymbol(function) as KaNamedFunctionSymbol
152152

@@ -198,13 +198,13 @@ class DeprecationProcessor(
198198
}
199199
}
200200

201-
context(findReplacePairs: MutableCollection<RewriteFindReplace>)
201+
context(findReplacePairs: FindReplacePairs)
202202
private fun createTypeAlias(clazz: KSClassDeclaration): String {
203203
val aliasName = clazz.simpleName.asString()
204204
val implFullName = clazz.qualifiedName!!.asString()
205205
val typeArguments = clazz.renderTypeParameters()
206206

207-
findReplacePairs += RewriteFindReplace.Companion.from(clazz, "$UTILS_PACKAGE.$aliasName", implFullName)
207+
findReplacePairs.add(clazz, "$UTILS_PACKAGE.$aliasName", implFullName)
208208

209209
return "typealias $aliasName$typeArguments = $implFullName$typeArguments"
210210
}

jda-ktx-deprecation-processor/src/main/kotlin/dev/freya02/botcommands/jda/ktx/deprecation/processor/ktx/JdaKtxMigrationProcessor.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.google.devtools.ksp.processing.*
44
import com.google.devtools.ksp.symbol.KSAnnotated
55
import com.google.devtools.ksp.symbol.KSClassDeclaration
66
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
7-
import dev.freya02.botcommands.jda.ktx.deprecation.RewriteFindReplace
7+
import dev.freya02.botcommands.jda.ktx.deprecation.FindReplacePairs
88
import dev.freya02.botcommands.jda.ktx.deprecation.utils.RichKotlinClassMetadata
99
import dev.freya02.botcommands.jda.ktx.deprecation.utils.createFindAndReplaceRecipe
1010
import io.github.classgraph.ClassGraph
@@ -18,7 +18,7 @@ class JdaKtxMigrationProcessor(
1818
private val codeGenerator: CodeGenerator,
1919
) : SymbolProcessor {
2020

21-
private val findReplacePairs = linkedSetOf<RewriteFindReplace>()
21+
private val findReplacePairs = FindReplacePairs()
2222

2323
@Suppress("UNCHECKED_CAST")
2424
override fun process(resolver: Resolver): List<KSAnnotated> {
@@ -34,18 +34,18 @@ class JdaKtxMigrationProcessor(
3434

3535
when (symbol) {
3636
is KSClassDeclaration -> {
37-
findReplacePairs += if (forcedPkg.isNotBlank()) {
38-
RewriteFindReplace.Companion.from(symbol, forcedPkg + "." + symbol.simpleName.asString(), symbol.qualifiedName!!.asString())
37+
if (forcedPkg.isNotBlank()) {
38+
findReplacePairs.add(symbol, forcedPkg + "." + symbol.simpleName.asString(), symbol.qualifiedName!!.asString())
3939
} else {
40-
JdaKtxClassProcessor.findReplacement(symbol, metadata)
40+
findReplacePairs.add(JdaKtxClassProcessor.findReplacement(symbol, metadata))
4141
}
4242
}
4343
is KSFunctionDeclaration -> {
4444
require(symbol.parentDeclaration == null) { "Only top-level functions are supported" }
45-
findReplacePairs += if (forcedPkg.isNotBlank()) {
46-
RewriteFindReplace.Companion.from(symbol, forcedPkg + "." + symbol.simpleName.asString(), symbol.qualifiedName!!.asString())
45+
if (forcedPkg.isNotBlank()) {
46+
findReplacePairs.add(symbol, forcedPkg + "." + symbol.simpleName.asString(), symbol.qualifiedName!!.asString())
4747
} else {
48-
JdaKtxFunctionProcessor.findReplacement(symbol, metadata)
48+
findReplacePairs.add(JdaKtxFunctionProcessor.findReplacement(symbol, metadata))
4949
}
5050
}
5151
else -> {
@@ -89,11 +89,11 @@ class JdaKtxMigrationProcessor(
8989
}
9090

9191
override fun finish() {
92-
require(findReplacePairs.isNotEmpty())
92+
require(findReplacePairs.pairs.isNotEmpty())
9393
codeGenerator.createNewFile(
9494
Dependencies(
9595
aggregating = true,
96-
sources = findReplacePairs.map { it.processedFile }.toTypedArray()
96+
sources = findReplacePairs.processedFiles.toTypedArray()
9797
),
9898
"META-INF/rewrite",
9999
"jda-ktx-to-bc-jdk-ktx",
Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package dev.freya02.botcommands.jda.ktx.deprecation.utils
22

3-
import dev.freya02.botcommands.jda.ktx.deprecation.RewriteFindReplace
3+
import dev.freya02.botcommands.jda.ktx.deprecation.FindReplacePairs
44

5-
fun createFindAndReplaceRecipe(name: String, description: String, pairs: Collection<RewriteFindReplace>): String {
5+
fun createFindAndReplaceRecipe(name: String, description: String, pairs: FindReplacePairs): String {
66
val header = """
77
---
88
type: specs.openrewrite.org/v1beta/recipe
@@ -11,32 +11,21 @@ fun createFindAndReplaceRecipe(name: String, description: String, pairs: Collect
1111
recipeList:
1212
""".trimIndent()
1313

14-
val recipes = pairs.withStarImports().joinToString("\n") { (_, old, new) ->
14+
val recipes = pairs.pairs.entries.joinToString("\n") { (old, replacements) ->
1515
"""
1616
- org.openrewrite.text.FindAndReplace:
17-
find: "$old"
18-
replace: "$new"
17+
find: "import $old"
18+
replace: "${replacements.joinToString("\\n") { "import $it" }}"
19+
- org.openrewrite.text.FindAndReplace:
20+
find: "import ${old.getPackage()}.*"
21+
replace: "${replacements.joinToString("\\n") { "import ${it.getPackage()}.*" }}"
1922
""".trimIndent().prependIndent(" ")
2023
}
2124

2225
return header + "\n" + recipes
2326
}
2427

25-
private fun Collection<RewriteFindReplace>.withStarImports(): List<RewriteFindReplace> {
26-
val added = hashSetOf<Pair<String, String>>()
27-
28-
return flatMap { rule ->
29-
fun String.getPackage(): String {
30-
// Assume there are no rule with nested classes, so we can just drop the last import component
31-
return substringBeforeLast('.')
32-
}
33-
34-
val oldPackage = rule.old.getPackage()
35-
val newPackage = rule.new.getPackage()
36-
if (added.add(oldPackage to newPackage)) {
37-
listOf(rule, rule.copy(old = "$oldPackage.*", new = "$oldPackage.*\\nimport $newPackage.*"))
38-
} else {
39-
listOf(rule)
40-
}
41-
}
28+
private fun String.getPackage(): String {
29+
// Assume there are no rule with nested classes, so we can just drop the last import component
30+
return substringBeforeLast('.')
4231
}

0 commit comments

Comments
 (0)