Search in sources :

Example 1 with GradleModuleVersion

use of org.gradle.tooling.model.GradleModuleVersion in project intellij-community by JetBrains.

the class BaseGradleProjectResolverExtension method buildDependency.

@NotNull
private LibraryDependencyData buildDependency(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ownerModule, @NotNull IdeaSingleEntryLibraryDependency dependency, @NotNull DataNode<ProjectData> ideProject) throws IllegalStateException {
    File binaryPath = dependency.getFile();
    if (binaryPath == null) {
        throw new IllegalStateException(String.format("Can't parse external library dependency '%s'. Reason: it doesn't specify path to the binaries", dependency));
    }
    String libraryName;
    final GradleModuleVersion moduleVersion = dependency.getGradleModuleVersion();
    final LibraryLevel level;
    // Gradle API doesn't explicitly provide information about unresolved libraries (http://issues.gradle.org/browse/GRADLE-1995).
    // That's why we use this dirty hack here.
    boolean unresolved = binaryPath.getPath().startsWith(UNRESOLVED_DEPENDENCY_PREFIX);
    if (moduleVersion == null) {
        // use module library level if the dependency does not originate from a remote repository.
        level = LibraryLevel.MODULE;
        if (binaryPath.isFile()) {
            libraryName = FileUtil.getNameWithoutExtension(binaryPath);
        } else {
            libraryName = "";
        }
        if (unresolved) {
            // Gradle uses names like 'unresolved dependency - commons-collections commons-collections 3.2' for unresolved dependencies.
            libraryName = binaryPath.getPath().substring(UNRESOLVED_DEPENDENCY_PREFIX.length());
            int i = libraryName.indexOf(' ');
            if (i >= 0) {
                i = CharArrayUtil.shiftForward(libraryName, i + 1, " ");
            }
            if (i >= 0 && i < libraryName.length()) {
                int dependencyNameIndex = i;
                i = libraryName.indexOf(' ', dependencyNameIndex);
                if (i > 0) {
                    libraryName = String.format("%s-%s", libraryName.substring(dependencyNameIndex, i), libraryName.substring(i + 1));
                }
            }
        }
    } else {
        level = LibraryLevel.PROJECT;
        libraryName = String.format("%s:%s:%s", moduleVersion.getGroup(), moduleVersion.getName(), moduleVersion.getVersion());
        if (binaryPath.isFile()) {
            String libraryFileName = FileUtil.getNameWithoutExtension(binaryPath);
            final String mavenLibraryFileName = String.format("%s-%s", moduleVersion.getName(), moduleVersion.getVersion());
            if (!mavenLibraryFileName.equals(libraryFileName)) {
                Pattern pattern = Pattern.compile(moduleVersion.getName() + "-" + moduleVersion.getVersion() + "-(.*)");
                Matcher matcher = pattern.matcher(libraryFileName);
                if (matcher.matches()) {
                    final String classifier = matcher.group(1);
                    libraryName += (":" + classifier);
                } else {
                    final String artifactId = StringUtil.trimEnd(StringUtil.trimEnd(libraryFileName, moduleVersion.getVersion()), "-");
                    libraryName = String.format("%s:%s:%s", moduleVersion.getGroup(), artifactId, moduleVersion.getVersion());
                }
            }
        }
    }
    // add packaging type to distinguish different artifact dependencies with same groupId:artifactId:version
    if (StringUtil.isNotEmpty(libraryName) && !FileUtilRt.extensionEquals(binaryPath.getPath(), "jar")) {
        libraryName += (":" + FileUtilRt.getExtension(binaryPath.getPath()));
    }
    final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, libraryName, unresolved);
    if (!unresolved) {
        library.addPath(LibraryPathType.BINARY, binaryPath.getAbsolutePath());
    }
    File sourcePath = dependency.getSource();
    if (!unresolved && sourcePath != null) {
        library.addPath(LibraryPathType.SOURCE, sourcePath.getAbsolutePath());
    }
    if (!unresolved && sourcePath == null) {
        attachGradleSdkSources(gradleModule, binaryPath, library, resolverCtx);
    }
    File javadocPath = dependency.getJavadoc();
    if (!unresolved && javadocPath != null) {
        library.addPath(LibraryPathType.DOC, javadocPath.getAbsolutePath());
    }
    if (level == LibraryLevel.PROJECT) {
        linkProjectLibrary(ideProject, library);
    }
    return new LibraryDependencyData(ownerModule.getData(), library, level);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) File(java.io.File) GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GradleModuleVersion

use of org.gradle.tooling.model.GradleModuleVersion in project android by JetBrains.

the class PsJavaDependencyCollection method addDependencies.

