Skip to content

feat(api): provide task guards for lazy register #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
}
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand All @@ -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<out Task>) {
taskProvider.configure { task ->
task.onlyIf {
getOrThrow(
task.project
).shouldInclude(task.project)
}
}
}

/**
* Call this method to determine if the project was affected in this change
*
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -399,9 +424,11 @@ class AffectedModuleDetectorImpl constructor(
changedProjects.contains(project.projectPath) -> {
ProjectSubset.CHANGED_PROJECTS
}

dependentProjects.contains(project.projectPath) -> {
ProjectSubset.DEPENDENT_PROJECTS
}

else -> {
ProjectSubset.NONE
}
Expand Down Expand Up @@ -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."
)
}
}
Expand Down Expand Up @@ -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,
Expand Down