Search in sources :

Example 6 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class PythonFacetUtil method removeLibrary.

public static void removeLibrary(Module module) {
    ApplicationManager.getApplication().runWriteAction(() -> {
        final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
        final ModifiableRootModel model = rootManager.getModifiableModel();
        // Just remove all old facet libraries
        for (OrderEntry entry : model.getOrderEntries()) {
            if (entry instanceof LibraryOrderEntry) {
                final Library library = ((LibraryOrderEntry) entry).getLibrary();
                if (library != null) {
                    final String libraryName = library.getName();
                    if (libraryName != null && libraryName.endsWith(PYTHON_FACET_LIBRARY_NAME_SUFFIX)) {
                        model.removeOrderEntry(entry);
                    //PyBuiltinCache.clearInstanceCache();
                    }
                }
            }
        }
        model.commit();
    });
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Example 7 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class MvcFramework method getImplicitClasspathRoots.

protected List<File> getImplicitClasspathRoots(@NotNull Module module) {
    final List<File> toExclude = new ArrayList<>();
    VirtualFile sdkRoot = getSdkRoot(module);
    if (sdkRoot != null)
        toExclude.add(VfsUtil.virtualToIoFile(sdkRoot));
    ContainerUtil.addIfNotNull(toExclude, getCommonPluginsDir(module));
    final VirtualFile appRoot = findAppRoot(module);
    if (appRoot != null) {
        VirtualFile pluginDir = appRoot.findChild(MvcModuleStructureUtil.PLUGINS_DIRECTORY);
        if (pluginDir != null)
            toExclude.add(VfsUtil.virtualToIoFile(pluginDir));
        VirtualFile libDir = appRoot.findChild("lib");
        if (libDir != null)
            toExclude.add(VfsUtil.virtualToIoFile(libDir));
    }
    final Library library = MvcModuleStructureUtil.findUserLibrary(module, getUserLibraryName());
    if (library != null) {
        for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) {
            toExclude.add(VfsUtil.virtualToIoFile(PathUtil.getLocalFile(file)));
        }
    }
    return toExclude;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Library(com.intellij.openapi.roots.libraries.Library) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 8 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class MvcModuleStructureUtil method syncAuxModuleSdk.

public static void syncAuxModuleSdk(@NotNull Module appModule, @NotNull Module pluginsModule, @NotNull final MvcFramework framework) {
    final ModuleRootManager auxRootManager = ModuleRootManager.getInstance(pluginsModule);
    final ModuleRootManager appRootManager = ModuleRootManager.getInstance(appModule);
    final boolean isSdkEquals = Comparing.equal(auxRootManager.getSdk(), appRootManager.getSdk());
    List<Library> appLibraries = new ArrayList<>();
    Library appUserLibrary = extractNonModuleLibraries(appLibraries, appRootManager, false, framework.getUserLibraryName());
    List<Library> auxLibraries = new ArrayList<>();
    Library auxUserLibrary = extractNonModuleLibraries(auxLibraries, auxRootManager, false, framework.getUserLibraryName());
    final boolean isLibrariesEquals = appLibraries.equals(auxLibraries) && getJarUrls(auxUserLibrary).equals(getJarUrls(appUserLibrary));
    if (!isSdkEquals || !isLibrariesEquals) {
        ModuleRootModificationUtil.updateModel(pluginsModule, model -> {
            if (!isSdkEquals) {
                copySdk(appRootManager, model);
            }
            if (!isLibrariesEquals) {
                copyUserLibraries(appRootManager, model, framework);
            }
        });
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Example 9 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class MvcModuleStructureUtil method addJarDirectory.

@Nullable
public static Consumer<ModifiableRootModel> addJarDirectory(VirtualFile root, Module module, final String libName) {
    final VirtualFile libDir = root.findFileByRelativePath("lib");
    if (libDir == null || !libDir.isDirectory() || ProjectRootManager.getInstance(module.getProject()).getFileIndex().isExcluded(libDir)) {
        return null;
    }
    final Library library = findUserLibrary(module, libName);
    if (library != null && library.isJarDirectory(libDir.getUrl())) {
        return null;
    }
    return model -> {
        Library.ModifiableModel libModel = modifyDefaultLibrary(model, libName);
        libModel.addJarDirectory(libDir, false);
        libModel.commit();
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) java.util(java.util) Application(com.intellij.openapi.application.Application) ArrayUtil(com.intellij.util.ArrayUtil) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) PsiManager(com.intellij.psi.PsiManager) com.intellij.openapi.roots(com.intellij.openapi.roots) Library(com.intellij.openapi.roots.libraries.Library) Comparing(com.intellij.openapi.util.Comparing) SmartList(com.intellij.util.SmartList) JpsModuleSourceRootType(org.jetbrains.jps.model.module.JpsModuleSourceRootType) ModifiableFacetModel(com.intellij.facet.ModifiableFacetModel) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) MultiMap(com.intellij.util.containers.MultiMap) CommonProcessors(com.intellij.util.CommonProcessors) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) LibraryUtil(com.intellij.openapi.roots.libraries.LibraryUtil) ModifiableModelCommitter(com.intellij.openapi.roots.impl.ModifiableModelCommitter) JavaSourceRootType(org.jetbrains.jps.model.java.JavaSourceRootType) Key(com.intellij.openapi.util.Key) IOException(java.io.IOException) FacetManager(com.intellij.facet.FacetManager) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) ModuleDeleteProvider(com.intellij.openapi.roots.ui.configuration.actions.ModuleDeleteProvider) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) VfsUtil(com.intellij.openapi.vfs.VfsUtil) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) Library(com.intellij.openapi.roots.libraries.Library) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class MvcModuleStructureUtil method copyUserLibraries.

