From caa5aadc981049fc0e7bd6b850962ca6e8e468da Mon Sep 17 00:00:00 2001 From: Sergey Rostov Date: Sun, 14 Jun 2020 19:03:32 +0300 Subject: [PATCH] GradleBuildRootsManager: check gradle version change in gradle-wrapper.properties --- .../gradle/roots/GradleBuildRootsLocator.kt | 20 ++++++---- .../gradle/roots/GradleBuildRootsManager.kt | 38 ++++++++++++++----- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsLocator.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsLocator.kt index 66de9b60f83..2f712437cd4 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsLocator.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsLocator.kt @@ -28,10 +28,12 @@ abstract class GradleBuildRootsLocator { private val VirtualFile.localPath get() = path + private val gradleWrapperEnding = "/gradle/wrapper/gradle-wrapper.properties" + fun maybeAffectedGradleProjectFile(filePath: String): Boolean = filePath.endsWith("/gradle.properties") || filePath.endsWith("/gradle.local") || - filePath.endsWith("/gradle-wrapper.properties") || + filePath.endsWith(gradleWrapperEnding) || filePath.endsWith(".gradle.kts") fun isAffectedGradleProjectFile(filePath: String): Boolean = @@ -43,14 +45,18 @@ abstract class GradleBuildRootsLocator { filePath.endsWith("/gradle.local") ) { return roots.getBuildByProjectDir(filePath.substringBeforeLast("/")) - } else if (filePath.endsWith("/gradle-wrapper.properties")) { - val gradleWrapperDirIndex = filePath.lastIndexOfOrNull('/') ?: return null - val gradleDirIndex = filePath.lastIndexOfOrNull('/', gradleWrapperDirIndex - 1) ?: return null - val buildDirIndex = filePath.lastIndexOfOrNull('/', gradleDirIndex - 1) ?: return null - return roots.getBuildByRootDir(filePath.substring(0, buildDirIndex)) } - return findScriptBuildRoot(filePath, searchNearestLegacy = false)?.root as? GradleBuildRoot + return findGradleWrapperPropertiesBuildDir(filePath)?.let { roots.getBuildByRootDir(it) } + ?: findScriptBuildRoot(filePath, searchNearestLegacy = false)?.root + } + + fun findGradleWrapperPropertiesBuildDir(filePath: String): String? { + if (filePath.endsWith(gradleWrapperEnding)) { + return filePath.substring(0, filePath.length - gradleWrapperEnding.length) + } + + return null } @Suppress("EnumEntryName") diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsManager.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsManager.kt index 11744f90c36..5f1a68c924f 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsManager.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/scripting/gradle/roots/GradleBuildRootsManager.kt @@ -36,6 +36,7 @@ import java.nio.file.FileSystems import java.nio.file.Files import java.nio.file.Paths import java.nio.file.attribute.BasicFileAttributes +import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.atomic.AtomicBoolean /** @@ -110,7 +111,7 @@ class GradleBuildRootsManager(val project: Project) : GradleBuildRootsLocator(), fun fileChanged(filePath: String, ts: Long = System.currentTimeMillis()) { findAffectedFileRoot(filePath)?.fileChanged(filePath, ts) - scheduleLastModifiedFilesSave() + scheduleModifiedFilesCheck(filePath) } fun markImportingInProgress(workingDir: String, inProgress: Boolean = true) { @@ -156,22 +157,39 @@ class GradleBuildRootsManager(val project: Project) : GradleBuildRootsLocator(), return GradleBuildRootData(new.importTs, roots, new.templateClasspath, models.values) } - private val lastModifiedFilesSaveScheduled = AtomicBoolean() + private val modifiedFilesCheckScheduled = AtomicBoolean() + private val modifiedFiles = ConcurrentLinkedQueue() - fun scheduleLastModifiedFilesSave() { - if (lastModifiedFilesSaveScheduled.compareAndSet(false, true)) { + fun scheduleModifiedFilesCheck(filePath: String) { + modifiedFiles.add(filePath) + if (modifiedFilesCheckScheduled.compareAndSet(false, true)) { BackgroundTaskUtil.executeOnPooledThread(project) { - if (lastModifiedFilesSaveScheduled.compareAndSet(true, false)) { - updateNotifications(restartAnalyzer = false) { true } - - roots.list.forEach { - it.saveLastModifiedFiles() - } + if (modifiedFilesCheckScheduled.compareAndSet(true, false)) { + checkModifiedFiles() } } } } + private fun checkModifiedFiles() { + updateNotifications(restartAnalyzer = false) { true } + + roots.list.forEach { + it.saveLastModifiedFiles() + } + + // process modifiedFiles queue + while (true) { + val file = modifiedFiles.poll() ?: break + + // detect gradle version change + val buildDir = findGradleWrapperPropertiesBuildDir(file) + if (buildDir != null) { + actualizeBuildRoot(buildDir) + } + } + } + fun updateStandaloneScripts(update: StandaloneScriptsUpdater.() -> Unit) { val changes = StandaloneScriptsUpdater.collectChanges(delegate = roots, update)