[KPM] Implement IdeaKotlinVariant

KT-51262
KT-51220
This commit is contained in:
sebastian.sellmair
2022-02-16 08:41:46 +01:00
committed by Space
parent 851303e62a
commit 11d408f71e
6 changed files with 103 additions and 18 deletions
@@ -50,6 +50,11 @@ public abstract class org/jetbrains/kotlin/gradle/kpm/KotlinMutableExternalModel
public abstract fun set (Lorg/jetbrains/kotlin/gradle/kpm/KotlinExternalModelKey;Ljava/lang/Object;)V
}
public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinCompilationOutput : java/io/Serializable {
public abstract fun getClassesDirs ()Ljava/util/Set;
public abstract fun getResourcesDir ()Ljava/io/File;
}
public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinFragment : java/io/Serializable {
public abstract fun getDependencies ()Ljava/util/Collection;
public abstract fun getDirectRefinesDependencies ()Ljava/util/Collection;
@@ -85,7 +90,7 @@ public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinM
}
public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinModule : java/io/Serializable {
public abstract fun getFragments ()Ljava/util/Collection;
public abstract fun getFragments ()Ljava/util/List;
public abstract fun getModuleIdentifier ()Lorg/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinModuleIdentifier;
}
@@ -109,6 +114,11 @@ public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinS
public abstract fun getFile ()Ljava/io/File;
}
public abstract interface class org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinVariant : java/io/Serializable, org/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinFragment {
public abstract fun getCompilationOutputs ()Lorg/jetbrains/kotlin/gradle/kpm/idea/IdeaKotlinCompilationOutput;
public abstract fun getVariantAttributes ()Ljava/util/Map;
}
public abstract interface annotation class org/jetbrains/kotlin/gradle/kpm/idea/InternalKotlinGradlePluginApi : java/lang/annotation/Annotation {
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.kpm.idea
import java.io.File
import java.io.Serializable
interface IdeaKotlinCompilationOutput : Serializable {
val classesDirs: Set<File>
val resourcesDir: File?
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinCompilationOutputImpl(
override val classesDirs: Set<File>,
override val resourcesDir: File?
) : IdeaKotlinCompilationOutput
@@ -9,13 +9,13 @@ import java.io.Serializable
interface IdeaKotlinModule : Serializable {
val moduleIdentifier: IdeaKotlinModuleIdentifier
val fragments: Collection<IdeaKotlinFragment>
}
val fragments: List<IdeaKotlinFragment>
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinModuleImpl(
override val moduleIdentifier: IdeaKotlinModuleIdentifier,
override val fragments: Collection<IdeaKotlinFragment>
override val fragments: List<IdeaKotlinFragment>
) : IdeaKotlinModule {
@InternalKotlinGradlePluginApi
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.kpm.idea
import java.io.Serializable
interface IdeaKotlinVariant : IdeaKotlinFragment, Serializable {
val variantAttributes: Map<String, String>
val compilationOutputs: IdeaKotlinCompilationOutput
}
@InternalKotlinGradlePluginApi
data class IdeaKotlinVariantImpl(
internal val fragment: IdeaKotlinFragment,
override val variantAttributes: Map<String, String>,
override val compilationOutputs: IdeaKotlinCompilationOutput,
) : IdeaKotlinVariant, IdeaKotlinFragment by fragment
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.kpm.idea
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationOutput
internal fun KotlinCompilationOutput.toIdeaKotlinCompilationOutputs(): IdeaKotlinCompilationOutput {
return IdeaKotlinCompilationOutputImpl(
classesDirs = this.classesDirs.toSet(),
resourcesDir = this.resourcesDir
)
}
@@ -8,23 +8,43 @@ package org.jetbrains.kotlin.gradle.kpm.idea
import org.jetbrains.kotlin.gradle.kpm.KotlinExternalModelContainer
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleFragment
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleFragmentInternal
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinGradleVariant
private typealias FragmentInterner = MutableMap<KotlinGradleFragment, IdeaKotlinFragment>
internal fun KotlinGradleFragment.toIdeaKotlinFragment(
cache: MutableMap<KotlinGradleFragment, IdeaKotlinFragment> = mutableMapOf()
cache: FragmentInterner = mutableMapOf()
): IdeaKotlinFragment {
return cache.getOrPut(this) {
IdeaKotlinFragmentImpl(
name = name,
languageSettings = languageSettings.toIdeaKotlinLanguageSettings(),
dependencies = emptyList(),
directRefinesDependencies = directRefinesDependencies.map { refinesFragment ->
refinesFragment.toIdeaKotlinFragment(cache)
},
sourceDirectories = kotlinSourceRoots.sourceDirectories.files.toList().map { file ->
IdeaKotlinSourceDirectoryImpl(file)
},
resourceDirectories = emptyList(),
external = (this as? KotlinGradleFragmentInternal)?.external ?: KotlinExternalModelContainer.Empty
)
if (this is KotlinGradleVariant) buildIdeaKotlinVariant(cache)
else buildIdeaKotlinFragment(cache)
}
}
internal fun KotlinGradleVariant.toIdeaKotlinVariant(
cache: FragmentInterner = mutableMapOf()
): IdeaKotlinVariant = toIdeaKotlinVariant(cache) as IdeaKotlinVariant
private fun KotlinGradleFragment.buildIdeaKotlinFragment(cache: FragmentInterner): IdeaKotlinFragment {
return IdeaKotlinFragmentImpl(
name = name,
languageSettings = languageSettings.toIdeaKotlinLanguageSettings(),
dependencies = emptyList(),
directRefinesDependencies = directRefinesDependencies.map { refinesFragment ->
refinesFragment.toIdeaKotlinFragment(cache)
},
sourceDirectories = kotlinSourceRoots.sourceDirectories.files.toList().map { file ->
IdeaKotlinSourceDirectoryImpl(file)
},
resourceDirectories = emptyList(),
external = (this as? KotlinGradleFragmentInternal)?.external ?: KotlinExternalModelContainer.Empty
)
}
private fun KotlinGradleVariant.buildIdeaKotlinVariant(cache: FragmentInterner): IdeaKotlinVariant {
return IdeaKotlinVariantImpl(
fragment = buildIdeaKotlinFragment(cache),
variantAttributes = variantAttributes.mapKeys { (key, _) -> key.uniqueName },
compilationOutputs = compilationOutputs.toIdeaKotlinCompilationOutputs()
)
}