[Gradle][MPP] KotlinProjectStructureMetadata: Add .sourceSetNames property

^KT-48135 WIP
This commit is contained in:
Sebastian Sellmair
2022-09-13 12:51:43 +02:00
committed by Space
parent c5d4619ebc
commit 2391720999
3 changed files with 26 additions and 9 deletions
@@ -570,6 +570,7 @@ class HierarchicalMppIT : KGPBaseTest() {
sourceSetCInteropMetadataDirectory = mapOf(), sourceSetCInteropMetadataDirectory = mapOf(),
hostSpecificSourceSets = emptySet(), hostSpecificSourceSets = emptySet(),
sourceSetBinaryLayout = sourceSetModuleDependencies.mapValues { SourceSetMetadataLayout.KLIB }, sourceSetBinaryLayout = sourceSetModuleDependencies.mapValues { SourceSetMetadataLayout.KLIB },
sourceSetNames = setOf("commonMain", "jvmAndJsMain", "linuxAndJsMain"),
isPublishedAsRoot = true isPublishedAsRoot = true
) )
} }
@@ -114,8 +114,11 @@ data class KotlinProjectStructureMetadata(
@get:Input @get:Input
val isPublishedAsRoot: Boolean, val isPublishedAsRoot: Boolean,
@get:Input
val sourceSetNames: Set<String>,
@Input @Input
val formatVersion: String = FORMAT_VERSION_0_3_2 val formatVersion: String = FORMAT_VERSION_0_3_3
) : Serializable { ) : Serializable {
@Suppress("UNUSED") // Gradle input @Suppress("UNUSED") // Gradle input
@get:Input @get:Input
@@ -136,9 +139,13 @@ data class KotlinProjectStructureMetadata(
// + 'sourceSetCInteropMetadataDirectory' map // + 'sourceSetCInteropMetadataDirectory' map
internal const val FORMAT_VERSION_0_3_2 = "0.3.2" internal const val FORMAT_VERSION_0_3_2 = "0.3.2"
// + 'sourceSetsNames'
internal const val FORMAT_VERSION_0_3_3 = "0.3.3"
} }
} }
internal val KotlinMultiplatformExtension.kotlinProjectStructureMetadata: KotlinProjectStructureMetadata internal val KotlinMultiplatformExtension.kotlinProjectStructureMetadata: KotlinProjectStructureMetadata
get() = project.extensions.extraProperties.getOrPut("org.jetbrains.kotlin.gradle.plugin.mpp.kotlinProjectStructureMetadata") { get() = project.extensions.extraProperties.getOrPut("org.jetbrains.kotlin.gradle.plugin.mpp.kotlinProjectStructureMetadata") {
buildKotlinProjectStructureMetadata(this) buildKotlinProjectStructureMetadata(this)
@@ -193,7 +200,8 @@ private fun buildKotlinProjectStructureMetadata(extension: KotlinMultiplatformEx
sourceSetBinaryLayout = sourceSetsWithMetadataCompilations.keys.associate { sourceSet -> sourceSetBinaryLayout = sourceSetsWithMetadataCompilations.keys.associate { sourceSet ->
sourceSet.name to SourceSetMetadataLayout.chooseForProducingProject() sourceSet.name to SourceSetMetadataLayout.chooseForProducingProject()
}, },
isPublishedAsRoot = true isPublishedAsRoot = true,
sourceSetNames = sourceSetsWithMetadataCompilations.keys.map { it.name }.toSet(),
) )
} }
@@ -226,7 +234,8 @@ internal fun buildProjectStructureMetadata(module: GradleKpmModule): KotlinProje
sourceSetModuleDependencies = fragmentDependencies, sourceSetModuleDependencies = fragmentDependencies,
sourceSetCInteropMetadataDirectory = emptyMap(), // Not supported yet sourceSetCInteropMetadataDirectory = emptyMap(), // Not supported yet
hostSpecificSourceSets = getHostSpecificFragments(module).mapTo(mutableSetOf()) { it.name }, hostSpecificSourceSets = getHostSpecificFragments(module).mapTo(mutableSetOf()) { it.name },
isPublishedAsRoot = true isPublishedAsRoot = true,
sourceSetNames = module.fragments.map { it.name }.toSet()
) )
} }
@@ -253,8 +262,7 @@ internal fun <Serializer> KotlinProjectStructureMetadata.serialize(
} }
multiNodes(SOURCE_SETS_NODE_NAME) { multiNodes(SOURCE_SETS_NODE_NAME) {
val keys = sourceSetsDependsOnRelation.keys + sourceSetModuleDependencies.keys for (sourceSet in sourceSetNames) {
for (sourceSet in keys) {
multiNodesItem(SOURCE_SET_NODE_NAME) { multiNodesItem(SOURCE_SET_NODE_NAME) {
value(NAME_NODE_NAME, sourceSet) value(NAME_NODE_NAME, sourceSet)
multiValue(DEPENDS_ON_NODE_NAME, sourceSetsDependsOnRelation[sourceSet].orEmpty().toList()) multiValue(DEPENDS_ON_NODE_NAME, sourceSetsDependsOnRelation[sourceSet].orEmpty().toList())
@@ -304,7 +312,7 @@ internal fun KotlinProjectStructureMetadata.toJson(): String {
private val NodeList.elements: Iterable<Element> get() = (0 until length).map { this@elements.item(it) }.filterIsInstance<Element>() private val NodeList.elements: Iterable<Element> get() = (0 until length).map { this@elements.item(it) }.filterIsInstance<Element>()
internal fun parseKotlinSourceSetMetadataFromJson(string: String): KotlinProjectStructureMetadata? { internal fun parseKotlinSourceSetMetadataFromJson(string: String): KotlinProjectStructureMetadata {
@Suppress("DEPRECATION") // The replacement doesn't compile against old dependencies such as AS 4.0 @Suppress("DEPRECATION") // The replacement doesn't compile against old dependencies such as AS 4.0
val json = JsonParser().parse(string).asJsonObject val json = JsonParser().parse(string).asJsonObject
val valueNamed: JsonObject.(String) -> String? = { name -> get(name)?.asString } val valueNamed: JsonObject.(String) -> String? = { name -> get(name)?.asString }
@@ -314,7 +322,7 @@ internal fun parseKotlinSourceSetMetadataFromJson(string: String): KotlinProject
return parseKotlinSourceSetMetadata({ json.get(ROOT_NODE_NAME).asJsonObject }, valueNamed, multiObjects, multiValues) return parseKotlinSourceSetMetadata({ json.get(ROOT_NODE_NAME).asJsonObject }, valueNamed, multiObjects, multiValues)
} }
internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProjectStructureMetadata? { internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProjectStructureMetadata {
val nodeNamed: Element.(String) -> Element? = { name -> getElementsByTagName(name).elements.singleOrNull() } val nodeNamed: Element.(String) -> Element? = { name -> getElementsByTagName(name).elements.singleOrNull() }
val valueNamed: Element.(String) -> String? = val valueNamed: Element.(String) -> String? =
{ name -> getElementsByTagName(name).run { if (length > 0) item(0).textContent else null } } { name -> getElementsByTagName(name).run { if (length > 0) item(0).textContent else null } }
@@ -334,7 +342,7 @@ internal fun <ParsingContext> parseKotlinSourceSetMetadata(
valueNamed: ParsingContext.(key: String) -> String?, valueNamed: ParsingContext.(key: String) -> String?,
multiObjects: ParsingContext.(named: String) -> Iterable<ParsingContext>, multiObjects: ParsingContext.(named: String) -> Iterable<ParsingContext>,
multiValues: ParsingContext.(named: String) -> Iterable<String> multiValues: ParsingContext.(named: String) -> Iterable<String>
): KotlinProjectStructureMetadata? { ): KotlinProjectStructureMetadata {
val projectStructureNode = getRoot() val projectStructureNode = getRoot()
val formatVersion = checkNotNull(projectStructureNode.valueNamed(FORMAT_VERSION_NODE_NAME)) val formatVersion = checkNotNull(projectStructureNode.valueNamed(FORMAT_VERSION_NODE_NAME))
@@ -355,11 +363,13 @@ internal fun <ParsingContext> parseKotlinSourceSetMetadata(
val sourceSetBinaryLayout = mutableMapOf<String, SourceSetMetadataLayout>() val sourceSetBinaryLayout = mutableMapOf<String, SourceSetMetadataLayout>()
val sourceSetCInteropMetadataDirectory = mutableMapOf<String, String>() val sourceSetCInteropMetadataDirectory = mutableMapOf<String, String>()
val hostSpecificSourceSets = mutableSetOf<String>() val hostSpecificSourceSets = mutableSetOf<String>()
val sourceSetNames = mutableSetOf<String>()
val sourceSetsNode = projectStructureNode.multiObjects(SOURCE_SETS_NODE_NAME) val sourceSetsNode = projectStructureNode.multiObjects(SOURCE_SETS_NODE_NAME)
sourceSetsNode.forEach { sourceSetNode -> sourceSetsNode.forEach { sourceSetNode ->
val sourceSetName = checkNotNull(sourceSetNode.valueNamed(NAME_NODE_NAME)) val sourceSetName = checkNotNull(sourceSetNode.valueNamed(NAME_NODE_NAME))
sourceSetNames.add(sourceSetName)
val dependsOn = sourceSetNode.multiValues(DEPENDS_ON_NODE_NAME).toSet() val dependsOn = sourceSetNode.multiValues(DEPENDS_ON_NODE_NAME).toSet()
val moduleDependencies = sourceSetNode.multiValues(MODULE_DEPENDENCY_NODE_NAME).mapTo(mutableSetOf()) { val moduleDependencies = sourceSetNode.multiValues(MODULE_DEPENDENCY_NODE_NAME).mapTo(mutableSetOf()) {
@@ -390,6 +400,7 @@ internal fun <ParsingContext> parseKotlinSourceSetMetadata(
sourceSetCInteropMetadataDirectory = sourceSetCInteropMetadataDirectory, sourceSetCInteropMetadataDirectory = sourceSetCInteropMetadataDirectory,
hostSpecificSourceSets = hostSpecificSourceSets, hostSpecificSourceSets = hostSpecificSourceSets,
isPublishedAsRoot = isPublishedAsRoot, isPublishedAsRoot = isPublishedAsRoot,
sourceSetNames = sourceSetNames,
formatVersion = formatVersion formatVersion = formatVersion
) )
} }
@@ -35,7 +35,8 @@ class KotlinProjectStructureMetadataSerializationTest {
), ),
sourceSetCInteropMetadataDirectory = mapOf("sourceSetB" to "xx/cinterop/", "sourceSetC" to "cinterops/C"), sourceSetCInteropMetadataDirectory = mapOf("sourceSetB" to "xx/cinterop/", "sourceSetC" to "cinterops/C"),
hostSpecificSourceSets = setOf("sourceSetC"), hostSpecificSourceSets = setOf("sourceSetC"),
isPublishedAsRoot = true isPublishedAsRoot = true,
sourceSetNames = setOf("sourceSetA", "sourceSetB", "sourceSetC"),
) )
@Test @Test
@@ -64,6 +65,10 @@ class KotlinProjectStructureMetadataSerializationTest {
We expect no 'cinterop metadata' in artifacts with older format versions We expect no 'cinterop metadata' in artifacts with older format versions
*/ */
assertEquals(emptyMap(), deserialized.sourceSetCInteropMetadataDirectory) assertEquals(emptyMap(), deserialized.sourceSetCInteropMetadataDirectory)
assertEquals(
setOf("commonMain", "concurrentMain", "nativeDarwinMain", "nativeMain", "nativeOtherMain"),
deserialized.sourceSetNames
)
} }
} }