aboutsummaryrefslogtreecommitdiff
path: root/src/test/kotlin/com/replaymod/gradle/remap/util/TestData.kt
blob: 0f83373cbf19d0b4d9ca2486f4505a0dc4629bba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.replaymod.gradle.remap.util

import com.replaymod.gradle.remap.Transformer
import com.replaymod.gradle.remap.legacy.LegacyMappingSetModelFactory
import org.cadixdev.lorenz.MappingSet
import java.net.URL
import java.nio.file.Paths
import kotlin.io.path.absolutePathString

object TestData {
    private val mappingsPath = Paths.get(javaClass.getResource("/mappings.srg")!!.toURI())
    private val mappings = mappingsPath.readMappings().let { mappings ->
        val legacyCopy = MappingSet.create(LegacyMappingSetModelFactory())
        mappings.topLevelClassMappings.forEach { it.copy(legacyCopy) }
        legacyCopy
    }

    val transformer = Transformer(mappings).apply {
        fun findClasspathEntry(cls: String): String {
            val classFilePath = "/${cls.replace('.', '/')}.class"
            val url = javaClass.getResource(classFilePath)
                ?: throw RuntimeException("Failed to find $cls on classpath.")

            return when {
                url.protocol == "jar" && url.file.endsWith("!$classFilePath") -> {
                    Paths.get(URL(url.file.removeSuffix("!$classFilePath")).toURI()).absolutePathString()
                }
                url.protocol == "file" && url.file.endsWith(classFilePath) -> {
                    var path = Paths.get(url.toURI())
                    repeat(cls.count { it == '.' } + 1) {
                        path = path.parent
                    }
                    path.absolutePathString()
                }
                else -> {
                    throw RuntimeException("Do not know how to turn $url into classpath entry.")
                }
            }
        }
        classpath = arrayOf(
            findClasspathEntry("org.spongepowered.asm.mixin.Mixin"),
            findClasspathEntry("a.pkg.A"),
        )
    }

    fun remap(content: String): String = transformer.remap(mapOf("test.java" to content))["test.java"]!!.first
    fun remapWithErrors(content: String) = transformer.remap(mapOf("test.java" to content))["test.java"]!!

    fun remapKt(content: String): String = transformer.remap(mapOf("test.kt" to content))["test.kt"]!!.first
    fun remapKtWithErrors(content: String) = transformer.remap(mapOf("test.kt" to content))["test.kt"]!!
}