public static void copyUserLibraries(ModuleRootModel from, ModifiableRootModel to, @NotNull MvcFramework framework) {
    Library userLibraryTo = null;
    for (OrderEntry entry : to.getOrderEntries()) {
        if (entry instanceof LibraryOrderEntry) {
            LibraryOrderEntry libraryEntry = (LibraryOrderEntry) entry;
            Library library = libraryEntry.getLibrary();
            if (library != null) {
                String libraryName = libraryEntry.getLibraryName();
                if (libraryName != null && libraryName.startsWith(framework.getUserLibraryName())) {
                    userLibraryTo = library;
                } else {
                    if (library.getTable() != null && (!libraryEntry.getScope().isForProductionRuntime() || framework.isSDKLibrary(library))) {
                        to.removeOrderEntry(entry);
                    }
                }
            }
        }
    }
    Library userLibraryFrom = null;
    for (OrderEntry entry : from.getOrderEntries()) {
        if (entry instanceof LibraryOrderEntry) {
            LibraryOrderEntry libraryEntry = (LibraryOrderEntry) entry;
            Library library = libraryEntry.getLibrary();
            if (library != null) {
                String libraryName = library.getName();
                if (libraryName != null && libraryName.startsWith(framework.getUserLibraryName())) {
                    userLibraryFrom = library;
                } else {
                    if (library.getTable() != null) {
                        LibraryOrderEntry libraryOrderEntry = to.addLibraryEntry(library);
                        libraryOrderEntry.setScope(DependencyScope.PROVIDED);
                    }
                }
            }
        }
    }
    if (userLibraryTo == null) {
        if (userLibraryFrom == null)
            return;
        userLibraryTo = to.getModuleLibraryTable().createLibrary(framework.getUserLibraryName() + " (" + to.getModule().getName() + ')');
    } else {
        OrderEntry[] orderEntries = to.getOrderEntries().clone();
        for (int i = 0; i < orderEntries.length; i++) {
            OrderEntry orderEntry = orderEntries[i];
            if (orderEntry instanceof LibraryOrderEntry) {
                if (userLibraryTo == ((LibraryOrderEntry) orderEntry).getLibrary()) {
                    System.arraycopy(orderEntries, i + 1, orderEntries, i, orderEntries.length - i - 1);
                    orderEntries[orderEntries.length - 1] = orderEntry;
                    to.rearrangeOrderEntries(orderEntries);
                    break;
                }
            }
        }
    }
    Library.ModifiableModel model = userLibraryTo.getModifiableModel();
    for (String url : model.getUrls(OrderRootType.CLASSES)) {
        if (!model.isJarDirectory(url)) {
            model.removeRoot(url, OrderRootType.CLASSES);
        }
    }
    if (userLibraryFrom != null) {
        for (String url : userLibraryFrom.getUrls(OrderRootType.CLASSES)) {
            if (!userLibraryFrom.isJarDirectory(url)) {
                model.addRoot(url, OrderRootType.CLASSES);
            }
        }
    }
    model.commit();
}
Also used : Library(com.intellij.openapi.roots.libraries.Library)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)266 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)65 Module (com.intellij.openapi.module.Module)43 NotNull (org.jetbrains.annotations.NotNull)32 File (java.io.File)31 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)28 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)27 Project (com.intellij.openapi.project.Project)25 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)25 Nullable (org.jetbrains.annotations.Nullable)25 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)19 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)12 THashSet (gnu.trove.THashSet)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 Result (com.intellij.openapi.application.Result)7 Logger (com.intellij.openapi.diagnostic.Logger)7