From 05639721833ec1c8bb3eb8d7b52f04ae49d21a11 Mon Sep 17 00:00:00 2001 From: Josh Feinberg Date: Wed, 12 Oct 2022 11:01:54 -0500 Subject: [PATCH] Switch registering tasks to lazy --- .../AffectedModuleDetectorPlugin.kt | 42 +++++++++---------- .../AffectedModuleDetectorPluginTest.kt | 1 + 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt index 86512fb1..5171d74e 100644 --- a/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt +++ b/affectedmoduledetector/src/main/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPlugin.kt @@ -93,14 +93,15 @@ class AffectedModuleDetectorPlugin : Plugin { customTasks: Set ) { customTasks.forEach { taskType -> - val task = rootProject.tasks.register(taskType.commandByImpact).get() - task.group = CUSTOM_TASK_GROUP_NAME - task.description = taskType.taskDescription - disableConfigCache(task) - - rootProject.subprojects { project -> - pluginIds.forEach { pluginId -> - withPlugin(pluginId, task, taskType, project) + rootProject.tasks.register(taskType.commandByImpact) { task -> + task.group = CUSTOM_TASK_GROUP_NAME + task.description = taskType.taskDescription + disableConfigCache(task) + + rootProject.subprojects { project -> + pluginIds.forEach { pluginId -> + withPlugin(pluginId, task, taskType, project) + } } } } @@ -134,20 +135,19 @@ class AffectedModuleDetectorPlugin : Plugin { taskType: AffectedModuleTaskType, groupName: String ) { - val task = rootProject.tasks.register(taskType.commandByImpact).get() - task.group = groupName - task.description = taskType.taskDescription - disableConfigCache(task) + rootProject.tasks.register(taskType.commandByImpact) { task -> + task.group = groupName + task.description = taskType.taskDescription + disableConfigCache(task) - rootProject.subprojects { project -> - project.afterEvaluate { evaluatedProject -> + rootProject.subprojects { project -> pluginIds.forEach { pluginId -> if (pluginId == PLUGIN_JAVA_LIBRARY || pluginId == PLUGIN_KOTLIN) { if (taskType == InternalTaskType.ANDROID_JVM_TEST) { - withPlugin(pluginId, task, InternalTaskType.JVM_TEST, evaluatedProject) + withPlugin(pluginId, task, InternalTaskType.JVM_TEST, project) } } else { - withPlugin(pluginId, task, taskType, evaluatedProject) + withPlugin(pluginId, task, taskType, project) } } } @@ -172,12 +172,10 @@ class AffectedModuleDetectorPlugin : Plugin { task.dependsOn(path) } - project.afterEvaluate { - project.tasks.findByPath(path)?.onlyIf { task -> - when { - !AffectedModuleDetector.isProjectEnabled(task.project) -> true - else -> AffectedModuleDetector.isProjectAffected(task.project) - } + project.tasks.findByPath(path)?.onlyIf { task -> + when { + !AffectedModuleDetector.isProjectEnabled(task.project) -> true + else -> AffectedModuleDetector.isProjectAffected(task.project) } } } diff --git a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPluginTest.kt b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPluginTest.kt index 49807e50..ff1dd737 100644 --- a/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPluginTest.kt +++ b/affectedmoduledetector/src/test/kotlin/com/dropbox/affectedmoduledetector/AffectedModuleDetectorPluginTest.kt @@ -82,6 +82,7 @@ class AffectedModuleDetectorPluginTest { // GIVEN val task = fakeTask val plugin = AffectedModuleDetectorPlugin() + rootProject.pluginManager.apply(AffectedModuleDetectorPlugin::class.java) // WHEN plugin.registerInternalTask(