|
| 1 | +package io.sentry.gradle |
| 2 | + |
| 3 | +import com.vanniktech.maven.publish.JavaLibrary |
| 4 | +import com.vanniktech.maven.publish.JavadocJar |
| 5 | +import com.vanniktech.maven.publish.MavenPublishBaseExtension |
| 6 | +import java.io.File |
| 7 | +import org.gradle.api.Plugin |
| 8 | +import org.gradle.api.Project |
| 9 | +import org.gradle.api.distribution.DistributionContainer |
| 10 | +import org.gradle.api.distribution.plugins.DistributionPlugin |
| 11 | +import org.gradle.api.plugins.JavaPluginExtension |
| 12 | +import org.gradle.api.publish.PublishingExtension |
| 13 | +import org.gradle.kotlin.dsl.configure |
| 14 | + |
| 15 | +/** Publishes a JVM or Android module and packages it as the zip distribution craft consumes. */ |
| 16 | +class SentryPublishPlugin : Plugin<Project> { |
| 17 | + override fun apply(project: Project) { |
| 18 | + project.configurePublishing(DistributionContainer::configureForJvm) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Publishes a Kotlin Multiplatform module. Separate from [SentryPublishPlugin] because the |
| 24 | + * per-target distributions it lays out have no counterpart in a single-target module. |
| 25 | + */ |
| 26 | +class SentryMultiplatformPublishPlugin : Plugin<Project> { |
| 27 | + override fun apply(project: Project) { |
| 28 | + project.configurePublishing(DistributionContainer::configureForMultiplatform) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +private fun Project.configurePublishing( |
| 33 | + configureDistributions: DistributionContainer.(Project) -> Unit |
| 34 | +) { |
| 35 | + pluginManager.apply(DistributionPlugin::class.java) |
| 36 | + pluginManager.apply("com.vanniktech.maven.publish") |
| 37 | + |
| 38 | + extensions.configure<DistributionContainer> { |
| 39 | + configureDistributions(project) |
| 40 | + // craft only uses zip archives |
| 41 | + forEach { dist -> |
| 42 | + if (dist.name == DistributionPlugin.MAIN_DISTRIBUTION_NAME) { |
| 43 | + tasks.named("distTar").configure { enabled = false } |
| 44 | + } else { |
| 45 | + tasks.named(dist.name + "DistTar").configure { enabled = false } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + val sep = File.separator |
| 51 | + val distributionFile = |
| 52 | + layout.buildDirectory.file("distributions$sep$name-$version.zip").get().asFile |
| 53 | + |
| 54 | + tasks.named("distZip").configure { |
| 55 | + dependsOn("publishToMavenLocal") |
| 56 | + dependsOn("publishAllPublicationsTo${DIST_MAVEN_REPO.replaceFirstChar { it.uppercase() }}Repository") |
| 57 | + val file = distributionFile |
| 58 | + doLast { |
| 59 | + if (!file.exists()) { |
| 60 | + throw IllegalStateException("Distribution file: ${file.absolutePath} does not exist") |
| 61 | + } |
| 62 | + if (file.length() == 0L) { |
| 63 | + throw IllegalStateException("Distribution file: ${file.absolutePath} is empty") |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + extensions.configure<MavenPublishBaseExtension> { assignAarTypes() } |
| 69 | + |
| 70 | + plugins.withId("java-library") { |
| 71 | + extensions.configure<MavenPublishBaseExtension> { |
| 72 | + // we have to disable javadoc publication in maven-publish plugin as it's not |
| 73 | + // including it in the .module file |
| 74 | + // https://github.com/vanniktech/gradle-maven-publish-plugin/issues/861 |
| 75 | + // and do it ourselves |
| 76 | + configure(JavaLibrary(JavadocJar.None(), sourcesJar = true)) |
| 77 | + } |
| 78 | + |
| 79 | + extensions.configure<JavaPluginExtension> { withJavadocJar() } |
| 80 | + } |
| 81 | + |
| 82 | + extensions.configure<PublishingExtension> { |
| 83 | + // the dist zips craft publishes are assembled from this, see Publication.kt |
| 84 | + repositories.maven { |
| 85 | + name = DIST_MAVEN_REPO |
| 86 | + url = distMavenDir.get().asFile.toURI() |
| 87 | + } |
| 88 | + // this is needed for sentry-unity to consume our artifacts locally as proper maven publication |
| 89 | + repositories.maven { |
| 90 | + name = "unityMaven" |
| 91 | + url = isolated.rootProject.projectDirectory.dir("build${sep}unityMaven").asFile.toURI() |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments