From 94e3fa213228f55f53071af3796ed6e52523c7cb Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Mon, 2 Mar 2020 18:38:08 +0300 Subject: [PATCH] (minor) Extract constants for XML nodes in project structure metadata --- .../mpp/KotlinProjectStructureMetadata.kt | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinProjectStructureMetadata.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinProjectStructureMetadata.kt index 11802444d74..0b3459f6c9d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinProjectStructureMetadata.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/KotlinProjectStructureMetadata.kt @@ -128,34 +128,34 @@ internal fun KotlinProjectStructureMetadata.toXmlDocument(): Document { fun Node.textNode(name: String, value: String) = appendChild(createElement(name).apply { appendChild(createTextNode(value)) }) - node("projectStructure") { - textNode("formatVersion", formatVersion) + node(ROOT_NODE_NAME) { + textNode(FORMAT_VERSION_NODE_NAME, formatVersion) - node("variants") { + node(VARIANTS_NODE_NAME) { sourceSetNamesByVariantName.forEach { (variantName, sourceSets) -> - node("variant") { - textNode("name", variantName) - sourceSets.forEach { sourceSetName -> textNode("sourceSet", sourceSetName) } + node(VARIANT_NODE_NAME) { + textNode(NAME_NODE_NAME, variantName) + sourceSets.forEach { sourceSetName -> textNode(SOURCE_SET_NODE_NAME, sourceSetName) } } } } - node("sourceSets") { + node(SOURCE_SETS_NODE_NAME) { val keys = sourceSetsDependsOnRelation.keys + sourceSetModuleDependencies.keys for (sourceSet in keys) { - node("sourceSet") { - textNode("name", sourceSet) + node(SOURCE_SET_NODE_NAME) { + textNode(NAME_NODE_NAME, sourceSet) sourceSetsDependsOnRelation[sourceSet].orEmpty().forEach { dependsOn -> - textNode("dependsOn", dependsOn) + textNode(DEPENDS_ON_NODE_NAME, dependsOn) } sourceSetModuleDependencies[sourceSet].orEmpty().forEach { moduleDependency -> - textNode("moduleDependency", moduleDependency.groupId + ":" + moduleDependency.moduleId) + textNode(MODULE_DEPENDENCY_NODE_NAME, moduleDependency.groupId + ":" + moduleDependency.moduleId) } sourceSetBinaryLayout[sourceSet]?.let { binaryLayout -> - textNode("binaryLayout", binaryLayout.name) + textNode(BINARY_LAYOUT_NODE_NAME, binaryLayout.name) } if (sourceSet in hostSpecificSourceSets) { - textNode("hostSpecific", "true") + textNode(HOST_SPECIFIC_NODE_NAME, "true") } } } @@ -167,17 +167,18 @@ internal fun KotlinProjectStructureMetadata.toXmlDocument(): Document { private val NodeList.elements: Iterable get() = (0 until length).map { this@elements.item(it) }.filterIsInstance() internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProjectStructureMetadata? { - val projectStructureNode = document.getElementsByTagName("projectStructure").elements.single() + val projectStructureNode = document.getElementsByTagName(ROOT_NODE_NAME).elements.single() - val formatVersion = projectStructureNode.getElementsByTagName("formatVersion").item(0).textContent + val formatVersion = projectStructureNode.getElementsByTagName(FORMAT_VERSION_NODE_NAME).item(0).textContent - val variantsNode = projectStructureNode.getElementsByTagName("variants").item(0) ?: return null + val variantsNode = projectStructureNode.getElementsByTagName(VARIANTS_NODE_NAME).item(0) ?: return null val sourceSetsByVariant = mutableMapOf>() - variantsNode.childNodes.elements.filter { it.tagName == "variant" }.forEach { variantNode -> - val variantName = variantNode.getElementsByTagName("name").elements.single().textContent - val sourceSets = variantNode.childNodes.elements.filter { it.tagName == "sourceSet" }.mapTo(mutableSetOf()) { it.textContent } + variantsNode.childNodes.elements.filter { it.tagName == VARIANT_NODE_NAME }.forEach { variantNode -> + val variantName = variantNode.getElementsByTagName(NAME_NODE_NAME).elements.single().textContent + val sourceSets = + variantNode.childNodes.elements.filter { it.tagName == SOURCE_SET_NODE_NAME }.mapTo(mutableSetOf()) { it.textContent } sourceSetsByVariant[variantName] = sourceSets } @@ -187,27 +188,27 @@ internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProj val sourceSetBinaryLayout = mutableMapOf() val hostSpecificSourceSets = mutableSetOf() - val sourceSetsNode = projectStructureNode.getElementsByTagName("sourceSets").item(0) ?: return null + val sourceSetsNode = projectStructureNode.getElementsByTagName(SOURCE_SETS_NODE_NAME).item(0) ?: return null - sourceSetsNode.childNodes.elements.filter { it.tagName == "sourceSet" }.forEach { sourceSetNode -> - val sourceSetName = sourceSetNode.getElementsByTagName("name").elements.single().textContent + sourceSetsNode.childNodes.elements.filter { it.tagName == SOURCE_SET_NODE_NAME }.forEach { sourceSetNode -> + val sourceSetName = sourceSetNode.getElementsByTagName(NAME_NODE_NAME).elements.single().textContent val dependsOn = mutableSetOf() val moduleDependencies = mutableSetOf() sourceSetNode.childNodes.elements.forEach { node -> when (node.tagName) { - "dependsOn" -> dependsOn.add(node.textContent) - "moduleDependency" -> { + DEPENDS_ON_NODE_NAME -> dependsOn.add(node.textContent) + MODULE_DEPENDENCY_NODE_NAME -> { val (groupId, moduleId) = node.textContent.split(":") moduleDependencies.add(ModuleDependencyIdentifier(groupId, moduleId)) } - "binaryLayout" -> { + BINARY_LAYOUT_NODE_NAME -> { SourceSetMetadataLayout.byName(node.textContent)?.let { binaryLayout -> sourceSetBinaryLayout[sourceSetName] = binaryLayout } } - "hostSpecific" -> { + HOST_SPECIFIC_NODE_NAME -> { if (node.textContent == "true") { hostSpecificSourceSets.add(sourceSetName) } @@ -227,4 +228,16 @@ internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProj hostSpecificSourceSets, formatVersion ) -} \ No newline at end of file +} + +private const val ROOT_NODE_NAME = "projectStructure" +private const val FORMAT_VERSION_NODE_NAME = "formatVersion" +private const val VARIANTS_NODE_NAME = "variants" +private const val VARIANT_NODE_NAME = "variant" +private const val NAME_NODE_NAME = "name" +private const val SOURCE_SETS_NODE_NAME = "sourceSets" +private const val SOURCE_SET_NODE_NAME = "sourceSet" +private const val DEPENDS_ON_NODE_NAME = "dependsOn" +private const val MODULE_DEPENDENCY_NODE_NAME = "moduleDependency" +private const val BINARY_LAYOUT_NODE_NAME = "binaryLayout" +private const val HOST_SPECIFIC_NODE_NAME = "hostSpecific" \ No newline at end of file