GTW-8605 use JDOM

GitOrigin-RevId: 74148fc93bf8320a3ecb53d878b2f7e6b0064ca0
This commit is contained in:
Vladimir Krivosheev
2024-05-07 07:50:32 +02:00
committed by intellij-monorepo-bot
parent a7eaa28945
commit 9b73ef4d95
6 changed files with 131 additions and 169 deletions

View File

@@ -1,18 +0,0 @@
<component name="libraryTable">
<library name="decentxml" type="repository">
<properties maven-id="de.pdark:decentxml:1.4">
<verification>
<artifact url="file://$MAVEN_REPOSITORY$/de/pdark/decentxml/1.4/decentxml-1.4.jar">
<sha256sum>fe671ff552a8d724710e37fdb224b19c9aaf16ed3ce8dc8bbc694788f79edd41</sha256sum>
</artifact>
</verification>
</properties>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/de/pdark/decentxml/1.4/decentxml-1.4.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/de/pdark/decentxml/1.4/decentxml-1.4-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -61,7 +61,6 @@
<orderEntry type="module" module-name="intellij.platform.util.jdom" />
<orderEntry type="library" name="jackson-databind" level="project" />
<orderEntry type="library" name="json-schema-validator" level="project" />
<orderEntry type="library" name="decentxml" level="project" />
<orderEntry type="module" module-name="intellij.platform.diagnostic.telemetry" />
<orderEntry type="library" name="kotlinx-serialization-json" level="project" />
<orderEntry type="library" name="kotlinx-serialization-core" level="project" />

View File

