KotlinDslModels: project can be null in ExternalSystemTaskNotificationListener.onStart

This commit is contained in:
Sergey Rostov
2020-05-22 18:19:11 +03:00
parent 44f6d490b6
commit 06dbbe2452
2 changed files with 45 additions and 32 deletions
@@ -21,11 +21,6 @@ import org.jetbrains.plugins.gradle.service.project.ProjectResolverContext
import java.io.File
import java.util.concurrent.ConcurrentHashMap
var Project.kotlinGradleDslSync: MutableMap<ExternalSystemTaskId, KotlinDslGradleBuildSync>
by NotNullableUserDataProperty<Project, MutableMap<ExternalSystemTaskId, KotlinDslGradleBuildSync>>(
Key("Kotlin DSL Scripts Models"), ConcurrentHashMap()
)
fun processScriptModel(
resolverCtx: ProjectResolverContext,
model: KotlinDslScriptsModel,
@@ -40,7 +35,13 @@ fun processScriptModel(
val project = task.findProject() ?: return
val models = model.toListOfScriptModels(project)
project.kotlinGradleDslSync[task]?.models?.addAll(models)
val tasks = KotlinDslSyncListener.instance.tasks
val sync = synchronized(tasks) { tasks[task] }
if (sync != null) {
synchronized(sync) {
sync.models.addAll(models)
}
}
if (models.containsErrors()) {
throw IllegalStateException(KotlinIdeaGradleBundle.message("title.kotlin.build.script"))
@@ -106,14 +107,16 @@ class KotlinDslGradleBuildSync(val workingDir: String, val taskId: ExternalSyste
}
fun saveScriptModels(project: Project, build: KotlinDslGradleBuildSync) {
val errorReporter = KotlinGradleDslErrorReporter(project, build.taskId)
synchronized(build) {
val errorReporter = KotlinGradleDslErrorReporter(project, build.taskId)
build.models.forEach { model ->
errorReporter.reportError(File(model.file), model)
build.models.forEach { model ->
errorReporter.reportError(File(model.file), model)
}
// todo: use real info about projects
build.projectRoots.addAll(build.models.map { toSystemIndependentName(File(it.file).parent) })
GradleBuildRootsManager.getInstance(project).update(build)
}
// todo: use real info about projects
build.projectRoots.addAll(build.models.map { toSystemIndependentName(File(it.file).parent) })
GradleBuildRootsManager.getInstance(project).update(build)
}
@@ -6,53 +6,63 @@
package org.jetbrains.kotlin.idea.scripting.gradle.importing
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType
import org.jetbrains.kotlin.idea.framework.GRADLE_SYSTEM_ID
import org.jetbrains.kotlin.idea.scripting.gradle.roots.GradleBuildRootsManager
import java.lang.Exception
import java.util.*
class KotlinDslSyncListener : ExternalSystemTaskNotificationListenerAdapter() {
companion object {
val instance: KotlinDslSyncListener
get() = ExternalSystemTaskNotificationListener.EP_NAME
.extensionList.filterIsInstance<KotlinDslSyncListener>().single()
}
val tasks = WeakHashMap<ExternalSystemTaskId, KotlinDslGradleBuildSync>()
override fun onStart(id: ExternalSystemTaskId, workingDir: String?) {
if (id.type != ExternalSystemTaskType.RESOLVE_PROJECT) return
if (id.projectSystemId != GRADLE_SYSTEM_ID) return
if (!isGradleProjectResolve(id)) return
if (workingDir == null) return
val project = id.findProject() ?: return
synchronized(tasks) { tasks[id] = KotlinDslGradleBuildSync(workingDir, id) }
// project may be null in case of new project
val project = id.findProject() ?: return
GradleBuildRootsManager.getInstance(project).markImportingInProgress(workingDir)
project.kotlinGradleDslSync[id] = KotlinDslGradleBuildSync(workingDir, id)
}
override fun onEnd(id: ExternalSystemTaskId) {
if (id.type != ExternalSystemTaskType.RESOLVE_PROJECT) return
if (id.projectSystemId != GRADLE_SYSTEM_ID) return
if (!isGradleProjectResolve(id)) return
val sync = synchronized(tasks) { tasks.remove(id) } ?: return
// project may be null in case of new project
val project = id.findProject() ?: return
val sync = project.kotlinGradleDslSync.remove(id) ?: return
saveScriptModels(project, sync)
}
override fun onFailure(id: ExternalSystemTaskId, e: Exception) {
if (id.type != ExternalSystemTaskType.RESOLVE_PROJECT) return
if (id.projectSystemId != GRADLE_SYSTEM_ID) return
val project = id.findProject() ?: return
val sync = project.kotlinGradleDslSync[id] ?: return
if (!isGradleProjectResolve(id)) return
val sync = synchronized(tasks) { tasks[id] } ?: return
sync.failed = true
}
override fun onCancel(id: ExternalSystemTaskId) {
if (id.type != ExternalSystemTaskType.RESOLVE_PROJECT) return
if (id.projectSystemId != GRADLE_SYSTEM_ID) return
if (!isGradleProjectResolve(id)) return
val cancelled = synchronized(tasks) { tasks.remove(id) }
// project may be null in case of new project
val project = id.findProject() ?: return
val cancelled = project.kotlinGradleDslSync.remove(id)
if (cancelled != null) {
GradleBuildRootsManager.getInstance(project).markImportingInProgress(cancelled.workingDir, false)
}
}
private fun isGradleProjectResolve(id: ExternalSystemTaskId) =
id.type == ExternalSystemTaskType.RESOLVE_PROJECT &&
id.projectSystemId == GRADLE_SYSTEM_ID
}