diff options
Diffstat (limited to 'src/test/kotlin/com/replaymod/gradle/remap/util')
-rw-r--r-- | src/test/kotlin/com/replaymod/gradle/remap/util/TestData.kt | 48 | ||||
-rw-r--r-- | src/test/kotlin/com/replaymod/gradle/remap/util/mappingExtensions.kt | 13 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/test/kotlin/com/replaymod/gradle/remap/util/TestData.kt b/src/test/kotlin/com/replaymod/gradle/remap/util/TestData.kt new file mode 100644 index 0000000..cb0ad44 --- /dev/null +++ b/src/test/kotlin/com/replaymod/gradle/remap/util/TestData.kt @@ -0,0 +1,48 @@ +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"]!! +}
\ No newline at end of file diff --git a/src/test/kotlin/com/replaymod/gradle/remap/util/mappingExtensions.kt b/src/test/kotlin/com/replaymod/gradle/remap/util/mappingExtensions.kt new file mode 100644 index 0000000..9a869d4 --- /dev/null +++ b/src/test/kotlin/com/replaymod/gradle/remap/util/mappingExtensions.kt @@ -0,0 +1,13 @@ +package com.replaymod.gradle.remap.util + +import org.cadixdev.lorenz.MappingSet +import org.cadixdev.lorenz.io.MappingFormats +import java.nio.file.Path + +fun Path.readMappings(): MappingSet { + val name = fileName.toString() + val ext = name.substring(name.lastIndexOf(".") + 1) + val format = MappingFormats.REGISTRY.values().find { it.standardFileExtension.orElse(null) == ext } + ?: throw UnsupportedOperationException("Cannot find mapping format for $this") + return format.read(this) +} |