15
15
*/
16
16
package com .google .idea .blaze .base .qsync ;
17
17
18
- import com .google .common .collect .ImmutableList ;
18
+ import static com .google .common .collect .ImmutableSet .toImmutableSet ;
19
+
19
20
import com .google .common .collect .ImmutableMap ;
20
21
import com .google .common .collect .ImmutableSet ;
21
22
import com .google .common .collect .Sets ;
35
36
import com .google .idea .blaze .qsync .project .ProjectProto .LibrarySource ;
36
37
import com .intellij .openapi .externalSystem .service .project .IdeModifiableModelsProvider ;
37
38
import com .intellij .openapi .externalSystem .service .project .ProjectDataManager ;
38
- import com .intellij .openapi .module .Module ;
39
39
import com .intellij .openapi .module .ModuleType ;
40
40
import com .intellij .openapi .module .ModuleTypeManager ;
41
41
import com .intellij .openapi .project .Project ;
42
- import com .intellij .openapi .roots .ContentEntry ;
43
- import com .intellij .openapi .roots .DependencyScope ;
44
- import com .intellij .openapi .roots .LibraryOrderEntry ;
45
- import com .intellij .openapi .roots .ModifiableRootModel ;
46
- import com .intellij .openapi .roots .OrderEntry ;
47
42
import com .intellij .openapi .roots .OrderRootType ;
48
- import com .intellij .openapi .roots .SourceFolder ;
49
43
import com .intellij .openapi .roots .libraries .Library ;
50
44
import com .intellij .openapi .roots .libraries .Library .ModifiableModel ;
51
45
import com .intellij .openapi .vfs .VfsUtil ;
52
- import org .jetbrains .jps .model .java .JavaSourceRootProperties ;
53
- import org .jetbrains .jps .model .java .JavaSourceRootType ;
54
- import org .jetbrains .jps .model .java .JpsJavaExtensionService ;
55
-
56
46
import java .io .File ;
57
47
import java .nio .file .Path ;
58
48
import java .nio .file .Paths ;
59
- import java .util .AbstractMap ;
60
49
import java .util .List ;
61
50
import java .util .Set ;
62
51
import java .util .function .Function ;
63
52
64
- import static com .google .common .collect .ImmutableList .toImmutableList ;
65
- import static com .google .common .collect .ImmutableSet .toImmutableSet ;
66
- import static java .util .Arrays .stream ;
67
-
68
53
/**
69
54
* An object that monitors the build graph and applies the changes to the project structure.
70
55
*/
@@ -141,74 +126,9 @@ private void updateProjectModel(ProjectProto.Project spec, Context<?> context) {
141
126
libMapBuilder .put (libSpec .getName (), library );
142
127
}
143
128
ImmutableMap <String , Library > libMap = libMapBuilder .buildOrThrow ();
144
-
145
- List <AbstractMap .SimpleImmutableEntry <Module , ProjectProto .Module >> modules =
146
- spec .getModulesList ().stream ().map (moduleSpec -> {
147
- Module module =
148
- models .newModule (
149
- imlDirectory .toPath ().resolve (moduleSpec .getName () + ".iml" ).toString (),
150
- mapModuleType (moduleSpec .getType ()).getId ());
151
-
152
- ModifiableRootModel roots = models .getModifiableRootModel (module );
153
- ImmutableList <OrderEntry > existingLibraryOrderEntries =
154
- stream (roots .getOrderEntries ())
155
- .filter (it -> it instanceof LibraryOrderEntry )
156
- .collect (toImmutableList ());
157
- for (OrderEntry entry : existingLibraryOrderEntries ) {
158
- roots .removeOrderEntry (entry );
159
- }
160
- // TODO: should this be encapsulated in ProjectProto.Module?
161
- roots .inheritSdk ();
162
-
163
- // TODO instead of removing all content entries and re-adding, we should calculate the
164
- // diff.
165
- for (ContentEntry entry : roots .getContentEntries ()) {
166
- roots .removeContentEntry (entry );
167
- }
168
- for (ProjectProto .ContentEntry ceSpec : moduleSpec .getContentEntriesList ()) {
169
- ProjectPath projectPath = ProjectPath .create (ceSpec .getRoot ());
170
-
171
- ContentEntry contentEntry =
172
- roots .addContentEntry (
173
- UrlUtil .pathToUrl (projectPathResolver .resolve (projectPath ).toString ()));
174
- for (ProjectProto .SourceFolder sfSpec : ceSpec .getSourcesList ()) {
175
- ProjectPath sourceFolderProjectPath = ProjectPath .create (sfSpec .getProjectPath ());
176
-
177
- JavaSourceRootProperties properties =
178
- JpsJavaExtensionService .getInstance ()
179
- .createSourceRootProperties (
180
- sfSpec .getPackagePrefix (), sfSpec .getIsGenerated ());
181
- JavaSourceRootType rootType =
182
- sfSpec .getIsTest () ? JavaSourceRootType .TEST_SOURCE : JavaSourceRootType .SOURCE ;
183
- String url =
184
- UrlUtil .pathToUrl (
185
- projectPathResolver .resolve (sourceFolderProjectPath ).toString (),
186
- sourceFolderProjectPath .innerJarPath ());
187
- SourceFolder unused = contentEntry .addSourceFolder (url , rootType , properties );
188
- }
189
- for (String exclude : ceSpec .getExcludesList ()) {
190
- contentEntry .addExcludeFolder (
191
- UrlUtil .pathToIdeaDirectoryUrl (workspaceRoot .absolutePathFor (exclude )));
192
- }
193
- }
194
-
195
- for (String lib : moduleSpec .getLibraryNameList ()) {
196
- Library library = libMap .get (lib );
197
- if (library == null ) {
198
- throw new IllegalStateException (
199
- "Module refers to library " + lib + " not present in the project spec" );
200
- }
201
- LibraryOrderEntry entry = roots .addLibraryEntry (library );
202
- // TODO should this stuff be specified by the Module proto too?
203
- entry .setScope (DependencyScope .COMPILE );
204
- entry .setExported (false );
205
- }
206
- return new AbstractMap .SimpleImmutableEntry <>(module , moduleSpec );
207
- }).toList ();
208
- return new AbstractMap .SimpleImmutableEntry <>(models , modules );
129
+ return ProjectUpdaterHelper .getModulesForModels (spec , models , imlDirectory , projectPathResolver , workspaceRoot , libMap );
209
130
},
210
- readValue -> {
211
- IdeModifiableModelsProvider models = readValue .getKey ();
131
+ (models , modules ) -> {
212
132
WorkspaceLanguageSettings workspaceLanguageSettings =
213
133
LanguageSupport .createWorkspaceLanguageSettings (projectViewSet );
214
134
@@ -217,21 +137,15 @@ private void updateProjectModel(ProjectProto.Project spec, Context<?> context) {
217
137
// suitable
218
138
// data type to be passed in here instead of androidResourceDirectories and
219
139
// androidSourcePackages
220
- for (AbstractMap .SimpleImmutableEntry <Module , ProjectProto .Module > moduleEntry : readValue .getValue ()) {
221
- ProjectProto .Module moduleSpec = moduleEntry .getValue ();
222
- syncPlugin .updateProjectStructureForQuerySync (
223
- project ,
224
- context ,
225
- models ,
226
- workspaceRoot ,
227
- moduleEntry .getKey (),
228
- ImmutableSet .copyOf (moduleSpec .getAndroidResourceDirectoriesList ()),
229
- ImmutableSet .<String >builder ()
230
- .addAll (moduleSpec .getAndroidSourcePackagesList ())
231
- .addAll (moduleSpec .getAndroidCustomPackagesList ())
232
- .build (),
233
- workspaceLanguageSettings );
234
- }
140
+ ProjectUpdaterHelper .updateProjectStructureForQuerySync (
141
+ project ,
142
+ context ,
143
+ models ,
144
+ modules ,
145
+ workspaceRoot ,
146
+ workspaceLanguageSettings ,
147
+ syncPlugin
148
+ );
235
149
}
236
150
models .commit ();
237
151
});
0 commit comments