Search in sources :

Example 21 with LibraryEx

use of com.intellij.openapi.roots.impl.libraries.LibraryEx in project intellij-plugins by JetBrains.

the class FlexProjectConfigurationEditor method commit.

public void commit() throws ConfigurationException {
    final Map<Pair<String, String>, String> renamedConfigs = new HashMap<>();
    for (Module module : myModule2Editors.keySet()) {
        ModifiableRootModel modifiableModel = myProvider.getModuleModifiableModel(module);
        Collection<String> usedModulesLibrariesIds = new ArrayList<>();
        // ---------------- SDK and shared libraries entries ----------------------
        // Library -> add_library_entry_flag
        Map<Library, Boolean> librariesToAdd = new LinkedHashMap<>();
        final Collection<String> sdkNames = new HashSet<>();
        for (Editor editor : myModule2Editors.get(module)) {
            final SdkEntry sdkEntry = editor.getDependencies().getSdkEntry();
            if (sdkEntry != null) {
                sdkNames.add(sdkEntry.getName());
            }
            for (DependencyEntry dependencyEntry : editor.getDependencies().getEntries()) {
                if (dependencyEntry instanceof ModuleLibraryEntry) {
                    ModuleLibraryEntry moduleLibraryEntry = (ModuleLibraryEntry) dependencyEntry;
                    usedModulesLibrariesIds.add(moduleLibraryEntry.getLibraryId());
                }
                if (dependencyEntry instanceof SharedLibraryEntry) {
                    SharedLibraryEntry sharedLibraryEntry = (SharedLibraryEntry) dependencyEntry;
                    Library library = myProvider.findSourceLibraryForLiveName(sharedLibraryEntry.getLibraryName(), sharedLibraryEntry.getLibraryLevel());
                    if (library != null) {
                        librariesToAdd.put(library, true);
                    }
                }
            }
            String originalName = editor.getOriginalName();
            if (originalName != null && !originalName.equals(editor.getName())) {
                renamedConfigs.put(Pair.create(module.getName(), originalName), editor.getName());
            }
        }
        final Sdk sdk;
        if (sdkNames.isEmpty()) {
            sdk = null;
        } else if (sdkNames.size() == 1) {
            sdk = FlexSdkUtils.findFlexOrFlexmojosSdk(sdkNames.iterator().next());
        } else {
            sdk = new FlexCompositeSdk(ArrayUtil.toStringArray(sdkNames));
        }
        modifiableModel.setSdk(sdk);
        Collection<OrderEntry> entriesToRemove = new ArrayList<>();
        for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
            if (orderEntry instanceof LibraryOrderEntry) {
                if (((LibraryOrderEntry) orderEntry).isModuleLevel()) {
                    LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
                    if (FlexProjectRootsUtil.isFlexLibrary(library) && !usedModulesLibrariesIds.contains(FlexProjectRootsUtil.getLibraryId(library))) {
                        entriesToRemove.add(orderEntry);
                    }
                } else {
                    LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
                    if (librariesToAdd.containsKey(library)) {
                        // entry already exists for this library
                        librariesToAdd.put(library, false);
                    } else if (library != null && FlexProjectRootsUtil.isFlexLibrary(library)) {
                        entriesToRemove.add(orderEntry);
                    }
                }
            }
        }
        for (OrderEntry e : entriesToRemove) {
            modifiableModel.removeOrderEntry(e);
        }
        for (Library library : librariesToAdd.keySet()) {
            if (!((LibraryEx) library).isDisposed() && librariesToAdd.get(library) && myProvider.findSourceLibrary(library.getName(), library.getTable().getTableLevel()) != null) {
                modifiableModel.addLibraryEntry(library);
            }
        }
        // ---------------- modules entries ----------------------
        final Set<Module> modulesToAdd = new THashSet<>();
        for (Editor editor : myModule2Editors.get(module)) {
            for (DependencyEntry dependencyEntry : editor.getDependencies().getEntries()) {
                if (dependencyEntry instanceof BuildConfigurationEntry) {
                    final Module dependencyModule = findModuleWithBC((BuildConfigurationEntry) dependencyEntry);
                    if (dependencyModule != null && dependencyModule != module) {
                        modulesToAdd.add(dependencyModule);
                    }
                }
            }
        }
        List<OrderEntry> moduleOrderEntriesToRemove = ContainerUtil.filter(modifiableModel.getOrderEntries(), orderEntry -> orderEntry instanceof ModuleOrderEntry && !modulesToAdd.remove(((ModuleOrderEntry) orderEntry).getModule()));
        for (OrderEntry orderEntry : moduleOrderEntriesToRemove) {
            modifiableModel.removeOrderEntry(orderEntry);
        }
        for (Module moduleToAdd : modulesToAdd) {
            modifiableModel.addModuleOrderEntry(moduleToAdd);
        }
        for (OrderEntry entry : modifiableModel.getOrderEntries()) {
            if (entry instanceof ExportableOrderEntry) {
                // transitiveness will be filtered out in FlexOrderEnumeratorHandler if needed
                ((ExportableOrderEntry) entry).setExported(true);
            }
        }
    }
    // ---------------- do commit ----------------------
    Collection<Module> modulesWithChangedModifiableModel = ContainerUtil.findAll(myModule2Editors.keySet(), module -> myProvider.getModuleModifiableModel(module).isChanged());
    if (!modulesWithChangedModifiableModel.isEmpty()) {
        myProvider.commitModifiableModels();
        myModulesModelChangeEventDispatcher.getMulticaster().modulesModelsChanged(modulesWithChangedModifiableModel);
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        for (Module module : myModule2Editors.keySet()) {
            Function<Editor, FlexBuildConfigurationImpl> f = editor -> editor.commit();
            FlexBuildConfigurationImpl[] current = ContainerUtil.map2Array(myModule2Editors.get(module), FlexBuildConfigurationImpl.class, f);
            ((FlexBuildConfigurationManagerImpl) FlexBuildConfigurationManager.getInstance(module)).setBuildConfigurations(current);
        }
        if (myProject != null) {
            FlexBuildConfigurationManagerImpl.resetHighlighting(myProject);
            if (!renamedConfigs.isEmpty()) {
                myProject.getMessageBus().syncPublisher(FlexBuildConfigurationChangeListener.TOPIC).buildConfigurationsRenamed(renamedConfigs);
            }
        }
    });
}
Also used : FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) java.util(java.util) OutputType(com.intellij.flex.model.bc.OutputType) ArrayUtil(com.intellij.util.ArrayUtil) EventDispatcher(com.intellij.util.EventDispatcher) FlexModuleType(com.intellij.lang.javascript.flex.FlexModuleType) HashMap(com.intellij.util.containers.HashMap) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) TargetPlatform(com.intellij.flex.model.bc.TargetPlatform) FlexCommonUtils(com.intellij.flex.FlexCommonUtils) com.intellij.openapi.roots(com.intellij.openapi.roots) Library(com.intellij.openapi.roots.libraries.Library) BCUtils(com.intellij.lang.javascript.flex.projectStructure.options.BCUtils) FlexBCConfigurable(com.intellij.lang.javascript.flex.projectStructure.ui.FlexBCConfigurable) Project(com.intellij.openapi.project.Project) ModuleType(com.intellij.openapi.module.ModuleType) ChangeListener(javax.swing.event.ChangeListener) FlexSdkUtils(com.intellij.lang.javascript.flex.sdk.FlexSdkUtils) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) FlexLibraryType(com.intellij.lang.javascript.flex.library.FlexLibraryType) FlexBuildConfigurationsExtension(com.intellij.lang.javascript.flex.projectStructure.FlexBuildConfigurationsExtension) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) LibraryTablesRegistrar(com.intellij.openapi.roots.libraries.LibraryTablesRegistrar) com.intellij.lang.javascript.flex.projectStructure.model(com.intellij.lang.javascript.flex.projectStructure.model) Disposable(com.intellij.openapi.Disposable) Sdk(com.intellij.openapi.projectRoots.Sdk) BuildConfigurationNature(com.intellij.flex.model.bc.BuildConfigurationNature) FlexProjectRootsUtil(com.intellij.lang.javascript.flex.projectStructure.options.FlexProjectRootsUtil) Nullable(org.jetbrains.annotations.Nullable) LibraryEditingUtil(com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil) Function(com.intellij.util.Function) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull) FlexLibraryProperties(com.intellij.lang.javascript.flex.library.FlexLibraryProperties) Consumer(com.intellij.util.Consumer) HashMap(com.intellij.util.containers.HashMap) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) Pair(com.intellij.openapi.util.Pair) THashSet(gnu.trove.THashSet) FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) THashSet(gnu.trove.THashSet) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Example 22 with LibraryEx

