Skip to content

Commit c39aa37

Browse files
committed
Update task-selector workaround to use task rules
- Workaround for gradle/gradle#22335 - Update tasks: - check runs all validations - test runs apiCheck - introduce 'publishAllPublicationsToRemoteRepositories' lifecycle task
1 parent 671e856 commit c39aa37

File tree

2 files changed

+133
-62
lines changed

2 files changed

+133
-62
lines changed

build-logic/src/main/kotlin/dokkabuild.base.gradle.kts

+71
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,74 @@ val integrationTestPreparation by tasks.registering {
2828
"lifecycle task for preparing the project for integration tests (for example, publishing to the test Maven repo)"
2929
group = VERIFICATION_GROUP
3030
}
31+
32+
33+
//val allProjectTasksPrefix = "allProjects_"
34+
val subprojectTasksPrefix = "subprojectTasks_"
35+
val allProjectTasksPrefix = "allProjectTasks_"
36+
val includedBuildTasksPrefix = "includedBuildTasks_"
37+
38+
//region Workarounds for running all tasks in included builds
39+
// https://github.com/gradle/gradle/issues/22335
40+
if (project == rootProject) {
41+
42+
tasks.addRule("Pattern: ${includedBuildTasksPrefix}<TASK>") {
43+
// Propagate <TASK> to all included builds.
44+
// (Except build-logic subprojects, because they don't need to be published/tested/etc).
45+
val taskName = this
46+
47+
if (startsWith(includedBuildTasksPrefix)) {
48+
task(taskName) {
49+
val requestedTaskName = taskName.removePrefix(includedBuildTasksPrefix)
50+
val includedBuildTasks = gradle.includedBuilds
51+
.filter { it.name != "build-logic" }
52+
.filter { it.name != "build-settings-logic" }
53+
.map { includedBuild ->
54+
includedBuild.task(":${subprojectTasksPrefix}${requestedTaskName}")
55+
}
56+
dependsOn(includedBuildTasks)
57+
}
58+
}
59+
}
60+
61+
tasks.addRule("Pattern: $subprojectTasksPrefix<TASK>") {
62+
// Run all <TASK>s in the current project's subprojects.
63+
// This should only be run via the 'includedBuildTasks' rule.
64+
val taskName = this
65+
66+
if (startsWith(subprojectTasksPrefix)) {
67+
task(taskName) {
68+
val requestedTaskName = taskName.removePrefix(subprojectTasksPrefix)
69+
val allProjectTasks = subprojects.map { project ->
70+
project.tasks.matching { it.name == requestedTaskName }
71+
}
72+
dependsOn(allProjectTasks)
73+
}
74+
}
75+
}
76+
77+
// Setup lifecycle tasks dependencies, so each is propagated to included builds.
78+
tasks.assemble {
79+
dependsOn("${includedBuildTasksPrefix}assemble")
80+
}
81+
82+
tasks.build {
83+
dependsOn("${includedBuildTasksPrefix}build")
84+
}
85+
86+
tasks.clean {
87+
dependsOn("${includedBuildTasksPrefix}clean")
88+
}
89+
90+
tasks.check {
91+
dependsOn("${includedBuildTasksPrefix}check")
92+
}
93+
}
94+
//endregion
95+
96+
val testTasks = tasks.matching { it.name == "test" }
97+
val apiCheckTasks = tasks.matching { it.name == "apiCheck" }
98+
99+
testTasks.configureEach {
100+
dependsOn(apiCheckTasks)
101+
}

build.gradle.kts

