[maven][external-systems][IDEA-349994][IDEA-349993] Refactor maven and external system recovery actions.

Merged into a single action.

GitOrigin-RevId: ac86aee443c3d32ad6a7450a5e72189c8fd0691b
This commit is contained in:
Alexander Bubenchikov
2024-06-07 17:05:29 +02:00
committed by intellij-monorepo-bot
parent 3849073765
commit 97a9366c5f
10 changed files with 157 additions and 92 deletions

View File

@@ -94,5 +94,8 @@
<extensionPoint qualifiedName="com.intellij.openapi.externalSystem.autoimport.autoReloadTypeProviderExtension"
interface="com.intellij.openapi.externalSystem.autoimport.DefaultAutoReloadTypeProvider"
dynamic="true" />
<extensionPoint qualifiedName="com.intellij.externalSystemRecoveryContributor"
interface="com.intellij.openapi.externalSystem.service.project.manage.ExternalSystemRecoveryContributor$Factory"
dynamic="true" />
</extensionPoints>
</idea-plugin>

View File

@@ -140,7 +140,9 @@
<trustedProjectsLocator implementation="com.intellij.openapi.externalSystem.service.project.trusted.ExternalSystemTrustedProjectsLocator"/>
<recoveryAction implementation="com.intellij.openapi.externalSystem.service.project.manage.ExternalProjectsDataRecoveryAction"/>
<recoveryAction implementation="com.intellij.openapi.externalSystem.service.project.manage.ExternalSystemRecoveryAction"/>
<externalSystemRecoveryContributor implementation="com.intellij.openapi.externalSystem.service.project.manage.ExternalProjectDataRecoveryContributor$Factory"/>
</extensions>
<applicationListeners>
<listener class="com.intellij.openapi.externalSystem.autoimport.ProjectRefreshFloatingProvider$Listener"

View File