use of com.intellij.openapi.roots.impl.libraries.LibraryEx in project intellij-plugins by JetBrains.

the class FlexProjectConfigurationEditor method copyModuleLibrary.

private static LibraryEx copyModuleLibrary(final ModifiableRootModel modifiableModel, final LibraryEx library) {
    LibraryTable.ModifiableModel librariesModifiableModel = getTableModifiableModel(modifiableModel);
    LibraryEx libraryCopy = (LibraryEx) librariesModifiableModel.createLibrary(library.getName(), library.getKind());
    LibraryEx.ModifiableModelEx libraryCopyModel = libraryCopy.getModifiableModel();
    // will overwrite library id
    LibraryEditingUtil.copyLibrary(library, Collections.emptyMap(), libraryCopyModel);
    // do assign unique library id
    libraryCopyModel.setProperties(new FlexLibraryProperties(FlexLibraryIdGenerator.generateId()));
    libraryCopyModel.commit();
    return libraryCopy;
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) FlexLibraryProperties(com.intellij.lang.javascript.flex.library.FlexLibraryProperties) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx)

Example 23 with LibraryEx

use of com.intellij.openapi.roots.impl.libraries.LibraryEx in project intellij-plugins by JetBrains.

the class FlexOrderEnumerationHandler method shouldAddDependency.

