Skip to content

Commit

Permalink
Update task-selector workaround to use task rules
Browse files Browse the repository at this point in the history
- Workaround for gradle/gradle#22335
- Update tasks:
  - check runs all validations
  - test runs apiCheck
  - introduce 'publishAllPublicationsToRemoteRepositories' lifecycle task
  • Loading branch information
adam-enko committed Jan 29, 2024
1 parent 671e856 commit c39aa37
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 62 deletions.
71 changes: 71 additions & 0 deletions build-logic/src/main/kotlin/dokkabuild.base.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,74 @@ val integrationTestPreparation by tasks.registering {
"lifecycle task for preparing the project for integration tests (for example, publishing to the test Maven repo)"
group = VERIFICATION_GROUP
}


//val allProjectTasksPrefix = "allProjects_"
val subprojectTasksPrefix = "subprojectTasks_"
val allProjectTasksPrefix = "allProjectTasks_"
val includedBuildTasksPrefix = "includedBuildTasks_"

//region Workarounds for running all tasks in included builds
// https://github.com/gradle/gradle/issues/22335
if (project == rootProject) {

tasks.addRule("Pattern: ${includedBuildTasksPrefix}<TASK>") {
// Propagate <TASK> to all included builds.
// (Except build-logic subprojects, because they don't need to be published/tested/etc).
val taskName = this

if (startsWith(includedBuildTasksPrefix)) {
task(taskName) {
val requestedTaskName = taskName.removePrefix(includedBuildTasksPrefix)
val includedBuildTasks = gradle.includedBuilds
.filter { it.name != "build-logic" }
.filter { it.name != "build-settings-logic" }
.map { includedBuild ->
includedBuild.task(":${subprojectTasksPrefix}${requestedTaskName}")
}
dependsOn(includedBuildTasks)
}
}
}

tasks.addRule("Pattern: $subprojectTasksPrefix<TASK>") {
// Run all <TASK>s in the current project's subprojects.
// This should only be run via the 'includedBuildTasks' rule.
val taskName = this

if (startsWith(subprojectTasksPrefix)) {
task(taskName) {
val requestedTaskName = taskName.removePrefix(subprojectTasksPrefix)
val allProjectTasks = subprojects.map { project ->
project.tasks.matching { it.name == requestedTaskName }
}
dependsOn(allProjectTasks)
}
}
}

// Setup lifecycle tasks dependencies, so each is propagated to included builds.
tasks.assemble {
dependsOn("${includedBuildTasksPrefix}assemble")
}

tasks.build {
dependsOn("${includedBuildTasksPrefix}build")
}

tasks.clean {
dependsOn("${includedBuildTasksPrefix}clean")
}

tasks.check {
dependsOn("${includedBuildTasksPrefix}check")
}
}
//endregion

val testTasks = tasks.matching { it.name == "test" }
val apiCheckTasks = tasks.matching { it.name == "apiCheck" }