+62-62
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
11
/*
22
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
4+
import org.gradle.api.publish.plugins.PublishingPlugin.PUBLISH_TASK_GROUP
5+
import org.gradle.language.base.plugins.LifecycleBasePlugin.VERIFICATION_GROUP
46

57
plugins {
68
id("dokkabuild.base")
79
}
810

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

12-
addDependencyOnSameTasksOfIncludedBuilds("assemble", "build", "clean", "check")
21+
val publishPlugins by tasks.registering {
22+
group = "gradle plugin"
23+
dependsOnIncludedBuildTasks()
24+
}
1325

14-
registerParentGroupTasks("publishing", taskNames = listOf(
15-
"publishAllPublicationsToMavenCentralRepository",
16-
"publishAllPublicationsToProjectLocalRepository",
17-
"publishAllPublicationsToSnapshotRepository",
18-
"publishAllPublicationsToSpaceDevRepository",
19-
"publishAllPublicationsToSpaceTestRepository",
20-
"publishToMavenLocal"
21-
)) {
22-
it.name in publishedIncludedBuilds
26+
val validatePlugins by tasks.registering {
27+
group = "gradle plugin"
28+
dependsOnIncludedBuildTasks()
2329
}
2430

25-
registerParentGroupTasks("gradle plugin", taskNames = listOf(
26-
"publishPlugins",
27-
"validatePlugins"
28-
)) {
29-
it.name in gradlePluginIncludedBuilds
31+
val apiDump by tasks.registering {
32+
group = "$VERIFICATION_GROUP bcv"
33+
dependsOnIncludedBuildTasks()
3034
}
3135

32-
registerParentGroupTasks("bcv", taskNames = listOf(
33-
"apiDump",
34-
"apiCheck",
35-
"apiBuild"
36-
)) {
37-
it.name in publishedIncludedBuilds
36+
val apiCheck by tasks.registering {
37+
group = "$VERIFICATION_GROUP bcv"
38+
dependsOnIncludedBuildTasks()
3839
}
3940

40-
registerParentGroupTasks("verification", taskNames = listOf(
41-
"test"
42-
))
41+
val test by tasks.registering {
42+
group = VERIFICATION_GROUP
43+
dependsOnIncludedBuildTasks()
44+
dependsOn(apiCheck)
45+
}
4346

44-
tasks.register("integrationTest") {
45-
group = "verification"
47+
val integrationTest by tasks.registering {
48+
group = VERIFICATION_GROUP
49+
dependsOnIncludedBuildTasks()
4650
description = "Runs integration tests of this project. Might take a while and require additional setup."
47-
48-
dependsOn(includedBuildTasks("integrationTest") {
49-
it.name == "dokka-integration-tests"
50-
})
5151
}
5252

53-
fun addDependencyOnSameTasksOfIncludedBuilds(vararg taskNames: String) {
54-
taskNames.forEach { taskName ->
55-
tasks.named(taskName) {
56-
dependsOn(includedBuildTasks(taskName))
57-
}
58-
}
53+
val publishAllPublicationsToRemoteRepositories by tasks.registering {
54+
group = PUBLISH_TASK_GROUP
55+
dependsOnIncludedBuildTasks("publishAllPublicationsToMavenCentralRepository")
56+
dependsOnIncludedBuildTasks("publishAllPublicationsToProjectLocalRepository")
57+
dependsOnIncludedBuildTasks("publishAllPublicationsToSnapshotRepository")
58+
dependsOnIncludedBuildTasks("publishAllPublicationsToSpaceDevRepository")
5959
}
60-
61-
fun registerParentGroupTasks(
62-
groupName: String,
63-
taskNames: List<String>,
64-
includedBuildFilter: (IncludedBuild) -> Boolean = { true }
65-
) = taskNames.forEach { taskName ->
66-
tasks.register(taskName) {
67-
group = groupName
68-
description = "A parent task that calls tasks with the same name in all subprojects and included builds"
69-
70-
dependsOn(subprojectTasks(taskName), includedBuildTasks(taskName, includedBuildFilter))
71-
}
60+
val publishAllPublicationsToMavenCentralRepository by tasks.registering {
61+
group = PUBLISH_TASK_GROUP
62+
dependsOnIncludedBuildTasks()
7263
}
73-
74-
fun subprojectTasks(taskName: String): List<String> =
75-
subprojects
76-
.filter { it.getTasksByName(taskName, false).isNotEmpty() }
77-
.map { ":${it.path}:$taskName" }
78-
79-
80-
fun includedBuildTasks(taskName: String, filter: (IncludedBuild) -> Boolean = { true }): List<TaskReference> =
81-
gradle.includedBuilds
82-
.filter { it.name != "build-logic" }
83-
.filter(filter)
84-
.mapNotNull { it.task(":$taskName") }
64+
val publishAllPublicationsToProjectLocalRepository by tasks.registering {
65+
group = PUBLISH_TASK_GROUP
66+
dependsOnIncludedBuildTasks()
67+
}
68+
val publishAllPublicationsToSnapshotRepository by tasks.registering {
69+
group = PUBLISH_TASK_GROUP
70+
dependsOnIncludedBuildTasks()
71+
}
72+
val publishAllPublicationsToSpaceDevRepository by tasks.registering {
73+
group = PUBLISH_TASK_GROUP
74+
dependsOnIncludedBuildTasks()
75+
}
76+
val publishAllPublicationsToSpaceTestRepository by tasks.registering {
77+
group = PUBLISH_TASK_GROUP
78+
dependsOnIncludedBuildTasks()
79+
}
80+
val publishToMavenLocal by tasks.registering {
81+
group = PUBLISH_TASK_GROUP
82+
dependsOnIncludedBuildTasks()
83+
}
84+
//endregion

0 commit comments

Comments
 (0)