[Workspace Model] Partially unbind module loading form builder

GitOrigin-RevId: 2054a80f319658f690d5882223c968387f586509
This commit is contained in:
Alex Plate
2023-01-06 18:08:19 +02:00
committed by intellij-monorepo-bot
parent 429e8139d6
commit 64f7a58112
6 changed files with 44 additions and 42 deletions

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspaceModel.ide.impl.jps.serialization
import com.intellij.workspaceModel.storage.MutableEntityStorage
import com.intellij.workspaceModel.storage.bridgeEntities.ModuleEntity
import com.intellij.workspaceModel.storage.url.VirtualFileUrl
import com.intellij.workspaceModel.storage.url.VirtualFileUrlManager
@@ -12,8 +11,10 @@ import org.jetbrains.annotations.ApiStatus
*/
@ApiStatus.Internal
interface CustomModuleComponentSerializer {
fun loadComponent(builder: MutableEntityStorage,
moduleEntity: ModuleEntity,
/**
* [detachedModuleEntity] - module entity that is not added to the builder. You can change it by casting to builder and modify properties
*/
fun loadComponent(detachedModuleEntity: ModuleEntity,
reader: JpsFileContentReader,
imlFileUrl: VirtualFileUrl,
errorReporter: ErrorReporter,

View File

@@ -12,13 +12,11 @@ import com.intellij.workspaceModel.storage.MutableEntityStorage
import com.intellij.workspaceModel.storage.bridgeEntities.ExternalSystemModuleOptionsEntity
import com.intellij.workspaceModel.storage.bridgeEntities.ModuleCustomImlDataEntity
import com.intellij.workspaceModel.storage.bridgeEntities.ModuleEntity
import com.intellij.workspaceModel.storage.bridgeEntities.getOrCreateExternalSystemModuleOptions
import com.intellij.workspaceModel.storage.url.VirtualFileUrl
import com.intellij.workspaceModel.storage.url.VirtualFileUrlManager
import org.jdom.Element
import org.jetbrains.jps.model.serialization.JDomSerializationUtil
import org.jetbrains.jps.util.JpsPathUtil
import com.intellij.workspaceModel.storage.bridgeEntities.modifyEntity
private val MODULE_OPTIONS_TO_CHECK = setOf(
"externalSystemModuleVersion", "linkedProjectPath", "linkedProjectId", "rootProjectPath", "externalSystemModuleGroup",
@@ -51,15 +49,14 @@ internal class ExternalModuleImlFileEntitiesSerializer(modulePath: ModulePath,
return Pair(options, options["externalSystem"])
}
override fun loadExternalSystemOptions(builder: MutableEntityStorage,
module: ModuleEntity,
override fun loadExternalSystemOptions(module: ModuleEntity,
reader: JpsFileContentReader,
externalSystemOptions: Map<String?, String?>,
externalSystemId: String?,
entitySource: EntitySource) {
if (!shouldCreateExternalSystemModuleOptions(externalSystemId, externalSystemOptions, MODULE_OPTIONS_TO_CHECK)) return
val optionsEntity = builder.getOrCreateExternalSystemModuleOptions(module, entitySource)
builder.modifyEntity(optionsEntity) {
ExternalSystemModuleOptionsEntity(entitySource) {
this.module = module
externalSystem = externalSystemId
externalSystemModuleVersion = externalSystemOptions["externalSystemModuleVersion"]
linkedProjectPath = externalSystemOptions["linkedProjectPath"]

View File

@@ -121,7 +121,8 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
customRootsSerializer = moduleOptions[JpsProjectLoader.CLASSPATH_ATTRIBUTE]?.let { customSerializerId ->
val serializer = CustomModuleRootsSerializer.EP_NAME.extensionList.firstOrNull { it.id == customSerializerId }
if (serializer == null) {
errorReporter.reportError(ProjectModelBundle.message("error.message.unknown.classpath.provider", fileUrl.fileName, customSerializerId), fileUrl)
errorReporter.reportError(
ProjectModelBundle.message("error.message.unknown.classpath.provider", fileUrl.fileName, customSerializerId), fileUrl)
}
return@let serializer
}
@@ -129,7 +130,7 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
customDir = moduleOptions[JpsProjectLoader.CLASSPATH_DIR_ATTRIBUTE]
val externalSystemEntitySource = createEntitySource(externalSystemId)
val moduleEntitySource = customRootsSerializer?.createEntitySource(fileUrl, internalEntitySource, customDir, virtualFileManager)
?: externalSystemEntitySource
?: externalSystemEntitySource
if (moduleEntitySource is DummyParentEntitySource) {
Pair(moduleEntitySource, externalSystemEntitySource)
}
@@ -138,37 +139,44 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
}
}
catch (e: JDOMException) {
builder.addModuleEntity(modulePath.moduleName, listOf(ModuleDependencyItem.ModuleSourceDependency), internalEntitySource)
// TODO: Handle loading with exceptions
//builder.addModuleEntity(modulePath.moduleName, listOf(ModuleDependencyItem.ModuleSourceDependency), internalEntitySource)
throw e
}
val moduleEntity = builder.addModuleEntity(modulePath.moduleName, listOf(ModuleDependencyItem.ModuleSourceDependency),
entitySourceForModuleAndOtherEntities.first)
val moduleEntity = ModuleEntity(modulePath.moduleName, listOf(ModuleDependencyItem.ModuleSourceDependency),
entitySourceForModuleAndOtherEntities.first)
val entitySource = entitySourceForModuleAndOtherEntities.second
val moduleGroup = modulePath.group
if (moduleGroup != null) {
builder.addModuleGroupPathEntity(moduleGroup.split('/'), moduleEntity, entitySource)
ModuleGroupPathEntity(moduleGroup.split('/'), entitySource) {
this.module = moduleEntity
}
}
val moduleType = moduleOptions["type"]
if (moduleType != null) {
builder.modifyEntity(moduleEntity) {
type = moduleType
}
(moduleEntity as ModuleEntity.Builder).type = moduleType
}
@Suppress("UNCHECKED_CAST")
val customModuleOptions =
moduleOptions.filter { (key, value) -> key != null && value != null && key !in STANDARD_MODULE_OPTIONS } as Map<String, String>
if (customModuleOptions.isNotEmpty()) {
builder.addModuleCustomImlDataEntity(null, customModuleOptions, moduleEntity, entitySource)
ModuleCustomImlDataEntity(customModuleOptions, entitySource) {
this.rootManagerTagCustomData = null
this.module = moduleEntity
}
}
CUSTOM_MODULE_COMPONENT_SERIALIZER_EP.extensionList.forEach {
it.loadComponent(builder, moduleEntity, reader, fileUrl, errorReporter, virtualFileManager)
it.loadComponent(moduleEntity, reader, fileUrl, errorReporter, virtualFileManager)
}
// Don't forget to load external system options even if custom root serializer exist
loadExternalSystemOptions(builder, moduleEntity, reader, externalSystemOptions, externalSystemId, entitySource)
loadExternalSystemOptions(moduleEntity, reader, externalSystemOptions, externalSystemId, entitySource)
builder addEntity moduleEntity
loadContentRoots(customRootsSerializer, builder, moduleEntity, reader, customDir, errorReporter, virtualFileManager,
moduleEntity.entitySource, false)
loadTestModuleProperty(builder, moduleEntity, reader, entitySource)
@@ -223,15 +231,14 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
{ it.getAttributeValue("value") })
}
protected open fun loadExternalSystemOptions(builder: MutableEntityStorage,
module: ModuleEntity,
protected open fun loadExternalSystemOptions(module: ModuleEntity,
reader: JpsFileContentReader,
externalSystemOptions: Map<String?, String?>,
externalSystemId: String?,
entitySource: EntitySource) {
if (!shouldCreateExternalSystemModuleOptions(externalSystemId, externalSystemOptions, MODULE_OPTIONS_TO_CHECK)) return
val optionsEntity = builder.getOrCreateExternalSystemModuleOptions(module, entitySource)
builder.modifyEntity(optionsEntity) {
ExternalSystemModuleOptionsEntity(entitySource) {
this.module = module
externalSystem = externalSystemId
externalSystemModuleVersion = externalSystemOptions["external.system.module.version"]
linkedProjectPath = externalSystemOptions["external.linked.project.path"]

View File

@@ -14,11 +14,9 @@ fun MutableEntityStorage.addModuleEntity(name: @NlsSafe String,
dependencies: List<ModuleDependencyItem>,
source: EntitySource,
type: @NonNls String? = null): ModuleEntity {
val entity = ModuleEntity(name, dependencies, source) {
return this addEntity ModuleEntity(name, dependencies, source) {
this.type = type
}
this.addEntity(entity)
return entity
}
fun MutableEntityStorage.addJavaModuleSettingsEntity(inheritedCompilerOutput: Boolean,
@@ -49,11 +47,9 @@ fun MutableEntityStorage.addModuleCustomImlDataEntity(rootManagerTagCustomData:
fun MutableEntityStorage.addModuleGroupPathEntity(path: List<@NlsSafe String>,
module: ModuleEntity,
source: EntitySource): ModuleGroupPathEntity {
val entity = ModuleGroupPathEntity(path, source) {
return this addEntity ModuleGroupPathEntity(path, source) {
this.module = module
}
this.addEntity(entity)
return entity
}
fun MutableEntityStorage.addSourceRootEntity(contentRoot: ContentRootEntity,

View File

@@ -5,7 +5,6 @@ import com.intellij.workspaceModel.ide.impl.jps.serialization.CustomModuleCompon
import com.intellij.workspaceModel.ide.impl.jps.serialization.ErrorReporter
import com.intellij.workspaceModel.ide.impl.jps.serialization.JpsFileContentReader
import com.intellij.workspaceModel.ide.impl.jps.serialization.JpsFileContentWriter
import com.intellij.workspaceModel.storage.MutableEntityStorage
import com.intellij.workspaceModel.storage.bridgeEntities.ModuleEntity
import com.intellij.workspaceModel.storage.url.VirtualFileUrl
import com.intellij.workspaceModel.storage.url.VirtualFileUrlManager
@@ -19,21 +18,25 @@ import org.jetbrains.jps.model.serialization.JpsProjectLoader
* Implements loading and saving configuration from [EclipseModuleManagerImpl] in iml file when workspace model is used
*/
class EclipseModuleManagerSerializer : CustomModuleComponentSerializer {
override fun loadComponent(builder: MutableEntityStorage,
moduleEntity: ModuleEntity,
override fun loadComponent(detachedModuleEntity: ModuleEntity,
reader: JpsFileContentReader,
imlFileUrl: VirtualFileUrl,
errorReporter: ErrorReporter,
virtualFileManager: VirtualFileUrlManager) {
val componentTag = reader.loadComponent(imlFileUrl.url, "EclipseModuleManager") ?: return
val entity = builder.addEclipseProjectPropertiesEntity(moduleEntity, moduleEntity.entitySource)
builder.modifyEntity(entity) {
val entity = EclipseProjectPropertiesEntity(LinkedHashMap(), ArrayList(), ArrayList(), ArrayList(), false, 0,
LinkedHashMap(), detachedModuleEntity.entitySource) {
this.module = detachedModuleEntity
}
(entity as EclipseProjectPropertiesEntity.Builder).apply {
componentTag.getChildren(LIBELEMENT).forEach {
eclipseUrls.add(virtualFileManager.fromUrl(it.getAttributeValue(VALUE_ATTR)!!))
}
componentTag.getChildren(VARELEMENT).forEach {
variablePaths = variablePaths.toMutableMap().also { map -> map[it.getAttributeValue(VAR_ATTRIBUTE)!!] =
it.getAttributeValue(PREFIX_ATTR, "") + it.getAttributeValue(VALUE_ATTR) }
variablePaths = variablePaths.toMutableMap().also { map ->
map[it.getAttributeValue(VAR_ATTRIBUTE)!!] =
it.getAttributeValue(PREFIX_ATTR, "") + it.getAttributeValue(VALUE_ATTR)
}
}
componentTag.getChildren(CONELEMENT).forEach {
unknownCons.add(it.getAttributeValue(VALUE_ATTR)!!)

View File

@@ -104,11 +104,9 @@ data class EclipseProjectFile(
}
fun MutableEntityStorage.addEclipseProjectPropertiesEntity(module: ModuleEntity, source: EntitySource): EclipseProjectPropertiesEntity {
val entity = EclipseProjectPropertiesEntity(LinkedHashMap(), ArrayList(), ArrayList(), ArrayList(), false, 0, LinkedHashMap(), source) {
return this addEntity EclipseProjectPropertiesEntity(LinkedHashMap(), ArrayList(), ArrayList(), ArrayList(), false, 0, LinkedHashMap(), source) {
this.module = module
}
this.addEntity(entity)
return entity
}
}