Skip to content

Commit

Permalink
KTOR-7738 Add aggregating publish tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Nov 13, 2024
1 parent ad4813e commit 0c15320
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/NamedDomainObjectCollection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

import org.gradle.api.*

internal fun <T> NamedDomainObjectCollection<T>.maybeNamed(name: String): NamedDomainObjectProvider<T>? {
return if (name in names) named(name) else null
}

internal inline fun <reified T> NamedDomainObjectCollection<*>.findByName(name: String): T? = findByName(name) as? T
118 changes: 76 additions & 42 deletions buildSrc/src/main/kotlin/Publication.kt
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import internal.*
import org.gradle.api.*
import org.gradle.api.publish.*
import org.gradle.api.publish.maven.*
import org.gradle.api.publish.maven.tasks.*
import org.gradle.api.publish.plugins.*
import org.gradle.jvm.tasks.*
import org.gradle.kotlin.dsl.*
import org.gradle.plugins.signing.*
import java.util.concurrent.locks.*

fun isAvailableForPublication(publication: Publication): Boolean {
val name = publication.name
if (name == "maven") return true

var result = false
val jvmAndCommon = setOf(
"jvm",
"androidRelease",
"androidDebug",
"js",
"metadata",
"kotlinMultiplatform"
)
result = result || name in jvmAndCommon
result = result || (HOST_NAME == "linux" && (name == "linuxX64" || name == "linuxArm64"))
result = result || (HOST_NAME == "windows" && name == "mingwX64")
val macPublications = setOf(
"iosX64",
"iosArm64",
"iosSimulatorArm64",

"watchosX64",
"watchosArm32",
"watchosArm64",
"watchosSimulatorArm64",

"tvosX64",
"tvosArm64",
"tvosSimulatorArm64",

"macosX64",
"macosArm64"
)

result = result || (HOST_NAME == "macos" && name in macPublications)

return result
}
private val jvmAndCommonTargets = setOf(
"jvm",
"androidRelease",
"androidDebug",
"metadata",
"kotlinMultiplatform",
"maven",
)

private val jsTargets = setOf(
"js",
)

private val linuxTargets = setOf(
"linuxX64",
"linuxArm64",
)

private val windowsTargets = setOf(
"mingwX64",
)

private val darwinTargets = setOf(
"iosX64",
"iosArm64",
"iosSimulatorArm64",

"watchosX64",
"watchosArm32",
"watchosArm64",
"watchosSimulatorArm64",

"tvosX64",
"tvosArm64",
"tvosSimulatorArm64",

"macosX64",
"macosArm64",
)

fun Project.configurePublication() {
if (COMMON_JVM_ONLY) return

apply(plugin = "maven-publish")

tasks.withType<AbstractPublishToMaven>().all {
onlyIf { isAvailableForPublication(publication) }
onlyIf { publication.isAvailableForPublication() }
}
configureAggregatingTasks()

val publishingUser: String? = System.getenv("PUBLISHING_USER")
val publishingPassword: String? = System.getenv("PUBLISHING_PASSWORD")
Expand Down Expand Up @@ -89,7 +93,7 @@ fun Project.configurePublication() {
}
maven {
name = "testLocal"
setUrl("${rootProject.layout.buildDirectory.get().asFile}/m2")
setUrl(rootProject.layout.buildDirectory.dir("m2"))
}
}

Expand All @@ -98,7 +102,8 @@ fun Project.configurePublication() {

pom {
name = project.name
description = project.description?.takeIf { it.isNotEmpty() } ?: "Ktor is a framework for quickly creating web applications in Kotlin with minimal effort."
description = project.description.orEmpty()
.ifEmpty { "Ktor is a framework for quickly creating web applications in Kotlin with minimal effort." }
url = "https://github.com/ktorio/ktor"
licenses {
license {
Expand Down Expand Up @@ -130,6 +135,35 @@ fun Project.configurePublication() {
configureJavadocArtifact()
}

private fun Publication.isAvailableForPublication(): Boolean {
val name = name

var result = name in jvmAndCommonTargets || name in jsTargets
result = result || (HOST_NAME == "linux" && name in linuxTargets)
result = result || (HOST_NAME == "windows" && name in windowsTargets)
result = result || (HOST_NAME == "macos" && name in darwinTargets)

return result
}

private fun Project.configureAggregatingTasks() {
registerAggregatingTask("JvmAndCommon", jvmAndCommonTargets)
if (hasJs) registerAggregatingTask("Js", jsTargets)
if (hasLinux) registerAggregatingTask("Linux", linuxTargets)
if (hasWindows) registerAggregatingTask("Windows", windowsTargets)
if (hasDarwin) registerAggregatingTask("Darwin", darwinTargets)
}

private fun Project.registerAggregatingTask(name: String, targets: Set<String>) {
tasks.register("publish${name}Publications") {
group = PublishingPlugin.PUBLISH_TASK_GROUP
val targetsTasks = targets.mapNotNull { target ->
tasks.maybeNamed("publish${target.capitalized()}PublicationToMavenRepository")
}
dependsOn(targetsTasks)
}
}

private fun Project.configureSigning() {
extra["signing.gnupg.keyName"] = (System.getenv("SIGN_KEY_ID") ?: return)
extra["signing.gnupg.passphrase"] = (System.getenv("SIGN_KEY_PASSPHRASE") ?: return)
Expand Down
7 changes: 7 additions & 0 deletions buildSrc/src/main/kotlin/internal/String.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package internal

internal fun String.capitalized() = replaceFirstChar { it.uppercase() }

0 comments on commit 0c15320

Please sign in to comment.