Search in sources :

Example 56 with Library

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

the class SyncLibraryRegistryTest method testGetLibrariesToUpdateWhenUrlsWereRemoved.

public void testGetLibrariesToUpdateWhenUrlsWereRemoved() throws IOException {
    File jarPath1 = createTempFile("fake1.jar", null);
    File jarPath2 = createTempFile("fake2.jar", null);
    Library library = simulateProjectHasLibrary(jarPath1, jarPath2);
    SyncLibraryRegistry libraryRegistry = SyncLibraryRegistry.getInstance(getProject());
    libraryRegistry.markAsUsed(library, jarPath1);
    List<LibraryToUpdate> librariesToUpdate = libraryRegistry.getLibrariesToUpdate();
    assertThat(librariesToUpdate).hasSize(1);
    LibraryToUpdate libraryToUpdate = librariesToUpdate.get(0);
    assertSame(library, libraryToUpdate.getLibrary());
    assertThat(libraryToUpdate.getNewBinaryUrls()).containsExactly(pathToUrl(jarPath1.getPath()));
}
Also used : LibraryToUpdate(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry.LibraryToUpdate) Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File)

Example 57 with Library

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

the class TestArtifactSearchScopesTest method testLibrariesExcluding.

public void testLibrariesExcluding() throws Exception {
    TestArtifactSearchScopes scopes = loadMultiProjectAndTestScopes();
    LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myFixture.getProject());
    // used by android test
    Library guava = libraryTable.getLibraryByName("guava-18.0");
    // used by android test and sometimes by unit test
    Library hamcrest = libraryTable.getLibraryByName("hamcrest-core-1.3");
    // used by unit test
    Library junit = libraryTable.getLibraryByName("junit-4.12");
    // used by android test
    Library gson = libraryTable.getLibraryByName("gson-2.2.4");
    FileRootSearchScope unitTestExcludeScope = scopes.getUnitTestExcludeScope();
    assertScopeContainsLibrary(unitTestExcludeScope, guava, true);
    assertScopeContainsLibrary(unitTestExcludeScope, gson, true);
    assertScopeContainsLibrary(unitTestExcludeScope, junit, false);
    FileRootSearchScope androidTestExcludeScope = scopes.getAndroidTestExcludeScope();
    assertScopeContainsLibrary(androidTestExcludeScope, junit, true);
    assertScopeContainsLibrary(androidTestExcludeScope, gson, false);
    assertScopeContainsLibrary(androidTestExcludeScope, guava, false);
    assertScopeContainsLibrary(androidTestExcludeScope, hamcrest, false);
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library)

Example 58 with Library

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

the class LibraryPropertiesDialogFixture method getLibrary.

@NotNull
public LibraryFixture getLibrary() {
    LibraryTable libraryTable = ProjectLibraryTable.getInstance(myProject);
    Library library = libraryTable.getLibraryByName(myLibraryName);
    assertNotNull("Failed to find library " + quote(myLibraryName), library);
    return new LibraryFixture(library);
}
Also used : ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with Library

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

the class AndroidModuleDependenciesSetup method setUpLibraryDependency.

