From 0d7b7dc1591d766920fe8fa658ce88cca7e6e557 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 | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 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..86c0c18 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 /** @@ -140,9 +141,11 @@ abstract class AffectedModuleDetector { rootProject.hasProperty(DEPENDENT_PROJECTS_ARG) -> { ProjectSubset.DEPENDENT_PROJECTS } + rootProject.hasProperty(CHANGED_PROJECTS_ARG) -> { ProjectSubset.CHANGED_PROJECTS } + else -> { ProjectSubset.ALL_AFFECTED_PROJECTS } @@ -153,7 +156,7 @@ abstract class AffectedModuleDetector { rootProject.extensions.findByType(AffectedModuleConfiguration::class.java) ) { "Root project ${rootProject.path} must have the AffectedModuleConfiguration " + - "extension added." + "extension added." } val logger = @@ -232,7 +235,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 +250,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 +361,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 ) } @@ -399,9 +424,11 @@ class AffectedModuleDetectorImpl constructor( changedProjects.contains(project.projectPath) -> { ProjectSubset.CHANGED_PROJECTS } + dependentProjects.contains(project.projectPath) -> { ProjectSubset.DEPENDENT_PROJECTS } + else -> { ProjectSubset.NONE } @@ -435,13 +462,13 @@ class AffectedModuleDetectorImpl constructor( unknownFiles.add(filePath) logger?.info( "Couldn't find containing project for file$filePath. " + - "Adding to unknownFiles." + "Adding to unknownFiles." ) } else { changedProjects[containingProject.projectPath] = containingProject logger?.info( "For file $filePath containing project is $containingProject. " + - "Adding to changedProjects." + "Adding to changedProjects." ) } } @@ -487,7 +514,7 @@ class AffectedModuleDetectorImpl constructor( } logger?.info( "unknownFiles: $unknownFiles, changedProjects: $changedProjects, buildAll: " + - "$buildAll" + "$buildAll" ) // If we're in a buildAll state, we return allProjects unless it's the changed target,