Skip to content

Commit

Permalink
Task configuration avoidance
Browse files Browse the repository at this point in the history
Register the taskTree task lazily.
This might help slightly with preformance when
the taskTree task is NOT run.

resolves #35
  • Loading branch information
dorongold committed Jan 4, 2020
1 parent 0e7aa58 commit 691d4ae
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import org.gradle.util.GradleVersion
*/
class TaskTreePlugin implements Plugin<Project> {

private static boolean IS_GRADLE_MIN_49 = GradleVersion.current().compareTo(GradleVersion.version('4.9-rc-1')) >= 0
private static boolean IS_GRADLE_MIN_50 = GradleVersion.current().compareTo(GradleVersion.version('5.0-milestone-1')) >= 0

public static final String TASK_TREE_TASK_NAME = 'taskTree'
public static String GRADLE_MINIMUM_SUPPORTED_VERSION = '2.3'
public static String GRADLE_VERSION_5 = '5.0-milestone-1'
public static String UNSUPPORTED_GRADLE_VERSION_MESSAGE =
"The taskTree task (defined by the task-tree plugin) cannot be run on a gradle version older than ${GRADLE_MINIMUM_SUPPORTED_VERSION}"

Expand All @@ -25,10 +27,10 @@ class TaskTreePlugin implements Plugin<Project> {
// Skip if this sub-project already has our task. This can happen for example if the plugin is applied on allProjects.
return
}
if (GradleVersion.current() < GradleVersion.version(GRADLE_VERSION_5)) {
p.task(TASK_TREE_TASK_NAME, type: TaskTreeTaskOld)
if (IS_GRADLE_MIN_50) {
createTask(p, TaskTreeTaskNew, TASK_TREE_TASK_NAME)
} else {
p.task(TASK_TREE_TASK_NAME, type: TaskTreeTaskNew)
createTask(p, TaskTreeTaskOld, TASK_TREE_TASK_NAME)
}
p.gradle.taskGraph.whenReady {
if (project.gradle.taskGraph.allTasks.any { Task task -> task.class in TaskTreeTask }) {
Expand All @@ -49,4 +51,13 @@ class TaskTreePlugin implements Plugin<Project> {
}
}

private static def createTask(Project project, Class type, String name) {
if (IS_GRADLE_MIN_49) {
// Lazy - avoids task configuration if not run
return project.tasks.register(name, type)
} else {
return project.tasks.create(name, type)
}
}

}

0 comments on commit 691d4ae

Please sign in to comment.