testTasks.configureEach {
dependsOn(apiCheckTasks)
}
124 changes: 62 additions & 62 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
/*
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import org.gradle.api.publish.plugins.PublishingPlugin.PUBLISH_TASK_GROUP
import org.gradle.language.base.plugins.LifecycleBasePlugin.VERIFICATION_GROUP

plugins {
id("dokkabuild.base")
}

val publishedIncludedBuilds = listOf("runner-cli", "runner-gradle-plugin-classic", "runner-maven-plugin")
val gradlePluginIncludedBuilds = listOf("runner-gradle-plugin-classic")
//region Workarounds for running all tasks in included builds
// https://github.com/gradle/gradle/issues/22335
// See build-logic/src/main/kotlin/dokkabuild.base.gradle.kts
fun Task.dependsOnIncludedBuildTasks(
taskName: String = name
) {
description = "Lifecycle task that runs '$taskName' in all included builds"
dependsOn("includedBuildTasks_$taskName")
}

addDependencyOnSameTasksOfIncludedBuilds("assemble", "build", "clean", "check")
val publishPlugins by tasks.registering {
group = "gradle plugin"
dependsOnIncludedBuildTasks()
}

registerParentGroupTasks("publishing", taskNames = listOf(
"publishAllPublicationsToMavenCentralRepository",
"publishAllPublicationsToProjectLocalRepository",
"publishAllPublicationsToSnapshotRepository",
"publishAllPublicationsToSpaceDevRepository",
"publishAllPublicationsToSpaceTestRepository",
"publishToMavenLocal"
)) {
it.name in publishedIncludedBuilds
val validatePlugins by tasks.registering {
group = "gradle plugin"
dependsOnIncludedBuildTasks()
}

registerParentGroupTasks("gradle plugin", taskNames = listOf(
"publishPlugins",
"validatePlugins"
)) {
it.name in gradlePluginIncludedBuilds
val apiDump by tasks.registering {
group = "$VERIFICATION_GROUP bcv"
dependsOnIncludedBuildTasks()
}

registerParentGroupTasks("bcv", taskNames = listOf(
"apiDump",
"apiCheck",
"apiBuild"
)) {
it.name in publishedIncludedBuilds
val apiCheck by tasks.registering {
group = "$VERIFICATION_GROUP bcv"
dependsOnIncludedBuildTasks()
}

registerParentGroupTasks("verification", taskNames = listOf(
"test"
))
val test by tasks.registering {
group = VERIFICATION_GROUP
dependsOnIncludedBuildTasks()
dependsOn(apiCheck)
}

tasks.register("integrationTest") {
group = "verification"
val integrationTest by tasks.registering {
group = VERIFICATION_GROUP
dependsOnIncludedBuildTasks()
description = "Runs integration tests of this project. Might take a while and require additional setup."

dependsOn(includedBuildTasks("integrationTest") {
it.name == "dokka-integration-tests"
})
}

fun addDependencyOnSameTasksOfIncludedBuilds(vararg taskNames: String) {
taskNames.forEach { taskName ->
tasks.named(taskName) {
dependsOn(includedBuildTasks(taskName))
}
}
val publishAllPublicationsToRemoteRepositories by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks("publishAllPublicationsToMavenCentralRepository")
dependsOnIncludedBuildTasks("publishAllPublicationsToProjectLocalRepository")
dependsOnIncludedBuildTasks("publishAllPublicationsToSnapshotRepository")
dependsOnIncludedBuildTasks("publishAllPublicationsToSpaceDevRepository")
}

fun registerParentGroupTasks(
groupName: String,
taskNames: List<String>,
includedBuildFilter: (IncludedBuild) -> Boolean = { true }
) = taskNames.forEach { taskName ->
tasks.register(taskName) {
group = groupName
description = "A parent task that calls tasks with the same name in all subprojects and included builds"

dependsOn(subprojectTasks(taskName), includedBuildTasks(taskName, includedBuildFilter))
}
val publishAllPublicationsToMavenCentralRepository by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}

fun subprojectTasks(taskName: String): List<String> =
subprojects
.filter { it.getTasksByName(taskName, false).isNotEmpty() }
.map { ":${it.path}:$taskName" }


fun includedBuildTasks(taskName: String, filter: (IncludedBuild) -> Boolean = { true }): List<TaskReference> =
gradle.includedBuilds
.filter { it.name != "build-logic" }
.filter(filter)
.mapNotNull { it.task(":$taskName") }
val publishAllPublicationsToProjectLocalRepository by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}
val publishAllPublicationsToSnapshotRepository by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}
val publishAllPublicationsToSpaceDevRepository by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}
val publishAllPublicationsToSpaceTestRepository by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}
val publishToMavenLocal by tasks.registering {
group = PUBLISH_TASK_GROUP
dependsOnIncludedBuildTasks()
}
//endregion

0 comments on commit c39aa37

Please sign in to comment.