GradleBuildRootsManager: check gradle version change in gradle-wrapper.properties

This commit is contained in:
Sergey Rostov
2020-06-14 19:03:32 +03:00
parent 463908f6f4
commit caa5aadc98
2 changed files with 41 additions and 17 deletions
@@ -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")
@@ -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<String>()
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)