From 1af62a6c8cac7f6d7ba9d0b14c3be57cbc4dfd78 Mon Sep 17 00:00:00 2001 From: Lucas Shadler Date: Wed, 12 Mar 2025 19:49:44 -0700 Subject: [PATCH] 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. --- .../AffectedModuleDetector.kt | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt index 84f855d..3690ce4 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetector.kt @@ -32,6 +32,7 @@ import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.UnknownDomainObjectException import org.gradle.api.logging.Logger +import org.gradle.api.tasks.TaskProvider import java.io.File /** @@ -232,7 +233,7 @@ abstract class AffectedModuleDetector { } /** - * Call this method to configure the given task to execute only if the owner project + * Call this method to configure the given provided task to execute only if the owner project * is affected by current changes * * Can be called during the configuration or execution phase @@ -247,6 +248,24 @@ abstract class AffectedModuleDetector { } } + /** + * Call this method to configure the given task to execute only if the owner project + * is affected by current changes + * + * Can be called during the configuration or execution phase + */ + @Throws(GradleException::class) + @JvmStatic + fun configureTaskGuard(taskProvider: TaskProvider) { + taskProvider.configure { task -> + task.onlyIf { + getOrThrow( + task.project + ).shouldInclude(task.project) + } + } + } + /** * Call this method to determine if the project was affected in this change * @@ -340,7 +359,11 @@ class AffectedModuleDetectorImpl constructor( injectedGitClient ?: GitClientImpl( rootProject.projectDir, logger, - commitShaProvider = CommitShaProvider.fromString(config.compareFrom, config.specifiedBranch, config.specifiedRawCommitSha), + commitShaProvider = CommitShaProvider.fromString( + config.compareFrom, + config.specifiedBranch, + config.specifiedRawCommitSha + ), ignoredFiles = config.ignoredFiles ) }