@@ -1,53 +1,37 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.externalSystem.service.project.manage
import com.intellij.ide.actions.cache.AsyncRecoveryResult
import com.intellij.ide.actions.cache.RecoveryScope
import com.intellij.ide.actions.cache.ReopenProjectRecoveryAction
import com.intellij.openapi.externalSystem.settings.workspaceModel.ExternalProjectsBuildClasspathEntity
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil
import com.intellij.openapi.externalSystem.util.ExternalSystemBundle
import com.intellij.openapi.externalSystem.util.ExternalSystemUtil
import com.intellij.openapi.progress.blockingContext
import com.intellij.openapi.progress.blockingContextScope
import com.intellij.openapi.util.io.NioFiles
import com.intellij.platform.backend.workspace.workspaceModel
import com.intellij.platform.workspace.storage.entities
import com.intellij.workspaceModel.ide.impl.WorkspaceModelCacheImpl
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.ApiStatus
import java.nio.file.Path
@ApiStatus.Internal
class ExternalProjectsDataRecoveryAction : ReopenProjectRecoveryAction() {
class ExternalProjectDataRecoveryContributor : ExternalSystemRecoveryContributor {
override val performanceRate: Int
get() = 0
private lateinit var myExternalSystemCache: Path
override val presentableName: String
get() = ExternalSystemBundle.message("action.ExternalSystem.RecoveryAction.name")
override val actionKey: String
get() = "ExternalSystem.RecoveryAction"
override suspend fun performAsync(recoveryScope: RecoveryScope): AsyncRecoveryResult {
override suspend fun beforeClose(recoveryScope: RecoveryScope) {
invalidateLocalExternalSystemCache(recoveryScope)
invalidateProjectBuildClasspathCache(recoveryScope)
invalidateExternalSystemDataStorage(recoveryScope)
invalidateExternalSystemToolwindow(recoveryScope)
WorkspaceModelCacheImpl.invalidateCaches()
val externalSystemCache = ExternalProjectsDataStorage.getProjectConfigurationDir(recoveryScope.project)
val projectPath = closeProject(recoveryScope)
myExternalSystemCache = ExternalProjectsDataStorage.getProjectConfigurationDir(recoveryScope.project)
}
override suspend fun afterClose() {
withContext(Dispatchers.IO) {
NioFiles.deleteRecursively(externalSystemCache)
NioFiles.deleteRecursively(myExternalSystemCache)
}
val newRecoveryScope = openProject(projectPath)
return AsyncRecoveryResult(newRecoveryScope, emptyList())
}
private suspend fun invalidateLocalExternalSystemCache(recoveryScope: RecoveryScope) {
@@ -91,4 +75,10 @@ class ExternalProjectsDataRecoveryAction : ReopenProjectRecoveryAction() {
}
}
}
class Factory : ExternalSystemRecoveryContributor.Factory {
override fun createContributor(): ExternalSystemRecoveryContributor {
return ExternalProjectDataRecoveryContributor()
}
}
}

View File

@@ -0,0 +1,51 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.externalSystem.service.project.manage
import com.intellij.ide.actions.cache.AsyncRecoveryResult
import com.intellij.ide.actions.cache.RecoveryScope
import com.intellij.ide.actions.cache.ReopenProjectRecoveryAction
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.externalSystem.util.ExternalSystemBundle
import com.intellij.util.containers.forEachLoggingErrors
import com.intellij.workspaceModel.ide.impl.WorkspaceModelCacheImpl
import org.jetbrains.annotations.ApiStatus
@ApiStatus.Internal
class ExternalSystemRecoveryAction : ReopenProjectRecoveryAction() {
override val performanceRate: Int
get() = 0
override val presentableName: String
get() = ExternalSystemBundle.message("action.ExternalSystem.RecoveryAction.name")
override val actionKey: String
get() = "ExternalSystem.RecoveryAction"
override suspend fun performAsync(recoveryScope: RecoveryScope): AsyncRecoveryResult {
val contributors = ArrayList<ExternalSystemRecoveryContributor>()
ExternalSystemRecoveryContributor.EP_NAME.forEachExtensionSafe {
contributors.add(it.createContributor())
}
contributors.forEachLoggingErrors(thisLogger()) {
it.beforeClose(recoveryScope)
}
WorkspaceModelCacheImpl.invalidateCaches()
val projectPath = closeProject(recoveryScope)
contributors.forEachLoggingErrors(thisLogger()) {
it.afterClose()
}
val newRecoveryScope = openProject(projectPath)
contributors.forEachLoggingErrors(thisLogger()) {
it.afterOpen(newRecoveryScope)
}
return AsyncRecoveryResult(newRecoveryScope, emptyList())
}
}

View File

@@ -0,0 +1,24 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.externalSystem.service.project.manage
import com.intellij.ide.actions.cache.RecoveryScope
import com.intellij.openapi.extensions.ExtensionPointName
import org.jetbrains.annotations.ApiStatus
@ApiStatus.Internal
interface ExternalSystemRecoveryContributor {
suspend fun beforeClose(recoveryScope: RecoveryScope) = Unit
suspend fun afterClose() = Unit
suspend fun afterOpen(recoveryScope: RecoveryScope) = Unit
interface Factory {
fun createContributor(): ExternalSystemRecoveryContributor;
}
companion object {
val EP_NAME: ExtensionPointName<Factory> = ExtensionPointName("com.intellij.externalSystemRecoveryContributor")
}
}

View File

@@ -352,7 +352,12 @@ public abstract class MavenProjectsManager extends MavenSimpleProjectComponent
@ApiStatus.Internal
public Path getProjectsTreeFile() {
return getProjectsTreesDir().resolve(myProject.getLocationHash()).resolve("tree.dat");
return getProjectCacheDir().resolve("tree.dat");
}
@ApiStatus.Internal
public Path getProjectCacheDir() {
return getProjectsTreesDir().resolve(myProject.getLocationHash());
}
@NotNull

View File

@@ -1,63 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.idea.maven.project.actions
import com.intellij.ide.actions.cache.AsyncRecoveryResult
import com.intellij.ide.actions.cache.RecoveryScope
import com.intellij.ide.actions.cache.ReopenProjectRecoveryAction
import com.intellij.openapi.util.io.NioFiles
import com.intellij.platform.ide.progress.withBackgroundProgress
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.idea.maven.project.MavenProjectBundle
import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.idea.maven.utils.MavenUtil
import kotlin.io.path.exists
class MavenProjectRecoveryAction : ReopenProjectRecoveryAction() {
override fun canBeApplied(recoveryScope: RecoveryScope): Boolean {
if (!super.canBeApplied(recoveryScope)) return false
val project = recoveryScope.project
val manager = MavenProjectsManager.getInstance(project)
if (manager.isMavenizedProject) return true
return manager.projectsTreeFile.exists()
}
override suspend fun performAsync(recoveryScope: RecoveryScope): AsyncRecoveryResult {
val project = recoveryScope.project
val manager = MavenProjectsManager.getInstance(project)
val path = manager.projectsTreeFile
withContext(Dispatchers.IO) {
withBackgroundProgress(project, MavenProjectBundle.message("maven.project.clean.restart.connectors"), false) {
MavenUtil.restartMavenConnectors(project, true)
}
}
val projectPath = closeProject(recoveryScope)
withContext(Dispatchers.IO) {
withBackgroundProgress(project, MavenProjectBundle.message("maven.project.clean.delete.project.structure.caches"), false) {
NioFiles.deleteRecursively(path.parent)
}
}
val newRecoveryScope = openProject(projectPath)
val newManager = MavenProjectsManager.getInstance(newRecoveryScope.project)
withContext(Dispatchers.IO) {
newManager.forceUpdateAllProjectsOrFindAllAvailablePomFiles()
}
return AsyncRecoveryResult(newRecoveryScope, emptyList())
}
override val performanceRate: Int
get() = 0
override val presentableName: String
get() = MavenProjectBundle.message("maven.project.clean.caches")
override val actionKey: String
get() = "invalidate-maven"
}

View File

@@ -0,0 +1,55 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.idea.maven.project.actions
import com.intellij.ide.actions.cache.RecoveryScope
import com.intellij.openapi.externalSystem.service.project.manage.ExternalSystemRecoveryContributor
import com.intellij.openapi.util.io.NioFiles
import com.intellij.platform.ide.progress.withBackgroundProgress
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.idea.maven.buildtool.MavenSyncSpec
import org.jetbrains.idea.maven.project.MavenProjectBundle
import org.jetbrains.idea.maven.project.MavenProjectsManager
import org.jetbrains.idea.maven.project.MavenProjectsManagerEx
import org.jetbrains.idea.maven.utils.MavenUtil
import java.nio.file.Path
@ApiStatus.Internal
class MavenProjectRecoveryContributor : ExternalSystemRecoveryContributor {
private lateinit var myProjectCacheDir: Path
override suspend fun beforeClose(recoveryScope: RecoveryScope) {
val project = recoveryScope.project
val manager = MavenProjectsManager.getInstance(project)
myProjectCacheDir = manager.projectCacheDir
withContext(Dispatchers.IO) {
withBackgroundProgress(project, MavenProjectBundle.message("maven.project.clean.restart.connectors"), false) {
MavenUtil.restartMavenConnectors(project, true)
}
}
}
override suspend fun afterClose() {
withContext(Dispatchers.IO) {
NioFiles.deleteRecursively(myProjectCacheDir)
}
}
override suspend fun afterOpen(recoveryScope: RecoveryScope) {
val newManager = MavenProjectsManager.getInstance(recoveryScope.project) as MavenProjectsManagerEx
withContext(Dispatchers.IO) {
newManager.updateAllMavenProjects(MavenSyncSpec.full("MavenProjectRecoveryContributor"))
}
}
class Factory : ExternalSystemRecoveryContributor.Factory {
override fun createContributor(): ExternalSystemRecoveryContributor {
return MavenProjectRecoveryContributor()
}
}
}

View File

@@ -161,7 +161,7 @@
<languageInjector implementation="org.jetbrains.idea.maven.plugins.api.MavenPluginConfigurationLanguageInjector"/>
<selectInTarget implementation="org.jetbrains.idea.maven.navigator.SelectInMavenNavigatorTarget"/>
<recoveryAction implementation="org.jetbrains.idea.maven.project.actions.MavenProjectRecoveryAction"/>
<externalSystemRecoveryContributor implementation="org.jetbrains.idea.maven.project.actions.MavenProjectRecoveryContributor$Factory"/>
<fileIconProvider implementation="org.jetbrains.idea.maven.utils.MavenIconProvider"/>
<editorTabTitleProvider implementation="org.jetbrains.idea.maven.utils.MavenEditorTabTitleProvider"/>

View File

@@ -293,6 +293,4 @@ wsl.jdk.searching=Searching for existing jdks...
wsl.jdk.downloading=Downloading jdk...
notification.group.maven=Maven
maven.project.clean.caches=Invalidate Maven internal caches and Re-Sync
maven.project.clean.restart.connectors=Restarting maven connectors
maven.project.clean.delete.project.structure.caches=Cleaning maven project tree cache