Skip to content

Commit 547b10e

Browse files
committed
Build: Write ivy.xml without usage of internal gradle apis
1 parent bbbd47e commit 547b10e

File tree

1 file changed

+85
-31
lines changed

1 file changed

+85
-31
lines changed

buildSrc/prepare-deps/build.gradle.kts

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
@file:Suppress("PropertyName", "HasPlatformType", "UnstableApiUsage")
22

3-
import org.gradle.api.publish.ivy.internal.artifact.FileBasedIvyArtifact
4-
import org.gradle.api.publish.ivy.internal.publication.DefaultIvyConfiguration
5-
import org.gradle.api.publish.ivy.internal.publication.DefaultIvyPublicationIdentity
6-
import org.gradle.api.publish.ivy.internal.publisher.IvyDescriptorFileGenerator
3+
import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter
74
import org.gradle.internal.os.OperatingSystem
5+
import java.io.FileWriter
86
import java.net.URI
7+
import javax.xml.stream.XMLOutputFactory
8+
import javax.xml.stream.XMLStreamWriter
99

1010
plugins {
1111
base
@@ -310,40 +310,94 @@ fun writeIvyXml(
310310
&& !jar.name.startsWith("kotlin-")
311311
&& (allowAnnotations || jar.name != "annotations.jar") // see comments for [intellijAnnotations] above
312312

313-
with(IvyDescriptorFileGenerator(DefaultIvyPublicationIdentity(organization, moduleName, version))) {
314-
addConfiguration(DefaultIvyConfiguration("default"))
315-
addConfiguration(DefaultIvyConfiguration("sources"))
316-
artifactDir.listFiles()?.forEach { jarFile ->
317-
if (shouldIncludeIntellijJar(jarFile)) {
318-
val relativeName = jarFile.toRelativeString(baseDir).removeSuffix(".jar")
319-
addArtifact(
320-
FileBasedIvyArtifact(
321-
jarFile,
322-
DefaultIvyPublicationIdentity(organization, relativeName, version)
323-
).also {
324-
it.conf = "default"
313+
val ivyFile = targetDir.resolve("$fileName.ivy.xml")
314+
ivyFile.parentFile.mkdirs()
315+
FileWriter(ivyFile).use {
316+
val xmlWriter = IndentingXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(it))
317+
with(xmlWriter) {
318+
document("UTF-8", "1.0") {
319+
element("ivy-module") {
320+
attribute("version", "2.0")
321+
attribute("xmlns:m", "http://ant.apache.org/ivy/maven")
322+
323+
emptyElement("info") {
324+
attributes(
325+
"organisation" to organization,
326+
"module" to moduleName,
327+
"revision" to version,
328+
"publication" to ""
329+
)
330+
}
331+
332+
element("configurations") {
333+
listOf("default", "sources").forEach { configurationName ->
334+
emptyElement("conf") {
335+
attributes("name" to configurationName, "visibility" to "public")
336+
}
337+
}
325338
}
326-
)
327-
}
328-
}
329339

330-
sourcesJar.forEach {
331-
val sourcesArtifactName = it.name.substringBeforeLast("-").substringBeforeLast("-")
332-
addArtifact(
333-
FileBasedIvyArtifact(
334-
it,
335-
DefaultIvyPublicationIdentity(organization, sourcesArtifactName, version)
336-
).also { artifact ->
337-
artifact.conf = "sources"
338-
artifact.classifier = "sources"
340+
element("publications") {
341+
artifactDir.listFiles()?.filter(::shouldIncludeIntellijJar)?.forEach { jarFile ->
342+
val relativeName = jarFile.toRelativeString(baseDir).removeSuffix(".jar")
343+
emptyElement("artifact") {
344+
attributes(
345+
"name" to relativeName,
346+
"type" to "jar",
347+
"ext" to "jar",
348+
"conf" to "default"
349+
)
350+
}
351+
}
352+
353+
sourcesJar.forEach { jarFile ->
354+
emptyElement("artifact") {
355+
val sourcesArtifactName = jarFile.name
356+
.substringBeforeLast("-")
357+
.substringBeforeLast("-")
358+
359+
attributes(
360+
"name" to sourcesArtifactName,
361+
"type" to "jar",
362+
"ext" to "jar",
363+
"conf" to "sources",
364+
"m:classifier" to "sources"
365+
)
366+
}
367+
}
368+
}
339369
}
340-
)
341-
}
370+
}
342371

343-
writeTo(File(targetDir, "$fileName.ivy.xml"))
372+
flush()
373+
close()
374+
}
344375
}
345376
}
346377

347378
fun skipToplevelDirectory(path: String) = path.substringAfter('/')
348379

349380
fun skipContentsDirectory(path: String) = path.substringAfter("Contents/")
381+
382+
fun XMLStreamWriter.document(encoding: String, version: String, init: XMLStreamWriter.() -> Unit) = apply {
383+
writeStartDocument(encoding, version)
384+
init()
385+
writeEndDocument()
386+
}
387+
388+
fun XMLStreamWriter.element(name: String, init: XMLStreamWriter.() -> Unit) = apply {
389+
writeStartElement(name)
390+
init()
391+
writeEndElement()
392+
}
393+
394+
fun XMLStreamWriter.emptyElement(name: String, init: XMLStreamWriter.() -> Unit) = apply {
395+
writeEmptyElement(name)
396+
init()
397+
}
398+
399+
fun XMLStreamWriter.attribute(name: String, value: String): Unit = writeAttribute(name, value)
400+
401+
fun XMLStreamWriter.attributes(vararg attributes: Pair<String, String>) {
402+
attributes.forEach { attribute(it.first, it.second) }
403+
}

0 commit comments

Comments
 (0)