1/*
2 * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5
6/*
7 * Publish the platform JAR and POM so that consumers who depend on this module and can't read Gradle module metadata
8 * can still get the platform artifact and transitive dependencies from the POM.
9 *
10 * See the full rationale here https://youtrack.jetbrains.com/issue/KMM-237#focus=streamItem-27-4115233.0-0
11 */
12project.ext.publishPlatformArtifactsInRootModule = { platformPublication ->
13    def platformPomBuilder = null
14
15    platformPublication.pom.withXml { platformPomBuilder = asString() }
16
17    publishing.publications.kotlinMultiplatform  {
18        platformPublication.artifacts.forEach {
19            artifact(it)
20        }
21
22        pom.withXml {
23            def pomStringBuilder = asString()
24            pomStringBuilder.setLength(0)
25            // The platform POM needs its artifact ID replaced with the artifact ID of the root module:
26            def platformPomString = platformPomBuilder.toString()
27            platformPomString.eachLine { line ->
28                if (!line.contains("<!--")) { // Remove the Gradle module metadata marker as it will be added anew
29                    pomStringBuilder.append(line.replace(platformPublication.artifactId, artifactId))
30                    pomStringBuilder.append("\n")
31                }
32            }
33        }
34    }
35
36    tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication"}.configureEach {
37        dependsOn(tasks["generatePomFileFor${platformPublication.name.capitalize()}Publication"])
38    }
39}