From 33065b99313c4283fb64c91c04017b33b5673852 Mon Sep 17 00:00:00 2001 From: Deftu Date: Fri, 7 Jun 2024 12:42:07 +0200 Subject: Clean up Kotlin-specific files & support K2 --- .../com/replaymod/gradle/remap/kotlin1521.kt | 2 +- .../kotlin/com/replaymod/gradle/remap/kotlin190.kt | 2 +- .../kotlin/com/replaymod/gradle/remap/kotlin200.kt | 18 ++++++++++++ .../com/replaymod/gradle/remap/Transformer.kt | 12 ++------ .../kotlin/com/replaymod/gradle/remap/version.kt | 33 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 src/kotlin200/kotlin/com/replaymod/gradle/remap/kotlin200.kt create mode 100644 src/main/kotlin/com/replaymod/gradle/remap/version.kt (limited to 'src') diff --git a/src/kotlin1521/kotlin/com/replaymod/gradle/remap/kotlin1521.kt b/src/kotlin1521/kotlin/com/replaymod/gradle/remap/kotlin1521.kt index 3031a9a..334d9b5 100644 --- a/src/kotlin1521/kotlin/com/replaymod/gradle/remap/kotlin1521.kt +++ b/src/kotlin1521/kotlin/com/replaymod/gradle/remap/kotlin1521.kt @@ -18,4 +18,4 @@ fun analyze1521(environment: KotlinCoreEnvironment, ktFiles: List): Anal ) } -fun kotlinSourceRoot1521(path: String, isCommon: Boolean) = KotlinSourceRoot(path, isCommon) +fun createSourceRoot1521(path: String, isCommon: Boolean) = KotlinSourceRoot(path, isCommon) diff --git a/src/kotlin190/kotlin/com/replaymod/gradle/remap/kotlin190.kt b/src/kotlin190/kotlin/com/replaymod/gradle/remap/kotlin190.kt index fecaa66..599170b 100644 --- a/src/kotlin190/kotlin/com/replaymod/gradle/remap/kotlin190.kt +++ b/src/kotlin190/kotlin/com/replaymod/gradle/remap/kotlin190.kt @@ -2,4 +2,4 @@ package com.replaymod.gradle.remap import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot -fun kotlinSourceRoot190(path: String, isCommon: Boolean) = KotlinSourceRoot(path, isCommon, null) +fun createSourceRoot190(path: String, isCommon: Boolean) = KotlinSourceRoot(path, isCommon, null) diff --git a/src/kotlin200/kotlin/com/replaymod/gradle/remap/kotlin200.kt b/src/kotlin200/kotlin/com/replaymod/gradle/remap/kotlin200.kt new file mode 100644 index 0000000..c9befe3 --- /dev/null +++ b/src/kotlin200/kotlin/com/replaymod/gradle/remap/kotlin200.kt @@ -0,0 +1,18 @@ +package com.replaymod.gradle.remap + +import org.jetbrains.kotlin.analyzer.AnalysisResult +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.compiler.NoScopeRecordCliBindingTrace +import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM +import org.jetbrains.kotlin.com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.psi.KtFile + +fun analyze200(environment: KotlinCoreEnvironment, ktFiles: List): AnalysisResult { + return TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( + environment.project, + ktFiles, + NoScopeRecordCliBindingTrace(environment.project), + environment.configuration, + { scope: GlobalSearchScope -> environment.createPackagePartProvider(scope) } + ) +} diff --git a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt index 0845e0d..9efd0e4 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt @@ -72,11 +72,7 @@ class Transformer(private val map: MappingSet) { config.put(CommonConfigurationKeys.MODULE_NAME, "main") jdkHome?.let {config.setupJdk(it) } config.add(CLIConfigurationKeys.CONTENT_ROOTS, JavaSourceRoot(tmpDir.toFile(), "")) - val kotlinSourceRoot = try { - kotlinSourceRoot1521(tmpDir.toAbsolutePath().toString(), false) - } catch (e: NoSuchMethodError) { - kotlinSourceRoot190(tmpDir.toAbsolutePath().toString(), false) - } + val kotlinSourceRoot = createSourceRoot(tmpDir, false) config.add(CLIConfigurationKeys.CONTENT_ROOTS, kotlinSourceRoot) config.addAll(CLIConfigurationKeys.CONTENT_ROOTS, classpath!!.map { JvmClasspathRoot(File(it)) }) config.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, PrintingMessageCollector(System.err, MessageRenderer.GRADLE_STYLE, true)) @@ -103,11 +99,7 @@ class Transformer(private val map: MappingSet) { val psiFiles = virtualFiles.mapValues { psiManager.findFile(it.value)!! } val ktFiles = psiFiles.values.filterIsInstance() - val analysis = try { - analyze1521(environment, ktFiles) - } catch (e: NoSuchMethodError) { - analyze1620(environment, ktFiles) - } + val analysis = analyze(environment, ktFiles) val remappedEnv = remappedClasspath?.let { setupRemappedProject(disposable, it, processedTmpDir) diff --git a/src/main/kotlin/com/replaymod/gradle/remap/version.kt b/src/main/kotlin/com/replaymod/gradle/remap/version.kt new file mode 100644 index 0000000..d85264e --- /dev/null +++ b/src/main/kotlin/com/replaymod/gradle/remap/version.kt @@ -0,0 +1,33 @@ +package com.replaymod.gradle.remap + +import org.jetbrains.kotlin.analyzer.AnalysisResult +import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.psi.KtFile +import java.nio.file.Path + +fun analyze( + environment: KotlinCoreEnvironment, + ktFiles: List +): AnalysisResult { + return try { + analyze1521(environment, ktFiles) + } catch (e: Throwable) { + try { + analyze1620(environment, ktFiles) + } catch (e: Throwable) { + analyze200(environment, ktFiles) + } + } +} + +fun createSourceRoot( + tempDir: Path, + isCommon: Boolean +): KotlinSourceRoot { + return try { + createSourceRoot1521(tempDir.toAbsolutePath().toString(), isCommon) + } catch (e: NoSuchMethodError) { + createSourceRoot190(tempDir.toAbsolutePath().toString(), isCommon) + } +} -- cgit