@NotNull
@Override
public AddDependencyType shouldAddDependency(@NotNull OrderEntry orderEntry, @NotNull OrderEnumeratorSettings settings) {
    Module module = orderEntry.getOwnerModule();
    if (ModuleType.get(module) != FlexModuleType.getInstance()) {
        return super.shouldAddDependency(orderEntry, settings);
    }
    if (orderEntry instanceof ModuleSourceOrderEntry) {
        return AddDependencyType.DEFAULT;
    }
    if (orderEntry instanceof JdkOrderEntry) {
        if (module != myRootModule) {
            // never add transitive dependency to Flex SDK
            return AddDependencyType.DO_NOT_ADD;
        }
        if (myActiveConfigurations == null) {
            return AddDependencyType.DEFAULT;
        }
        ModuleData moduleData = myActiveConfigurations.get(module);
        for (FlexBuildConfiguration bc : moduleData.bcs) {
            if (bc.getSdk() != null) {
                return AddDependencyType.DEFAULT;
            }
        }
        return AddDependencyType.DO_NOT_ADD;
    }
    Collection<FlexBuildConfiguration> accessibleConfigurations;
    if (myActiveConfigurations != null) {
        ModuleData moduleData = myActiveConfigurations.get(module);
        accessibleConfigurations = moduleData != null ? moduleData.bcs : Collections.emptyList();
    } else {
        // let all configurations be accessible in ProjectOrderEnumerator
        accessibleConfigurations = Arrays.asList(FlexBuildConfigurationManager.getInstance(module).getBuildConfigurations());
    }
    if (orderEntry instanceof LibraryOrderEntry) {
        final LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
        if (library == null) {
            return AddDependencyType.DEFAULT;
        }
        if (library.getKind() == FlexLibraryType.FLEX_LIBRARY) {
            return FlexProjectRootsUtil.dependOnLibrary(accessibleConfigurations, library, module != myRootModule, settings.isProductionOnly()) ? AddDependencyType.DEFAULT : AddDependencyType.DO_NOT_ADD;
        } else {
            // foreign library
            return AddDependencyType.DO_NOT_ADD;
        }
    } else if (orderEntry instanceof ModuleOrderEntry) {
        final Module dependencyModule = ((ModuleOrderEntry) orderEntry).getModule();
        if (dependencyModule == null) {
            return AddDependencyType.DO_NOT_ADD;
        }
        if (myActiveConfigurations != null) {
            ModuleData moduleData = myActiveConfigurations.get(dependencyModule);
            return moduleData != null && (moduleData.accessibleInProduction || !settings.isProductionOnly()) ? AddDependencyType.DEFAULT : AddDependencyType.DO_NOT_ADD;
        } else {
            // let all modules dependencies be accessible in ProjectOrderEnumerator
            return AddDependencyType.DEFAULT;
        }
    } else {
        return AddDependencyType.DEFAULT;
    }
}
Also used : FlexBuildConfiguration(com.intellij.lang.javascript.flex.projectStructure.model.FlexBuildConfiguration) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with LibraryEx

use of com.intellij.openapi.roots.impl.libraries.LibraryEx in project intellij-plugins by JetBrains.