private void addDependencies() {
    PsParsedDependencies parsedDependencies = myParent.getParsedDependencies();
    JavaModuleModel gradleModel = myParent.getGradleModel();
    for (JarLibraryDependency libraryDependency : gradleModel.getJarLibraryDependencies()) {
        GradleModuleVersion moduleVersion = libraryDependency.getModuleVersion();
        if (moduleVersion != null) {
            PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(moduleVersion);
            ArtifactDependencyModel parsed = parsedDependencies.findLibraryDependency(moduleVersion);
            PsLibraryJavaDependency dependency = new PsLibraryJavaDependency(myParent, spec, libraryDependency, parsed);
            myLibraryDependenciesBySpec.put(spec.toString(), dependency);
        }
    }
}
Also used : JavaModuleModel(com.android.tools.idea.gradle.project.model.JavaModuleModel) JarLibraryDependency(com.android.tools.idea.gradle.model.java.JarLibraryDependency) PsArtifactDependencySpec(com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) PsParsedDependencies(com.android.tools.idea.gradle.structure.model.PsParsedDependencies) GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion)

Example 3 with GradleModuleVersion

use of org.gradle.tooling.model.GradleModuleVersion in project android by JetBrains.

the class JarLibraryDependencyTest method testCopyWithResolvedDependency.

@Test
public void testCopyWithResolvedDependency() {
    FileStub binaryPath = new FileStub("fake.jar", true);
    FileStub sourcePath = new FileStub("fake-src.jar", true);
    FileStub javadocPath = new FileStub("fake-javadoc.jar", true);
    IdeaDependencyScope scope = mock(IdeaDependencyScope.class);
    GradleModuleVersion moduleVersion = mock(GradleModuleVersion.class);
    when(myOriginalDependency.getFile()).thenReturn(binaryPath);
    when(myOriginalDependency.getSource()).thenReturn(sourcePath);
    when(myOriginalDependency.getJavadoc()).thenReturn(javadocPath);
    when(myOriginalDependency.getScope()).thenReturn(scope);
    when(myOriginalDependency.getGradleModuleVersion()).thenReturn(moduleVersion);
    when(scope.getScope()).thenReturn("compile");
    JarLibraryDependency dependency = JarLibraryDependency.copy(myOriginalDependency);
    assertNotNull(dependency);
    assertEquals("fake", dependency.getName());
    assertEquals("compile", dependency.getScope());
    assertSame(binaryPath, dependency.getBinaryPath());
    assertSame(sourcePath, dependency.getSourcePath());
    assertSame(javadocPath, dependency.getJavadocPath());
    assertSame(moduleVersion, dependency.getModuleVersion());
    assertTrue(dependency.isResolved());
}
Also used : IdeaDependencyScope(org.gradle.tooling.model.idea.IdeaDependencyScope) GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion) Test(org.junit.Test)

Example 4 with GradleModuleVersion

use of org.gradle.tooling.model.GradleModuleVersion in project android by JetBrains.

the class JarLibraryDependencyTest method testCopyWithUnesolvedDependency.

@Test
public void testCopyWithUnesolvedDependency() {
    FileStub binaryPath = new FileStub("unresolved dependency - fake", true);
    GradleModuleVersion moduleVersion = mock(GradleModuleVersion.class);
    when(myOriginalDependency.getFile()).thenReturn(binaryPath);
    when(myOriginalDependency.getSource()).thenReturn(null);
    when(myOriginalDependency.getJavadoc()).thenReturn(null);
    when(myOriginalDependency.getScope()).thenReturn(null);
    when(myOriginalDependency.getGradleModuleVersion()).thenReturn(moduleVersion);
    JarLibraryDependency dependency = JarLibraryDependency.copy(myOriginalDependency);
    assertNotNull(dependency);
    assertEquals("fake", dependency.getName());
    assertNull(dependency.getScope());
    assertSame(binaryPath, dependency.getBinaryPath());
    assertNull(dependency.getSourcePath());
    assertNull(dependency.getJavadocPath());
    assertSame(moduleVersion, dependency.getModuleVersion());
    assertFalse(dependency.isResolved());
}
Also used : GradleModuleVersion(org.gradle.tooling.model.GradleModuleVersion) Test(org.junit.Test)

Aggregations

GradleModuleVersion (org.gradle.tooling.model.GradleModuleVersion)4 Test (org.junit.Test)2 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)1 JarLibraryDependency (com.android.tools.idea.gradle.model.java.JarLibraryDependency)1 JavaModuleModel (com.android.tools.idea.gradle.project.model.JavaModuleModel)1 PsArtifactDependencySpec (com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec)1 PsParsedDependencies (com.android.tools.idea.gradle.structure.model.PsParsedDependencies)1 File (java.io.File)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 IdeaDependencyScope (org.gradle.tooling.model.idea.IdeaDependencyScope)1 NotNull (org.jetbrains.annotations.NotNull)1