-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put
sample
and integration-tests
into the delegate build
- Loading branch information
Showing
23 changed files
with
241 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
build-logic/conventions/src/main/kotlin/com/squareup/anvil/conventions/CompositePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.squareup.anvil.conventions | ||
|
||
import com.rickbusarow.kgx.checkProjectIsRoot | ||
import com.squareup.anvil.conventions.utils.namedOrNull | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.composite.internal.DefaultIncludedBuild | ||
import org.gradle.composite.internal.DefaultIncludedBuild.IncludedBuildImpl | ||
import org.gradle.internal.DefaultTaskExecutionRequest | ||
|
||
class CompositePlugin : Plugin<Project> { | ||
|
||
override fun apply(target: Project) { | ||
|
||
target.checkProjectIsRoot() | ||
require(target.gradle.includedBuilds.isNotEmpty()) { | ||
"Only apply the 'composite' plugin to a root project with included builds. " + | ||
"This project has no included builds, " + | ||
"so the plugin would just waste time searching the task graph." | ||
} | ||
|
||
target.gradle.projectsEvaluated { gradle -> | ||
|
||
val oldRequests = gradle.startParameter.taskRequests | ||
|
||
val newRequests = oldRequests.map { request -> | ||
|
||
val originalSplit = request.args | ||
.splitInclusive { !it.startsWith('-') } | ||
|
||
val taskPaths = originalSplit.mapTo(mutableSetOf()) { it.first() } | ||
|
||
val includedProjects = gradle.includedBuilds | ||
.asSequence() | ||
.filterIsInstance<IncludedBuildImpl>() | ||
.flatMap { impl -> | ||
|
||
val defaultIncludedBuild = impl.target as DefaultIncludedBuild | ||
|
||
defaultIncludedBuild.mutableModel.projectRegistry.allProjects | ||
} | ||
|
||
val newSplit = originalSplit.flatMap { taskWithArgs -> | ||
|
||
val taskName = taskWithArgs.first() | ||
|
||
if (taskName.startsWith(':')) { | ||
// qualified task names aren't propagated to included builds | ||
return@flatMap listOf(taskWithArgs) | ||
} | ||
|
||
val resolvedInRootBuild = target.tasks.namedOrNull(taskName) | ||
|
||
// Don't propagate help tasks | ||
if (taskName == "help") return@flatMap listOf(taskWithArgs) | ||
|
||
val inRoot = resolvedInRootBuild != null | ||
|
||
val included = includedProjects.mapNotNull { includedProject -> | ||
|
||
val includedPath = "${includedProject.identityPath}:$taskName" | ||
|
||
// Don't include tasks that are already in the task graph | ||
if (taskPaths.contains(includedPath)) return@mapNotNull null | ||
|
||
includedProject.tasks.namedOrNull(taskName) ?: return@mapNotNull null | ||
|
||
target.logger.quiet("The task $taskName will delegate to $includedPath") | ||
|
||
buildList<String> { | ||
add(includedPath) | ||
addAll(taskWithArgs.subList(1, taskWithArgs.size)) | ||
} | ||
} | ||
|
||
buildList { | ||
if (inRoot) { | ||
add(taskWithArgs) | ||
} | ||
addAll(included) | ||
} | ||
} | ||
|
||
DefaultTaskExecutionRequest.of(newSplit.flatten(), request.projectPath, request.rootDir) | ||
} | ||
|
||
gradle.startParameter.setTaskRequests(newRequests) | ||
} | ||
} | ||
|
||
private inline fun <E> List<E>.splitInclusive(predicate: (E) -> Boolean): List<List<E>> { | ||
|
||
val toSplit = this@splitInclusive | ||
|
||
if (toSplit.isEmpty()) return emptyList() | ||
|
||
return toSplit.subList(1, toSplit.size) | ||
.fold(mutableListOf(mutableListOf(toSplit[0]))) { acc: MutableList<MutableList<E>>, e -> | ||
if (predicate(e)) { | ||
acc.add(mutableListOf(e)) | ||
} else { | ||
acc.last().add(e) | ||
} | ||
acc | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.