Skip to content

Commit 0d7b7dc

Browse files
committed
feat(api): provide task guards for lazy register
This update provides a way to use the basic AffectedModuleDetector task guard when leveraging the `register` api to create tasks, without needing to explicitly get the task immediately. This allows for the AMD calculation to be evaluated lazily, and avoids performing work on tasks that will not be executed.
1 parent cef5e51 commit 0d7b7dc

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.gradle.api.Project
3232
import org.gradle.api.Task
3333
import org.gradle.api.UnknownDomainObjectException
3434
import org.gradle.api.logging.Logger
35+
import org.gradle.api.tasks.TaskProvider
3536
import java.io.File
3637

3738
/**
@@ -140,9 +141,11 @@ abstract class AffectedModuleDetector {
140141
rootProject.hasProperty(DEPENDENT_PROJECTS_ARG) -> {
141142
ProjectSubset.DEPENDENT_PROJECTS
142143
}
144+
143145
rootProject.hasProperty(CHANGED_PROJECTS_ARG) -> {
144146
ProjectSubset.CHANGED_PROJECTS
145147
}
148+
146149
else -> {
147150
ProjectSubset.ALL_AFFECTED_PROJECTS
148151
}
@@ -153,7 +156,7 @@ abstract class AffectedModuleDetector {
153156
rootProject.extensions.findByType(AffectedModuleConfiguration::class.java)
154157
) {
155158
"Root project ${rootProject.path} must have the AffectedModuleConfiguration " +
156-
"extension added."
159+
"extension added."
157160
}
158161

159162
val logger =
@@ -232,7 +235,7 @@ abstract class AffectedModuleDetector {
232235
}
233236

234237
/**
235-
* Call this method to configure the given task to execute only if the owner project
238+
* Call this method to configure the given provided task to execute only if the owner project
236239
* is affected by current changes
237240
*
238241
* Can be called during the configuration or execution phase
@@ -247,6 +250,24 @@ abstract class AffectedModuleDetector {
247250
}
248251
}
249252

253+
/**
254+
* Call this method to configure the given task to execute only if the owner project
255+
* is affected by current changes
256+
*
257+
* Can be called during the configuration or execution phase
258+
*/
259+
@Throws(GradleException::class)
260+
@JvmStatic
261+
fun configureTaskGuard(taskProvider: TaskProvider<out Task>) {
262+
taskProvider.configure { task ->
263+
task.onlyIf {
264+
getOrThrow(
265+
task.project
266+
).shouldInclude(task.project)
267+
}
268+
}
269+
}
270+
250271
/**
251272
* Call this method to determine if the project was affected in this change
252273
*
@@ -340,7 +361,11 @@ class AffectedModuleDetectorImpl constructor(
340361
injectedGitClient ?: GitClientImpl(
341362
rootProject.projectDir,
342363
logger,
343-
commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch, config.specifiedRawCommitSha),
364+
commitShaProvider = CommitShaProvider.fromString(
365+
config.compareFrom,
366+
config.specifiedBranch,
367+
config.specifiedRawCommitSha
368+
),
344369
ignoredFiles = config.ignoredFiles
345370
)
346371
}
@@ -399,9 +424,11 @@ class AffectedModuleDetectorImpl constructor(
399424
changedProjects.contains(project.projectPath) -> {
400425
ProjectSubset.CHANGED_PROJECTS
401426
}
427+
402428
dependentProjects.contains(project.projectPath) -> {
403429
ProjectSubset.DEPENDENT_PROJECTS
404430
}
431+
405432
else -> {
406433
ProjectSubset.NONE
407434
}
@@ -435,13 +462,13 @@ class AffectedModuleDetectorImpl constructor(
435462
unknownFiles.add(filePath)
436463
logger?.info(
437464
"Couldn't find containing project for file$filePath. " +
438-
"Adding to unknownFiles."
465+
"Adding to unknownFiles."
439466
)
440467
} else {
441468
changedProjects[containingProject.projectPath] = containingProject
442469
logger?.info(
443470
"For file $filePath containing project is $containingProject. " +
444-
"Adding to changedProjects."
471+
"Adding to changedProjects."
445472
)
446473
}
447474
}
@@ -487,7 +514,7 @@ class AffectedModuleDetectorImpl constructor(
487514
}
488515
logger?.info(
489516
"unknownFiles: $unknownFiles, changedProjects: $changedProjects, buildAll: " +
490-
"$buildAll"
517+
"$buildAll"
491518
)
492519

493520
// If we're in a buildAll state, we return allProjects unless it's the changed target,

0 commit comments

Comments
 (0)