(minor) Extract constants for XML nodes in project structure metadata

This commit is contained in:
Sergey Igushkin
2020-03-02 18:38:08 +03:00
parent f30aada84a
commit 94e3fa2132
@@ -128,34 +128,34 @@ internal fun KotlinProjectStructureMetadata.toXmlDocument(): Document {
fun Node.textNode(name: String, value: String) = fun Node.textNode(name: String, value: String) =
appendChild(createElement(name).apply { appendChild(createTextNode(value)) }) appendChild(createElement(name).apply { appendChild(createTextNode(value)) })
node("projectStructure") { node(ROOT_NODE_NAME) {
textNode("formatVersion", formatVersion) textNode(FORMAT_VERSION_NODE_NAME, formatVersion)
node("variants") { node(VARIANTS_NODE_NAME) {
sourceSetNamesByVariantName.forEach { (variantName, sourceSets) -> sourceSetNamesByVariantName.forEach { (variantName, sourceSets) ->
node("variant") { node(VARIANT_NODE_NAME) {
textNode("name", variantName) textNode(NAME_NODE_NAME, variantName)
sourceSets.forEach { sourceSetName -> textNode("sourceSet", sourceSetName) } sourceSets.forEach { sourceSetName -> textNode(SOURCE_SET_NODE_NAME, sourceSetName) }
} }
} }
} }
node("sourceSets") { node(SOURCE_SETS_NODE_NAME) {
val keys = sourceSetsDependsOnRelation.keys + sourceSetModuleDependencies.keys val keys = sourceSetsDependsOnRelation.keys + sourceSetModuleDependencies.keys
for (sourceSet in keys) { for (sourceSet in keys) {
node("sourceSet") { node(SOURCE_SET_NODE_NAME) {
textNode("name", sourceSet) textNode(NAME_NODE_NAME, sourceSet)
sourceSetsDependsOnRelation[sourceSet].orEmpty().forEach { dependsOn -> sourceSetsDependsOnRelation[sourceSet].orEmpty().forEach { dependsOn ->
textNode("dependsOn", dependsOn) textNode(DEPENDS_ON_NODE_NAME, dependsOn)
} }
sourceSetModuleDependencies[sourceSet].orEmpty().forEach { moduleDependency -> sourceSetModuleDependencies[sourceSet].orEmpty().forEach { moduleDependency ->
textNode("moduleDependency", moduleDependency.groupId + ":" + moduleDependency.moduleId) textNode(MODULE_DEPENDENCY_NODE_NAME, moduleDependency.groupId + ":" + moduleDependency.moduleId)
} }
sourceSetBinaryLayout[sourceSet]?.let { binaryLayout -> sourceSetBinaryLayout[sourceSet]?.let { binaryLayout ->
textNode("binaryLayout", binaryLayout.name) textNode(BINARY_LAYOUT_NODE_NAME, binaryLayout.name)
} }
if (sourceSet in hostSpecificSourceSets) { 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<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 parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProjectStructureMetadata? { 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<String, Set<String>>() val sourceSetsByVariant = mutableMapOf<String, Set<String>>()
variantsNode.childNodes.elements.filter { it.tagName == "variant" }.forEach { variantNode -> variantsNode.childNodes.elements.filter { it.tagName == VARIANT_NODE_NAME }.forEach { variantNode ->
val variantName = variantNode.getElementsByTagName("name").elements.single().textContent val variantName = variantNode.getElementsByTagName(NAME_NODE_NAME).elements.single().textContent
val sourceSets = variantNode.childNodes.elements.filter { it.tagName == "sourceSet" }.mapTo(mutableSetOf()) { it.textContent } val sourceSets =
variantNode.childNodes.elements.filter { it.tagName == SOURCE_SET_NODE_NAME }.mapTo(mutableSetOf()) { it.textContent }
sourceSetsByVariant[variantName] = sourceSets sourceSetsByVariant[variantName] = sourceSets
} }
@@ -187,27 +188,27 @@ internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProj
val sourceSetBinaryLayout = mutableMapOf<String, SourceSetMetadataLayout>() val sourceSetBinaryLayout = mutableMapOf<String, SourceSetMetadataLayout>()
val hostSpecificSourceSets = mutableSetOf<String>() val hostSpecificSourceSets = mutableSetOf<String>()
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 -> sourceSetsNode.childNodes.elements.filter { it.tagName == SOURCE_SET_NODE_NAME }.forEach { sourceSetNode ->
val sourceSetName = sourceSetNode.getElementsByTagName("name").elements.single().textContent val sourceSetName = sourceSetNode.getElementsByTagName(NAME_NODE_NAME).elements.single().textContent
val dependsOn = mutableSetOf<String>() val dependsOn = mutableSetOf<String>()
val moduleDependencies = mutableSetOf<ModuleDependencyIdentifier>() val moduleDependencies = mutableSetOf<ModuleDependencyIdentifier>()
sourceSetNode.childNodes.elements.forEach { node -> sourceSetNode.childNodes.elements.forEach { node ->
when (node.tagName) { when (node.tagName) {
"dependsOn" -> dependsOn.add(node.textContent) DEPENDS_ON_NODE_NAME -> dependsOn.add(node.textContent)
"moduleDependency" -> { MODULE_DEPENDENCY_NODE_NAME -> {
val (groupId, moduleId) = node.textContent.split(":") val (groupId, moduleId) = node.textContent.split(":")
moduleDependencies.add(ModuleDependencyIdentifier(groupId, moduleId)) moduleDependencies.add(ModuleDependencyIdentifier(groupId, moduleId))
} }
"binaryLayout" -> { BINARY_LAYOUT_NODE_NAME -> {
SourceSetMetadataLayout.byName(node.textContent)?.let { binaryLayout -> SourceSetMetadataLayout.byName(node.textContent)?.let { binaryLayout ->
sourceSetBinaryLayout[sourceSetName] = binaryLayout sourceSetBinaryLayout[sourceSetName] = binaryLayout
} }
} }
"hostSpecific" -> { HOST_SPECIFIC_NODE_NAME -> {
if (node.textContent == "true") { if (node.textContent == "true") {
hostSpecificSourceSets.add(sourceSetName) hostSpecificSourceSets.add(sourceSetName)
} }
@@ -227,4 +228,16 @@ internal fun parseKotlinSourceSetMetadataFromXml(document: Document): KotlinProj
hostSpecificSourceSets, hostSpecificSourceSets,
formatVersion formatVersion
) )
} }
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"