the class FlexProjectConfigurationEditor method copyConfiguration.

public ModifiableFlexBuildConfiguration copyConfiguration(ModifiableFlexBuildConfiguration configuration, BuildConfigurationNature newNature) {
    assertAlive();
    Module module = ((Editor) configuration).myModule;
    List<Editor> editors = myModule2Editors.get(module);
    FlexBuildConfigurationImpl copy = ((Editor) configuration).getCopy();
    DependencyEntry[] entries = copy.getDependencies().getEntries();
    ModifiableRootModel modifiableModel = myProvider.getModuleModifiableModel(module);
    for (int i = 0; i < entries.length; i++) {
        if (entries[i] instanceof ModuleLibraryEntryImpl) {
            ModuleLibraryEntryImpl e = (ModuleLibraryEntryImpl) entries[i];
            LibraryEx library = findLibrary(modifiableModel, e.getLibraryId());
            if (library != null) {
                LibraryEx libraryCopy = copyModuleLibrary(modifiableModel, library);
                ModuleLibraryEntryImpl entryCopy = new ModuleLibraryEntryImpl(FlexProjectRootsUtil.getLibraryId(libraryCopy));
                e.getDependencyType().applyTo(entryCopy.getDependencyType());
                copy.getDependencies().getModifiableEntries().set(i, entryCopy);
            }
        }
    }
    Editor newConfig = new Editor(module, copy, false);
    newConfig.setNature(newNature);
    // just to simplify serialized view
    resetNonApplicableValuesToDefaults(newConfig);
    editors.add(newConfig);
    return newConfig;
}
Also used : LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Module(com.intellij.openapi.module.Module)

Example 25 with LibraryEx

use of com.intellij.openapi.roots.impl.libraries.LibraryEx in project intellij-plugins by JetBrains.

the class JstdLibraryUtil method doesCorrectJstdLibExist.

private static boolean doesCorrectJstdLibExist(@NotNull final Project project) {
    Boolean correctJstdLibExists = JSTD_LIBRARY_EXISTS;
    if (correctJstdLibExists == null) {
        correctJstdLibExists = ReadAction.compute(() -> {
            VirtualFile libVirtualFile = VfsUtil.findFileByURL(JstdDefaultAssertionFrameworkSrcMarker.class.getResource("TestCase.js"));
            if (libVirtualFile == null) {
                return false;
            }
            JSLibraryManager libraryManager = JSLibraryManager.getInstance(project);
            for (ScriptingLibraryModel libraryModel : libraryManager.getAllLibraries()) {
                if (libraryModel == null) {
                    continue;
                }
                String libraryName = libraryModel.getName();
                if (libraryName != null && libraryName.startsWith(LIBRARY_NAME)) {
                    Library library = libraryModel.getOriginalLibrary();
                    if (library instanceof LibraryEx) {
                        LibraryEx libraryEx = (LibraryEx) library;
                        if (libraryEx.isDisposed()) {
                            continue;
                        }
                    }
                    if (libraryModel.containsFile(libVirtualFile)) {
                        return true;
                    }
                }
            }
            return false;
        });
        JSTD_LIBRARY_EXISTS = correctJstdLibExists;
    }
    return correctJstdLibExists;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ScriptingLibraryModel(com.intellij.webcore.libraries.ScriptingLibraryModel) JSLibraryManager(com.intellij.lang.javascript.library.JSLibraryManager) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Library(com.intellij.openapi.roots.libraries.Library) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)38 Library (com.intellij.openapi.roots.libraries.Library)19 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)8 Module (com.intellij.openapi.module.Module)6 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)6 Project (com.intellij.openapi.project.Project)5 FlexLibraryProperties (com.intellij.lang.javascript.flex.library.FlexLibraryProperties)4 Logger (com.intellij.openapi.diagnostic.Logger)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4 Disposable (com.intellij.openapi.Disposable)3 OrderRootType (com.intellij.openapi.roots.OrderRootType)3 LibraryProperties (com.intellij.openapi.roots.libraries.LibraryProperties)3 StringUtil (com.intellij.openapi.util.text.StringUtil)3 List (java.util.List)3 FlexCommonUtils (com.intellij.flex.FlexCommonUtils)2 BuildConfigurationNature (com.intellij.flex.model.bc.BuildConfigurationNature)2 OutputType (com.intellij.flex.model.bc.OutputType)2