@@ -313,8 +313,6 @@ object CommunityLibraryLicenses {
url = "https://github.com/hypfvieh/dbus-java",
licenseUrl = "https://github.com/hypfvieh/dbus-java/blob/dbus-java-3.0/LICENSE")
.suppliedByPersons("David M. <hypfvieh@googlemail.com>"),
LibraryLicense(name = "DecentXML", libraryName = "decentxml",
url = "https://code.google.com/p/decentxml").newBsd(),
LibraryLicense(name = "docutils", attachedTo = "intellij.python", version = "0.12",
url = "https://docutils.sourceforge.io/").simplifiedBsd(),
LibraryLicense(name = "dotenv-kotlin", libraryName = "io.github.cdimascio.dotenv.kotlin",

View File

@@ -1,8 +1,10 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.intellij.build.impl
import de.pdark.decentxml.*
import com.intellij.openapi.util.JDOMUtil
import io.opentelemetry.api.trace.Span
import org.jdom.Element
import org.jdom.Text
import org.jetbrains.annotations.TestOnly
import org.jetbrains.intellij.build.BuildContext
import org.jetbrains.intellij.build.CompatibleBuildRange
@@ -34,12 +36,14 @@ fun getCompatiblePlatformVersionRange(compatibleBuildRange: CompatibleBuildRange
return Pair(sinceBuild, untilBuild)
}
internal fun patchPluginXml(moduleOutputPatcher: ModuleOutputPatcher,
plugin: PluginLayout,
releaseDate: String,
releaseVersion: String,
pluginsToPublish: Set<PluginLayout?>,
context: BuildContext) {
internal fun patchPluginXml(
moduleOutputPatcher: ModuleOutputPatcher,
plugin: PluginLayout,
releaseDate: String,
releaseVersion: String,
pluginsToPublish: Set<PluginLayout?>,
context: BuildContext,
) {
val moduleOutput = context.getModuleOutputDir(context.findRequiredModule(plugin.mainModule))
val pluginXmlFile = moduleOutput.resolve("META-INF/plugin.xml")
if (Files.notExists(pluginXmlFile)) {
@@ -59,17 +63,18 @@ internal fun patchPluginXml(moduleOutputPatcher: ModuleOutputPatcher,
val sinceUntil = getCompatiblePlatformVersionRange(compatibleBuildRange, context.buildNumber)
@Suppress("TestOnlyProblems") val content = try {
plugin.pluginXmlPatcher(
// using input stream allows us to support BOM
doPatchPluginXml(document = Files.newInputStream(pluginXmlFile).use { XMLParser().parse(XMLIOSource(it)) },
pluginModuleName = plugin.mainModule,
pluginVersion = pluginVersion,
releaseDate = releaseDate,
releaseVersion = releaseVersion,
compatibleSinceUntil = sinceUntil,
toPublish = pluginsToPublish.contains(plugin),
retainProductDescriptorForBundledPlugin = plugin.retainProductDescriptorForBundledPlugin,
isEap = context.applicationInfo.isEAP,
productName = context.applicationInfo.fullProductName),
doPatchPluginXml(
rootElement = JDOMUtil.load(pluginXmlFile),
pluginModuleName = plugin.mainModule,
pluginVersion = pluginVersion,
releaseDate = releaseDate,
releaseVersion = releaseVersion,
compatibleSinceUntil = sinceUntil,
toPublish = pluginsToPublish.contains(plugin),
retainProductDescriptorForBundledPlugin = plugin.retainProductDescriptorForBundledPlugin,
isEap = context.applicationInfo.isEAP,
productName = context.applicationInfo.fullProductName,
),
context,
)
}
@@ -80,17 +85,18 @@ internal fun patchPluginXml(moduleOutputPatcher: ModuleOutputPatcher,
}
@TestOnly
fun doPatchPluginXml(document: Document,
pluginModuleName: String,
pluginVersion: String?,
releaseDate: String,
releaseVersion: String,
compatibleSinceUntil: Pair<String, String>,
toPublish: Boolean,
retainProductDescriptorForBundledPlugin: Boolean,
isEap: Boolean,
productName: String): String {
val rootElement = document.rootElement
fun doPatchPluginXml(
rootElement: Element,
pluginModuleName: String,
pluginVersion: String?,
releaseDate: String,
releaseVersion: String,
compatibleSinceUntil: Pair<String, String>,
toPublish: Boolean,
retainProductDescriptorForBundledPlugin: Boolean,
isEap: Boolean,
productName: String,
): String {
val ideaVersionElement = getOrCreateTopElement(rootElement, "idea-version", listOf("id", "name"))
ideaVersionElement.setAttribute("since-build", compatibleSinceUntil.first)
ideaVersionElement.setAttribute("until-build", compatibleSinceUntil.second)
@@ -100,8 +106,7 @@ fun doPatchPluginXml(document: Document,
if (productDescriptor != null) {
if (!toPublish && !retainProductDescriptorForBundledPlugin) {
Span.current().addEvent("skip $pluginModuleName <product-descriptor/>")
removeTextBeforeElement(productDescriptor)
productDescriptor.remove()
productDescriptor.detach()
}
else {
Span.current().addEvent("patch $pluginModuleName <product-descriptor/>")
@@ -122,7 +127,7 @@ fun doPatchPluginXml(document: Document,
val replaced = replaceInElementText(description, "IntelliJ-based IDEs", "WebStorm")
check(replaced) { "Could not find \'IntelliJ-based IDEs\' in plugin description of $pluginModuleName" }
}
return document.toXML()
return JDOMUtil.write(rootElement)
}
fun getOrCreateTopElement(rootElement: Element, tagName: String, anchors: List<String>): Element {
@@ -133,39 +138,23 @@ fun getOrCreateTopElement(rootElement: Element, tagName: String, anchors: List<S
val newElement = Element(tagName)
val anchor = anchors.asSequence().mapNotNull { rootElement.getChild(it) }.firstOrNull()
if (anchor == null) {
rootElement.addNode(0, newElement)
rootElement.addNode(0, Text("\n "))
rootElement.addContent(0, newElement)
}
else {
val anchorIndex = rootElement.nodeIndexOf(anchor)
val anchorIndex = rootElement.indexOf(anchor)
// should not happen
check(anchorIndex >= 0) {
"anchor < 0 when getting child index of \'${anchor.name}\' in root element of ${rootElement.toXML()}"
"anchor < 0 when getting child index of \'${anchor.name}\' in root element of ${JDOMUtil.write(rootElement)}"
}
var indent = rootElement.getNode(anchorIndex - 1)
indent = if (indent is Text) indent.copy() else Text("")
rootElement.addNode(anchorIndex + 1, newElement)
rootElement.addNode(anchorIndex + 1, indent)
rootElement.addContent(anchorIndex + 1, newElement)
}
return newElement
}
private fun removeTextBeforeElement(element: Element) {
val parentElement = element.parentElement ?: throw IllegalStateException("Could not find parent of \'${element.toXML()}\'")
val elementIndex = parentElement.nodeIndexOf(element)
check(elementIndex >= 0) { "Could not find element index \'${element.toXML()}\' in parent \'${parentElement.toXML()}\'" }
if (elementIndex > 0) {
val text = parentElement.getNode(elementIndex - 1)
if (text is Text) {
parentElement.removeNode(elementIndex - 1)
}
}
}
@Suppress("SameParameterValue")
private fun replaceInElementText(element: Element, oldText: String, newText: String): Boolean {
var replaced = false
for (node in element.nodes) {
for (node in element.content) {
if (node is Text) {
val textBefore = node.text
val text = textBefore.replace(oldText, newText)

View File

@@ -19,12 +19,12 @@
<orderEntry type="library" scope="TEST" name="assertJ" level="project" />
<orderEntry type="library" scope="TEST" name="JUnit5" level="project" />
<orderEntry type="module" module-name="intellij.platform.testExtensions" scope="TEST" />
<orderEntry type="library" scope="TEST" name="decentxml" level="project" />
<orderEntry type="module" module-name="intellij.idea.community.build.tasks" scope="TEST" />
<orderEntry type="library" scope="TEST" name="kotlinx-collections-immutable" level="project" />
<orderEntry type="library" scope="TEST" name="commons-compress" level="project" />
<orderEntry type="module" module-name="intellij.idea.community.build" scope="TEST" />
<orderEntry type="library" scope="TEST" name="kotlinx-coroutines-core" level="project" />
<orderEntry type="module" module-name="intellij.platform.util.zip" scope="TEST" />
<orderEntry type="module" module-name="intellij.platform.util.jdom" scope="TEST" />
</component>
</module>

View File

@@ -1,8 +1,9 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.intellij.build
import de.pdark.decentxml.XMLParser
import com.intellij.openapi.util.JDOMUtil
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.jetbrains.intellij.build.impl.doPatchPluginXml
import org.junit.jupiter.api.Test
@@ -17,12 +18,12 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<version>x-plugin-version</version>
<name>CSS</name>
<id>com.intellij.css</id>
<idea-version since-build="new-since" until-build="new-until"/>
</idea-plugin>
<idea-plugin>
<version>x-plugin-version</version>
<name>CSS</name>
<id>com.intellij.css</id>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent())
@Test
@@ -34,12 +35,12 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent())
@Test
@@ -48,9 +49,10 @@ class PluginXmlPatcherTest {
<idea-plugin></idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/></idea-plugin>
<idea-plugin>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent())
@Test
@@ -63,12 +65,12 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent())
}
@@ -83,12 +85,12 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent())
}
@@ -105,12 +107,12 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
</idea-plugin>
""".trimIndent(),
toPublish = false
)
@@ -128,14 +130,13 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
<product-descriptor code="PDB" release-version="X-RELEASE-VERSION-X" release-date="X-RELEASE-DATE-X"/>
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
<product-descriptor code="PDB" release-version="X-RELEASE-VERSION-X" release-date="X-RELEASE-DATE-X" />
</idea-plugin>
""".trimIndent(),
toPublish = true,
isEap = false
@@ -154,14 +155,13 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
<product-descriptor code="PDB" eap="true" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" />
</idea-plugin>
<idea-plugin>
<name>CSS</name>
<id>com.intellij.css</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
<product-descriptor code="PDB" eap="true" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" />
</idea-plugin>
""".trimIndent(),
toPublish = true,
isEap = true
@@ -182,18 +182,14 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<name>Database Tools and SQL for WebStorm</name>
<id>com.intellij.database</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
<product-descriptor code="PDB" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X"/>
<description>
<![CDATA[
xxx for WebStorm provides
]]>
</description>
</idea-plugin>
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<name>Database Tools and SQL for WebStorm</name>
<id>com.intellij.database</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
<product-descriptor code="PDB" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" />
<description>xxx for WebStorm provides</description>
</idea-plugin>
""".trimIndent(),
productName = "WebStorm",
toPublish = true,
@@ -214,18 +210,14 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<name>Database Tools and SQL</name>
<id>com.intellij.database</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
<product-descriptor code="PDB" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X"/>
<description>
<![CDATA[
xxx for IntelliJ-based IDEs provides
]]>
</description>
</idea-plugin>
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<name>Database Tools and SQL</name>
<id>com.intellij.database</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
<product-descriptor code="PDB" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" />
<description>xxx for IntelliJ-based IDEs provides</description>
</idea-plugin>
""".trimIndent(),
toPublish = true,
)
@@ -239,35 +231,37 @@ class PluginXmlPatcherTest {
</idea-plugin>
""".trimIndent(),
"""
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<id>com</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until"/>
<product-descriptor code="PCWMP" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" optional="true"/>
</idea-plugin>
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude">
<id>com</id>
<version>x-plugin-version</version>
<idea-version since-build="new-since" until-build="new-until" />
<product-descriptor code="PCWMP" release-date="X-RELEASE-DATE-X" release-version="X-RELEASE-VERSION-X" optional="true" />
</idea-plugin>
""".trimIndent(),
retainProductDescriptorForBundledPlugin = true,
toPublish = false,
)
private fun assertTransform(
before: String,
after: String,
@Language("XML") before: String,
@Language("XML") after: String,
productName: String = "UnExistent",
toPublish: Boolean = false,
isEap: Boolean = false,
retainProductDescriptorForBundledPlugin: Boolean = false,
) {
val result = doPatchPluginXml(document = XMLParser.parse(before),
pluginModuleName = "x-plugin-module-name",
pluginVersion = "x-plugin-version",
releaseDate = "X-RELEASE-DATE-X",
releaseVersion = "X-RELEASE-VERSION-X",
compatibleSinceUntil = Pair("new-since", "new-until"),
toPublish = toPublish,
retainProductDescriptorForBundledPlugin = retainProductDescriptorForBundledPlugin,
isEap = isEap,
productName = productName)
val result = doPatchPluginXml(
rootElement = JDOMUtil.load(before),
pluginModuleName = "x-plugin-module-name",
pluginVersion = "x-plugin-version",
releaseDate = "X-RELEASE-DATE-X",
releaseVersion = "X-RELEASE-VERSION-X",
compatibleSinceUntil = Pair("new-since", "new-until"),
toPublish = toPublish,
retainProductDescriptorForBundledPlugin = retainProductDescriptorForBundledPlugin,
isEap = isEap,
productName = productName,
)
assertThat(result).isEqualTo(after)
}
}