Skip to content

Commit

Permalink
Add resource content roots for workspace module
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 350385526
  • Loading branch information
Googler authored and copybara-github committed Jan 6, 2021
1 parent a42f134 commit 0d2ca22
Showing 1 changed file with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.idea.blaze.android.sync.projectstructure;

import static com.google.common.base.Verify.verify;
import static java.util.stream.Collectors.toSet;

import com.android.annotations.VisibleForTesting;
Expand Down Expand Up @@ -151,25 +152,31 @@ public static void updateProjectStructure(
for (AndroidResourceModule androidResourceModule :
syncData.importResult.androidResourceModules) {
targetToAndroidResourceModule.put(androidResourceModule.targetKey, androidResourceModule);
String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleEditor.createModule(moduleName, StdModuleTypes.JAVA);
TargetIdeInfo target = blazeProjectData.getTargetMap().get(androidResourceModule.targetKey);
AndroidFacetModuleCustomizer.createAndroidFacet(
module,
target != null
&& target.kindIsOneOf(
AndroidBlazeRules.RuleTypes.ANDROID_BINARY.getKind(),
AndroidBlazeRules.RuleTypes.ANDROID_TEST.getKind()));
if (!BlazeAndroidWorkspaceImporter.WORKSPACE_RESOURCES_TARGET_KEY.equals(
androidResourceModule.targetKey)) {
String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleEditor.createModule(moduleName, StdModuleTypes.JAVA);
TargetIdeInfo target = blazeProjectData.getTargetMap().get(androidResourceModule.targetKey);
AndroidFacetModuleCustomizer.createAndroidFacet(
module,
target != null
&& target.kindIsOneOf(
AndroidBlazeRules.RuleTypes.ANDROID_BINARY.getKind(),
AndroidBlazeRules.RuleTypes.ANDROID_TEST.getKind()));
}
}

// Configure android resource modules
int totalOrderEntries = 0;
Set<File> existingRoots = Sets.newHashSet();
LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
for (AndroidResourceModule androidResourceModule : targetToAndroidResourceModule.values()) {
ArtifactLocationDecoder artifactLocationDecoder =
blazeProjectData.getArtifactLocationDecoder();

File manifest = null;
String moduleName;
ModifiableRootModel modifiableRootModel;
if (!BlazeAndroidWorkspaceImporter.WORKSPACE_RESOURCES_TARGET_KEY.equals(
androidResourceModule.targetKey)) {
// Calculate manifest if this is not the workspace resource module
Expand All @@ -182,33 +189,38 @@ public static void updateProjectStructure(
manifest =
manifestFileForAndroidTarget(
project, artifactLocationDecoder, androidIdeInfo, moduleDirectory);
}

String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleEditor.findModule(moduleName);
assert module != null;
ModifiableRootModel modifiableRootModel = moduleEditor.editModule(module);
LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);

ArrayList<File> newRoots =
new ArrayList<>(
OutputArtifactResolver.resolveAll(
project, artifactLocationDecoder, androidResourceModule.resources));
moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleEditor.findModule(moduleName);
verify(module != null);
modifiableRootModel = moduleEditor.editModule(module);

ArrayList<File> newRoots =
new ArrayList<>(
OutputArtifactResolver.resolveAll(
project, artifactLocationDecoder, androidResourceModule.resources));

if (manifest != null) {
newRoots.add(manifest);
}

if (manifest != null) {
newRoots.add(manifest);
// Remove existing resource roots to silence the duplicate content root error.
// We can only do this if we have cyclic resource dependencies, since otherwise we risk
// breaking dependencies within this resource module.
newRoots.removeAll(existingRoots);
existingRoots.addAll(newRoots);
ResourceModuleContentRootCustomizer.setupContentRoots(modifiableRootModel, newRoots);
modifiableRootModel.addModuleOrderEntry(workspaceModule);
++totalOrderEntries;

// Add a dependency from the workspace to the resource module
ModuleOrderEntry orderEntry = workspaceModifiableModel.addModuleOrderEntry(module);
++totalOrderEntries;
orderEntry.setExported(true);
} else {
moduleName = workspaceModule.getName();
modifiableRootModel = workspaceModifiableModel;
}

// Remove existing resource roots to silence the duplicate content root error.
// We can only do this if we have cyclic resource dependencies, since otherwise we risk
// breaking dependencies within this resource module.
newRoots.removeAll(existingRoots);
existingRoots.addAll(newRoots);
ResourceModuleContentRootCustomizer.setupContentRoots(modifiableRootModel, newRoots);

modifiableRootModel.addModuleOrderEntry(workspaceModule);
++totalOrderEntries;

for (String libraryName : androidResourceModule.resourceLibraryKeys) {
Library lib = libraryTable.getLibraryByName(libraryName);
if (lib == null) {
Expand All @@ -222,10 +234,6 @@ public static void updateProjectStructure(
modifiableRootModel.addLibraryEntry(lib);
}
}
// Add a dependency from the workspace to the resource module
ModuleOrderEntry orderEntry = workspaceModifiableModel.addModuleOrderEntry(module);
++totalOrderEntries;
orderEntry.setExported(true);
}

int whitelistedGenResources =
Expand Down Expand Up @@ -320,12 +328,14 @@ private static void updateInMemoryState(
File manifestFile = null;
String modulePackage;
File moduleDirectory;
Module module;
if (BlazeAndroidWorkspaceImporter.WORKSPACE_RESOURCES_TARGET_KEY.equals(
androidResourceModule.targetKey)) {
// For workspace resource module, give it a dummy package name, and set module
// directory to workspace root. No manifest is attached to this module
modulePackage = BlazeAndroidWorkspaceImporter.WORKSPACE_RESOURCES_MODULE_PACKAGE;
moduleDirectory = workspaceRoot.directory();
module = workspaceModule;
} else {
TargetIdeInfo target =
Preconditions.checkNotNull(
Expand All @@ -336,15 +346,15 @@ private static void updateInMemoryState(
manifestFile =
manifestFileForAndroidTarget(
project, artifactLocationDecoder, androidIdeInfo, moduleDirectory);
String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
module = moduleFinder.findModuleByName(moduleName);
if (module == null) {
log.warn("No module found for resource target: " + androidResourceModule.targetKey);
continue;
}
registry.put(module, androidResourceModule);
}

String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleFinder.findModuleByName(moduleName);
if (module == null) {
log.warn("No module found for resource target: " + androidResourceModule.targetKey);
continue;
}
registry.put(module, androidResourceModule);
List<File> resources =
OutputArtifactResolver.resolveAll(
project, artifactLocationDecoder, androidResourceModule.resources);
Expand Down

0 comments on commit 0d2ca22

Please sign in to comment.