use of com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData in project intellij by bazelbuild.
the class BlazeAndroidSyncPlugin method updateProjectSdk.
@Override
public void updateProjectSdk(Project project, BlazeContext context, ProjectViewSet projectViewSet, BlazeVersionData blazeVersionData, BlazeProjectData blazeProjectData) {
if (!isAndroidWorkspace(blazeProjectData.workspaceLanguageSettings)) {
return;
}
BlazeAndroidSyncData syncData = blazeProjectData.syncState.get(BlazeAndroidSyncData.class);
if (syncData == null) {
return;
}
AndroidSdkPlatform androidSdkPlatform = syncData.androidSdkPlatform;
if (androidSdkPlatform == null) {
return;
}
Sdk sdk = BlazeSdkProvider.getInstance().findSdk(androidSdkPlatform.androidSdk);
if (sdk == null) {
IssueOutput.error(String.format("Android platform '%s' not found.", androidSdkPlatform.androidSdk)).submit(context);
return;
}
LanguageLevel defaultLanguageLevel = BuildSystemAndroidJdkProvider.languageLevel(Blaze.getBuildSystem(project), blazeVersionData);
LanguageLevel javaLanguageLevel = JavaLanguageLevelSection.getLanguageLevel(projectViewSet, defaultLanguageLevel);
setProjectSdkAndLanguageLevel(project, sdk, javaLanguageLevel);
}
use of com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData in project intellij by bazelbuild.
the class BlazeAndroidSyncPlugin method updateSyncState.
@Override
public void updateSyncState(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet, WorkspaceLanguageSettings workspaceLanguageSettings, BlazeInfo blazeInfo, @Nullable WorkingSet workingSet, WorkspacePathResolver workspacePathResolver, ArtifactLocationDecoder artifactLocationDecoder, TargetMap targetMap, SyncState.Builder syncStateBuilder, @Nullable SyncState previousSyncState) {
if (!isAndroidWorkspace(workspaceLanguageSettings)) {
return;
}
AndroidSdkPlatform androidSdkPlatform = AndroidSdkFromProjectView.getAndroidSdkPlatform(context, projectViewSet);
JavaSourceFilter sourceFilter = new JavaSourceFilter(project, workspaceRoot, projectViewSet, targetMap);
BlazeAndroidWorkspaceImporter workspaceImporter = new BlazeAndroidWorkspaceImporter(project, context, workspaceRoot, projectViewSet, targetMap, sourceFilter, artifactLocationDecoder);
BlazeAndroidImportResult importResult = Scope.push(context, (childContext) -> {
childContext.push(new TimingScope("AndroidWorkspaceImporter", EventType.Other));
return workspaceImporter.importWorkspace();
});
BlazeAndroidSyncData syncData = new BlazeAndroidSyncData(importResult, androidSdkPlatform);
syncStateBuilder.put(BlazeAndroidSyncData.class, syncData);
}
use of com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData in project intellij by bazelbuild.
the class BlazeCreateResourceUtils method setupResDirectoryChoices.
static void setupResDirectoryChoices(Project project, @Nullable VirtualFile contextFile, JBLabel resDirLabel, ComboboxWithBrowseButton resDirComboAndBrowser) {
// Reset the item list before filling it back up.
resDirComboAndBrowser.getComboBox().removeAllItems();
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (blazeProjectData != null) {
BlazeAndroidSyncData syncData = blazeProjectData.syncState.get(BlazeAndroidSyncData.class);
if (syncData != null) {
ImmutableCollection<TargetKey> rulesRelatedToContext = null;
File fileFromContext = null;
if (contextFile != null) {
fileFromContext = VfsUtilCore.virtualToIoFile(contextFile);
rulesRelatedToContext = SourceToTargetMap.getInstance(project).getRulesForSourceFile(fileFromContext);
if (rulesRelatedToContext.isEmpty()) {
rulesRelatedToContext = null;
}
}
ArtifactLocationDecoder artifactLocationDecoder = blazeProjectData.artifactLocationDecoder;
// Sort:
// - contextFile/res if contextFile is a directory,
// to optimize the right click on directory case, or the "closest" string
// match to the contextFile from the res directories known to blaze
// - the rest of the direct dirs, then transitive dirs of the context rules,
// then any known res dir in the project
// as a backup, in alphabetical order.
Set<File> resourceDirs = Sets.newTreeSet();
Set<File> transitiveDirs = Sets.newTreeSet();
Set<File> allResDirs = Sets.newTreeSet();
for (AndroidResourceModule androidResourceModule : syncData.importResult.androidResourceModules) {
Collection<File> resources = artifactLocationDecoder.decodeAll(androidResourceModule.resources);
Collection<File> transitiveResources = artifactLocationDecoder.decodeAll(androidResourceModule.transitiveResources);
// labelsRelatedToContext should include deps,
// but as a first pass we only check the rules themselves
// for resources. If we come up empty, then have anyResDir as a backup.
allResDirs.addAll(transitiveResources);
if (rulesRelatedToContext != null && !rulesRelatedToContext.contains(androidResourceModule.targetKey)) {
continue;
}
resourceDirs.addAll(resources);
transitiveDirs.addAll(transitiveResources);
}
// No need to show some directories twice.
transitiveDirs.removeAll(resourceDirs);
JComboBox resDirCombo = resDirComboAndBrowser.getComboBox();
// Allow the user to browse and overwrite some of the entries,
// in case our inference is wrong.
resDirCombo.setEditable(true);
// After the use confirms the choice, a directory will be created if it is missing.
if (fileFromContext != null && fileFromContext.isDirectory()) {
File closestDirToContext = new File(fileFromContext.getPath(), "res");
resDirCombo.setSelectedItem(closestDirToContext);
} else {
// If we're not completely sure, let people know there are options
// via the placeholder text, and put the most likely on top.
String placeHolder = PLACEHOLDER_TEXT;
resDirCombo.addItem(placeHolder);
resDirCombo.setSelectedItem(placeHolder);
if (fileFromContext != null) {
File closestDirToContext = findClosestDirToContext(fileFromContext.getPath(), resourceDirs);
closestDirToContext = closestDirToContext != null ? closestDirToContext : findClosestDirToContext(fileFromContext.getPath(), transitiveDirs);
if (closestDirToContext != null) {
resDirCombo.addItem(closestDirToContext);
resourceDirs.remove(closestDirToContext);
transitiveDirs.remove(closestDirToContext);
}
}
}
if (!resourceDirs.isEmpty() || !transitiveDirs.isEmpty()) {
for (File resourceDir : resourceDirs) {
resDirCombo.addItem(resourceDir);
}
for (File resourceDir : transitiveDirs) {
resDirCombo.addItem(resourceDir);
}
} else {
for (File resourceDir : allResDirs) {
resDirCombo.addItem(resourceDir);
}
}
resDirComboAndBrowser.setVisible(true);
resDirLabel.setVisible(true);
}
}
}
use of com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData in project intellij by bazelbuild.
the class BlazeAndroidProjectStructureSyncer method updateInMemoryState.
private static void updateInMemoryState(Project project, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, Module workspaceModule, AndroidResourceModuleRegistry registry, LightResourceClassService.Builder rClassBuilder) {
BlazeAndroidSyncData syncData = blazeProjectData.syncState.get(BlazeAndroidSyncData.class);
if (syncData == null) {
return;
}
AndroidSdkPlatform androidSdkPlatform = syncData.androidSdkPlatform;
if (androidSdkPlatform == null) {
return;
}
updateWorkspaceModuleFacetInMemoryState(project, workspaceRoot, workspaceModule, androidSdkPlatform);
ArtifactLocationDecoder artifactLocationDecoder = blazeProjectData.artifactLocationDecoder;
ModuleFinder moduleFinder = ModuleFinder.getInstance(project);
Executor resourceRepositoryExecutor = Executors.newSingleThreadExecutor();
Module libraryResourcesModule = moduleFinder.findModuleByName(LIBRARY_RESOURCES_MODULE_NAME);
if (libraryResourcesModule != null) {
updateLibraryResourcesModuleFacetInMemoryState(project, workspaceRoot, libraryResourcesModule, androidSdkPlatform, syncData.importResult.resourceLibrary == null ? ImmutableList.of() : artifactLocationDecoder.decodeAll(syncData.importResult.resourceLibrary.sources), resourceRepositoryExecutor);
} else if (useLibraryResourcesModule.getValue()) {
logger.warn("Library resources module missing.");
}
for (AndroidResourceModule androidResourceModule : syncData.importResult.androidResourceModules) {
TargetIdeInfo target = blazeProjectData.targetMap.get(androidResourceModule.targetKey);
String moduleName = moduleNameForAndroidModule(target.key);
Module module = moduleFinder.findModuleByName(moduleName);
if (module == null) {
logger.warn("No module found for resource target: " + target.key);
continue;
}
registry.put(module, androidResourceModule);
AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
assert androidIdeInfo != null;
updateModuleFacetInMemoryState(project, androidSdkPlatform, module, moduleDirectoryForAndroidTarget(workspaceRoot, target), manifestFileForAndroidTarget(artifactLocationDecoder, androidIdeInfo, moduleDirectoryForAndroidTarget(workspaceRoot, target)), androidIdeInfo.resourceJavaPackage, artifactLocationDecoder.decodeAll(useLibraryResourcesModule.getValue() ? androidResourceModule.resources : androidResourceModule.transitiveResources), resourceRepositoryExecutor);
rClassBuilder.addRClass(androidIdeInfo.resourceJavaPackage, module);
}
Set<TargetKey> androidResourceModules = syncData.importResult.androidResourceModules.stream().map(androidResourceModule -> androidResourceModule.targetKey).collect(toSet());
List<TargetIdeInfo> runConfigurationTargets = getRunConfigurationTargets(project, projectViewSet, blazeProjectData, androidResourceModules);
for (TargetIdeInfo target : runConfigurationTargets) {
String moduleName = moduleNameForAndroidModule(target.key);
Module module = moduleFinder.findModuleByName(moduleName);
if (module == null) {
logger.warn("No module found for run configuration target: " + target.key);
continue;
}
AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
assert androidIdeInfo != null;
updateModuleFacetInMemoryState(project, androidSdkPlatform, module, moduleDirectoryForAndroidTarget(workspaceRoot, target), manifestFileForAndroidTarget(artifactLocationDecoder, androidIdeInfo, moduleDirectoryForAndroidTarget(workspaceRoot, target)), androidIdeInfo.resourceJavaPackage, ImmutableList.of(), null);
}
}
use of com.google.idea.blaze.android.sync.model.BlazeAndroidSyncData in project intellij by bazelbuild.
the class BlazeAndroidProjectStructureSyncer method updateProjectStructure.
public static void updateProjectStructure(Project project, BlazeContext context, ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData, BlazeSyncPlugin.ModuleEditor moduleEditor, Module workspaceModule, ModifiableRootModel workspaceModifiableModel, boolean isAndroidWorkspace) {
if (!isAndroidWorkspace) {
AndroidFacetModuleCustomizer.removeAndroidFacet(workspaceModule);
return;
}
BlazeAndroidSyncData syncData = blazeProjectData.syncState.get(BlazeAndroidSyncData.class);
if (syncData == null) {
return;
}
AndroidSdkPlatform androidSdkPlatform = syncData.androidSdkPlatform;
if (androidSdkPlatform == null) {
return;
}
// Configure workspace module as an android module
AndroidFacetModuleCustomizer.createAndroidFacet(workspaceModule);
Module libraryResourcesModule = null;
if (useLibraryResourcesModule.getValue()) {
libraryResourcesModule = moduleEditor.createModule(LIBRARY_RESOURCES_MODULE_NAME, StdModuleTypes.JAVA);
AndroidFacetModuleCustomizer.createAndroidFacet(libraryResourcesModule);
}
// Create android resource modules
// Because we're setting up dependencies, the modules have to exist before we configure them
Map<TargetKey, AndroidResourceModule> targetToAndroidResourceModule = Maps.newHashMap();
for (AndroidResourceModule androidResourceModule : syncData.importResult.androidResourceModules) {
targetToAndroidResourceModule.put(androidResourceModule.targetKey, androidResourceModule);
String moduleName = moduleNameForAndroidModule(androidResourceModule.targetKey);
Module module = moduleEditor.createModule(moduleName, StdModuleTypes.JAVA);
AndroidFacetModuleCustomizer.createAndroidFacet(module);
}
// Configure android resource modules
int totalOrderEntries = 0;
Set<File> existingRoots = Sets.newHashSet();
for (AndroidResourceModule androidResourceModule : targetToAndroidResourceModule.values()) {
TargetIdeInfo target = blazeProjectData.targetMap.get(androidResourceModule.targetKey);
AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
assert androidIdeInfo != null;
String moduleName = moduleNameForAndroidModule(target.key);
Module module = moduleEditor.findModule(moduleName);
assert module != null;
ModifiableRootModel modifiableRootModel = moduleEditor.editModule(module);
Collection<File> resources = blazeProjectData.artifactLocationDecoder.decodeAll(androidResourceModule.resources);
if (useCyclicResourceDependency.getValue()) {
// 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.
resources.removeAll(existingRoots);
existingRoots.addAll(resources);
}
ResourceModuleContentRootCustomizer.setupContentRoots(modifiableRootModel, resources);
if (useCyclicResourceDependency.getValue()) {
modifiableRootModel.addModuleOrderEntry(workspaceModule);
++totalOrderEntries;
} else {
for (TargetKey resourceDependency : androidResourceModule.transitiveResourceDependencies) {
if (!targetToAndroidResourceModule.containsKey(resourceDependency)) {
continue;
}
String dependencyModuleName = moduleNameForAndroidModule(resourceDependency);
Module dependency = moduleEditor.findModule(dependencyModuleName);
if (dependency == null) {
continue;
}
modifiableRootModel.addModuleOrderEntry(dependency);
++totalOrderEntries;
}
}
if (libraryResourcesModule != null) {
// Add a dependency from the resource module to the shared library resources module
modifiableRootModel.addModuleOrderEntry(libraryResourcesModule);
++totalOrderEntries;
}
// Add a dependency from the workspace to the resource module
ModuleOrderEntry orderEntry = workspaceModifiableModel.addModuleOrderEntry(module);
++totalOrderEntries;
if (useCyclicResourceDependency.getValue()) {
orderEntry.setExported(true);
}
}
List<TargetIdeInfo> runConfigurationTargets = getRunConfigurationTargets(project, projectViewSet, blazeProjectData, targetToAndroidResourceModule.keySet());
for (TargetIdeInfo target : runConfigurationTargets) {
TargetKey targetKey = target.key;
String moduleName = moduleNameForAndroidModule(targetKey);
Module module = moduleEditor.createModule(moduleName, StdModuleTypes.JAVA);
AndroidFacetModuleCustomizer.createAndroidFacet(module);
}
int whitelistedGenResources = projectViewSet.listItems(GeneratedAndroidResourcesSection.KEY).size();
context.output(PrintOutput.log(String.format("Android resource module count: %d, run config modules: %d, order entries: %d, " + "generated resources: %d", syncData.importResult.androidResourceModules.size(), runConfigurationTargets.size(), totalOrderEntries, whitelistedGenResources)));
}
Aggregations