[workspace model] provide implementation of FacetManager which stores data in workspa...
[idea/community.git] / platform / workspaceModel-ide / src / com / intellij / workspace / jps / JpsProjectEntitiesLoader.kt
1 package com.intellij.workspace.jps
2
3 import com.intellij.openapi.components.PathMacroManager
4 import com.intellij.openapi.components.impl.stores.FileStorageCoreUtil
5 import com.intellij.openapi.util.JDOMUtil
6 import com.intellij.workspace.api.LibraryTableId
7 import com.intellij.workspace.api.TypedEntityStorageBuilder
8 import com.intellij.workspace.api.toVirtualFileUrl
9 import com.intellij.workspace.ide.JpsFileEntitySource
10 import com.intellij.workspace.ide.JpsProjectStoragePlace
11 import org.jdom.Element
12 import java.io.File
13
14 object JpsProjectEntitiesLoader {
15   /**
16    * [serializeArtifacts] specifies whether artifacts should be serialized or not. We need this until a legacy bridge implementation for
17    * ArtifactManager is provided.
18    * [serializeFacets] specifies whether facets should be serialized or not. We need this until a legacy bridge implementation for
19    * FacetManager is provided.
20    */
21   fun createProjectSerializers(storagePlace: JpsProjectStoragePlace,
22                                reader: JpsFileContentReader,
23                                serializeArtifacts: Boolean,
24                                serializeFacets: Boolean): JpsEntitiesSerializationData {
25     return createProjectEntitiesSerializers(storagePlace, serializeArtifacts, serializeFacets).createSerializers(reader)
26   }
27
28   fun loadProject(storagePlace: JpsProjectStoragePlace, builder: TypedEntityStorageBuilder): JpsEntitiesSerializationData {
29     val mainFactories = createProjectEntitiesSerializers(storagePlace, true, true)
30     val reader = CachingJpsFileContentReader(storagePlace.baseDirectoryUrl)
31     val data = mainFactories.createSerializers(reader)
32     data.loadAll(reader, builder)
33     return data
34   }
35
36   fun loadModule(moduleFile: File, storagePlace: JpsProjectStoragePlace, builder: TypedEntityStorageBuilder) {
37     val source = JpsFileEntitySource.FileInDirectory(moduleFile.parentFile.toVirtualFileUrl(), storagePlace)
38     loadModule(moduleFile, source, storagePlace, builder)
39   }
40
41   internal fun loadModule(moduleFile: File,
42                           source: JpsFileEntitySource.FileInDirectory,
43                           storagePlace: JpsProjectStoragePlace,
44                           builder: TypedEntityStorageBuilder) {
45     val mainFactories = createProjectEntitiesSerializers(storagePlace, false, true)
46     val reader = CachingJpsFileContentReader(storagePlace.baseDirectoryUrl)
47     val moduleSerializerFactory = mainFactories.fileSerializerFactories.filterIsInstance<ModuleSerializersFactory>().single()
48
49     val serializer = moduleSerializerFactory.createSerializer(source, moduleFile.toVirtualFileUrl())
50     serializer.loadEntities(builder, reader)
51   }
52
53   private fun createProjectEntitiesSerializers(storagePlace: JpsProjectStoragePlace,
54                                                serializeArtifacts: Boolean,
55                                                serializeFacets: Boolean): JpsEntitiesSerializerFactories {
56     return when (storagePlace) {
57       is JpsProjectStoragePlace.FileBased -> createIprProjectSerializers(storagePlace, serializeArtifacts, serializeFacets)
58       is JpsProjectStoragePlace.DirectoryBased -> createDirectoryProjectSerializers(storagePlace, serializeArtifacts, serializeFacets)
59     }
60   }
61
62   private fun createDirectoryProjectSerializers(storagePlace: JpsProjectStoragePlace.DirectoryBased,
63                                                 serializeArtifacts: Boolean,
64                                                 serializeFacets: Boolean): JpsEntitiesSerializerFactories {
65     val projectDirUrl = storagePlace.projectDir.url
66     val directorySerializersFactories = ArrayList<JpsDirectoryEntitiesSerializerFactory<*>>()
67     directorySerializersFactories += JpsLibrariesDirectorySerializerFactory("$projectDirUrl/.idea/libraries")
68     if (serializeArtifacts) {
69       directorySerializersFactories += JpsArtifactsDirectorySerializerFactory("$projectDirUrl/.idea/artifacts")
70     }
71     return JpsEntitiesSerializerFactories(entityTypeSerializers = emptyList(),
72                                           directorySerializersFactories = directorySerializersFactories,
73                                           fileSerializerFactories = listOf(ModuleSerializersFactory("$projectDirUrl/.idea/modules.xml", serializeFacets)),
74                                           storagePlace = storagePlace)
75   }
76
77   private fun createIprProjectSerializers(storagePlace: JpsProjectStoragePlace.FileBased,
78                                           serializeArtifacts: Boolean,
79                                           serializeFacets: Boolean): JpsEntitiesSerializerFactories {
80     val projectFileSource = JpsFileEntitySource.ExactFile(storagePlace.iprFile, storagePlace)
81     val projectFileUrl = projectFileSource.file
82     val entityTypeSerializers = ArrayList<JpsFileEntityTypeSerializer<*>>()
83     entityTypeSerializers += JpsLibrariesFileSerializer(projectFileUrl, projectFileSource, LibraryTableId.ProjectLibraryTableId)
84     if (serializeArtifacts) {
85       entityTypeSerializers += JpsArtifactsFileSerializer(projectFileUrl, projectFileSource)
86     }
87     return JpsEntitiesSerializerFactories(entityTypeSerializers,
88                                           directorySerializersFactories = emptyList(),
89                                           fileSerializerFactories = listOf(ModuleSerializersFactory(projectFileUrl.url, serializeFacets)),
90                                           storagePlace = storagePlace)
91   }
92 }
93
94 /**
95  * @see com.intellij.core.CoreProjectLoader.loadStorageFile
96  */
97 internal fun loadStorageFile(xmlFile: File, pathMacroManager: PathMacroManager) =
98   FileStorageCoreUtil.load(JDOMUtil.load(xmlFile.toPath()), pathMacroManager) as Map<String, Element>