(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) =
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<Element> get() = (0 until length).map { this@elements.item(it) }.filterIsInstance<Element>()
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>>()
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<String, SourceSetMetadataLayout>()
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 ->
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<String>()
val moduleDependencies = mutableSetOf<ModuleDependencyIdentifier>()
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
)
}
}
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"