void setUpLibraryDependency(@NotNull Module module, @NotNull IdeModifiableModelsProvider modelsProvider, @NotNull String libraryName, @NotNull DependencyScope scope, @NotNull File artifactPath, @NotNull File[] binaryPaths, @NotNull File[] documentationPaths) {
    boolean newLibrary = false;
    Library library = modelsProvider.getLibraryByName(libraryName);
    if (library == null) {
        library = modelsProvider.createLibrary(libraryName);
        newLibrary = true;
    } else {
        SyncLibraryRegistry registry = SyncLibraryRegistry.getInstance(module.getProject());
        registry.markAsUsed(library, binaryPaths);
    }
    if (newLibrary) {
        updateLibraryRootTypePaths(library, CLASSES, modelsProvider, binaryPaths);
        // It is common that the same dependency is used by more than one module. Here we update the "sources" and "documentation" paths if they
        // were not set before.
        // Example:
        // In a multi-project project, there are 2 modules: 'app'' (an Android app) and 'util' (a Java lib.) Both of them depend on Guava. Since
        // Android artifacts do not support source attachments, the 'app' module may not indicate where to find the sources for Guava, but the
        // 'util' method can, since it is a plain Java module.
        // If the 'Guava' library was already defined when setting up 'app', it won't have source attachments. When setting up 'util' we may
        // have source attachments, but the library may have been already created. Here we just add the "source" paths if they were not already
        // set.
        File sourceJarPath = myLibraryFilePaths.findSourceJarPath(artifactPath);
        if (sourceJarPath != null) {
            updateLibraryRootTypePaths(library, SOURCES, modelsProvider, sourceJarPath);
        }
        updateLibraryRootTypePaths(library, JavadocOrderRootType.getInstance(), modelsProvider, documentationPaths);
        // TODO: Add this to the model instead!
        for (File binaryPath : binaryPaths) {
            String pathName = binaryPath.getName();
            if (pathName.endsWith(FD_RES) && pathName.length() > FD_RES.length() && pathName.charAt(pathName.length() - FD_RES.length() - 1) == separatorChar) {
                File annotations = new File(pathName.substring(0, pathName.length() - FD_RES.length()), FN_ANNOTATIONS_ZIP);
                if (annotations.isFile()) {
                    updateLibraryRootTypePaths(library, AnnotationOrderRootType.getInstance(), modelsProvider, annotations);
                }
            } else if (libraryName.startsWith("support-annotations-") && pathName.endsWith(DOT_JAR)) {
                // The support annotations is a Java library, not an Android library, so it's not distributed as an AAR
                // with its own external annotations. However, there are a few that we want to make available in the
                // IDE (for example, code completion on @VisibleForTesting(otherwise = |), so we bundle these in the
                // platform annotations zip file instead. We'll also need to add this as a root here.
                File annotations = new File(pathName.substring(0, pathName.length() - DOT_JAR.length()) + "-" + FN_ANNOTATIONS_ZIP);
                if (annotations.isFile()) {
                    updateLibraryRootTypePaths(library, AnnotationOrderRootType.getInstance(), modelsProvider, annotations);
                }
            }
        }
    }
    addLibraryAsDependency(library, libraryName, scope, module, modelsProvider);
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File) SyncLibraryRegistry(com.android.tools.idea.gradle.project.sync.setup.module.SyncLibraryRegistry)

Example 60 with Library

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

the class SyncLibraryRegistry method markAsUsed.

/**
   * Marks the given library as "used" by the project.
   *
   * @param library        the library that is being used by the project.
   * @param newBinaryPaths the library's binary paths. They may be different that the existing ones.
   */
public void markAsUsed(@NotNull Library library, @NotNull File... newBinaryPaths) {
    checkNotDisposed();
    String name = library.getName();
    if (name != null) {
        Library used = myProjectLibrariesByName.remove(name);
        if (used != null) {
            List<String> existingBinaryUrls = Lists.newArrayList(used.getUrls(CLASSES));
            boolean urlCountChanged = newBinaryPaths.length != existingBinaryUrls.size();
            List<String> newBinaryUrls = new ArrayList<>();
            for (File newBinaryPath : newBinaryPaths) {
                String newBinaryUrl = pathToIdeaUrl(newBinaryPath);
                existingBinaryUrls.remove(newBinaryUrl);
                newBinaryUrls.add(newBinaryUrl);
            }
            if (!existingBinaryUrls.isEmpty() || urlCountChanged) {
                // Library changed, we need to update binary paths.
                LibraryToUpdate libraryToUpdate = new LibraryToUpdate(used, newBinaryUrls);
                myLibrariesToUpdate.add(libraryToUpdate);
            }
        }
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)240 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)58 VirtualFile (com.intellij.openapi.vfs.VirtualFile)58 Module (com.intellij.openapi.module.Module)41 NotNull (org.jetbrains.annotations.NotNull)31 File (java.io.File)29 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)25 Nullable (org.jetbrains.annotations.Nullable)25 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)23 Project (com.intellij.openapi.project.Project)22 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)20 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)10 THashSet (gnu.trove.THashSet)9 IOException (java.io.IOException)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 LibrariesContainer (com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer)7