Skip to content

Commit 1af62a6

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 1af62a6

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

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

Lines changed: 25 additions & 2 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
/**
@@ -232,7 +233,7 @@ abstract class AffectedModuleDetector {
232233
}
233234

234235
/**
235-
* Call this method to configure the given task to execute only if the owner project
236+
* Call this method to configure the given provided task to execute only if the owner project
236237
* is affected by current changes
237238
*
238239
* Can be called during the configuration or execution phase
@@ -247,6 +248,24 @@ abstract class AffectedModuleDetector {
247248
}
248249
}
249250

251+
/**
252+
* Call this method to configure the given task to execute only if the owner project
253+
* is affected by current changes
254+
*
255+
* Can be called during the configuration or execution phase
256+
*/
257+
@Throws(GradleException::class)
258+
@JvmStatic
259+
fun configureTaskGuard(taskProvider: TaskProvider<out Task>) {
260+
taskProvider.configure { task ->
261+
task.onlyIf {
262+
getOrThrow(
263+
task.project
264+
).shouldInclude(task.project)
265+
}
266+
}
267+
}
268+
250269
/**
251270
* Call this method to determine if the project was affected in this change
252271
*
@@ -340,7 +359,11 @@ class AffectedModuleDetectorImpl constructor(
340359
injectedGitClient ?: GitClientImpl(
341360
rootProject.projectDir,
342361
logger,
343-
commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch, config.specifiedRawCommitSha),
362+
commitShaProvider = CommitShaProvider.fromString(
363+
config.compareFrom,
364+
config.specifiedBranch,
365+
config.specifiedRawCommitSha
366+
),
344367
ignoredFiles = config.ignoredFiles
345368
)
346369
}

0 commit comments

Comments
 (0)