Search in sources :

Example 1 with ContentRootData

use of com.intellij.openapi.externalSystem.model.project.ContentRootData in project intellij-community by JetBrains.

the class ContentRootDataService method importData.

@Override
public void importData(@NotNull Collection<DataNode<ContentRootData>> toImport, @Nullable ProjectData projectData, @NotNull Project project, @NotNull IdeModifiableModelsProvider modelsProvider) {
    if (toImport.isEmpty()) {
        return;
    }
    boolean isNewlyImportedProject = project.getUserData(ExternalSystemDataKeys.NEWLY_IMPORTED_PROJECT) == Boolean.TRUE;
    boolean forceDirectoriesCreation = false;
    DataNode<ProjectData> projectDataNode = ExternalSystemApiUtil.findParent(toImport.iterator().next(), ProjectKeys.PROJECT);
    if (projectDataNode != null) {
        forceDirectoriesCreation = projectDataNode.getUserData(CREATE_EMPTY_DIRECTORIES) == Boolean.TRUE;
    }
    Set<Module> modulesToExpand = ContainerUtil.newTroveSet();
    MultiMap<DataNode<ModuleData>, DataNode<ContentRootData>> byModule = ExternalSystemApiUtil.groupBy(toImport, ModuleData.class);
    for (Map.Entry<DataNode<ModuleData>, Collection<DataNode<ContentRootData>>> entry : byModule.entrySet()) {
        Module module = entry.getKey().getUserData(AbstractModuleDataService.MODULE_KEY);
        module = module != null ? module : modelsProvider.findIdeModule(entry.getKey().getData());
        if (module == null) {
            LOG.warn(String.format("Can't import content roots. Reason: target module (%s) is not found at the ide. Content roots: %s", entry.getKey(), entry.getValue()));
            continue;
        }
        importData(modelsProvider, entry.getValue(), module, forceDirectoriesCreation);
        if (forceDirectoriesCreation || (isNewlyImportedProject && projectData != null && projectData.getLinkedExternalProjectPath().equals(ExternalSystemApiUtil.getExternalProjectPath(module)))) {
            modulesToExpand.add(module);
        }
    }
    if (!modulesToExpand.isEmpty()) {
        for (Module module : modulesToExpand) {
            String productionModuleName = modelsProvider.getProductionModuleName(module);
            if (productionModuleName == null || !modulesToExpand.contains(modelsProvider.findIdeModule(productionModuleName))) {
                VirtualFile[] roots = modelsProvider.getModifiableRootModel(module).getContentRoots();
                if (roots.length > 0) {
                    VirtualFile virtualFile = roots[0];
                    ExternalSystemUtil.invokeLater(project, ModalityState.NON_MODAL, () -> {
                        final ProjectView projectView = ProjectView.getInstance(project);
                        projectView.changeViewCB(ProjectViewPane.ID, null).doWhenProcessed(() -> projectView.selectCB(null, virtualFile, false));
                    });
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentRootData(com.intellij.openapi.externalSystem.model.project.ContentRootData) Collection(java.util.Collection) ProjectView(com.intellij.ide.projectView.ProjectView) Module(com.intellij.openapi.module.Module) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData)

Example 2 with ContentRootData

use of com.intellij.openapi.externalSystem.model.project.ContentRootData in project intellij-community by JetBrains.

the class ContentRootDataService method importData.

private static void importData(@NotNull IdeModifiableModelsProvider modelsProvider, @NotNull final Collection<DataNode<ContentRootData>> data, @NotNull final Module module, boolean forceDirectoriesCreation) {
    final ModifiableRootModel modifiableRootModel = modelsProvider.getModifiableRootModel(module);
    final ContentEntry[] contentEntries = modifiableRootModel.getContentEntries();
    final Map<String, ContentEntry> contentEntriesMap = ContainerUtilRt.newHashMap();
    for (ContentEntry contentEntry : contentEntries) {
        contentEntriesMap.put(contentEntry.getUrl(), contentEntry);
    }
    boolean createEmptyContentRootDirectories = forceDirectoriesCreation;
    if (!forceDirectoriesCreation && !data.isEmpty()) {
        ProjectSystemId projectSystemId = data.iterator().next().getData().getOwner();
        AbstractExternalSystemSettings externalSystemSettings = ExternalSystemApiUtil.getSettings(module.getProject(), projectSystemId);
        String path = module.getOptionValue(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY);
        if (path != null) {
            ExternalProjectSettings projectSettings = externalSystemSettings.getLinkedProjectSettings(path);
            createEmptyContentRootDirectories = projectSettings != null && projectSettings.isCreateEmptyContentRootDirectories();
        }
    }
    final Set<ContentEntry> importedContentEntries = ContainerUtil.newIdentityTroveSet();
    for (final DataNode<ContentRootData> node : data) {
        final ContentRootData contentRoot = node.getData();
        final ContentEntry contentEntry = findOrCreateContentRoot(modifiableRootModel, contentRoot.getRootPath());
        if (!importedContentEntries.contains(contentEntry)) {
            // clear source folders but do not remove existing excluded folders
            contentEntry.clearSourceFolders();
            importedContentEntries.add(contentEntry);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Importing content root '%s' for module '%s'", contentRoot.getRootPath(), module.getName()));
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.SOURCE)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.SOURCE, false, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.TEST)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.TEST_SOURCE, false, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.SOURCE_GENERATED)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.SOURCE, true, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.TEST_GENERATED)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaSourceRootType.TEST_SOURCE, true, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.RESOURCE)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaResourceRootType.RESOURCE, false, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.TEST_RESOURCE)) {
            createSourceRootIfAbsent(contentEntry, path, module.getName(), JavaResourceRootType.TEST_RESOURCE, false, createEmptyContentRootDirectories);
        }
        for (SourceRoot path : contentRoot.getPaths(ExternalSystemSourceType.EXCLUDED)) {
            createExcludedRootIfAbsent(contentEntry, path, module.getName(), module.getProject());
        }
        contentEntriesMap.remove(contentEntry.getUrl());
    }
    for (ContentEntry contentEntry : contentEntriesMap.values()) {
        modifiableRootModel.removeContentEntry(contentEntry);
    }
}
Also used : ContentRootData(com.intellij.openapi.externalSystem.model.project.ContentRootData) SourceRoot(com.intellij.openapi.externalSystem.model.project.ContentRootData.SourceRoot) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) ContentEntry(com.intellij.openapi.roots.ContentEntry)

Aggregations

ContentRootData (com.intellij.openapi.externalSystem.model.project.ContentRootData)2 ProjectView (com.intellij.ide.projectView.ProjectView)1 SourceRoot (com.intellij.openapi.externalSystem.model.project.ContentRootData.SourceRoot)1 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)1 AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)1 ExternalProjectSettings (com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)1 Module (com.intellij.openapi.module.Module)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 MultiMap (com.intellij.util.containers.MultiMap)1 Collection (java.util.Collection)1 Map (java.util.Map)1