Search in sources :

Example 1 with ArtifactDependencySpec

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.

the class AndroidGradleJavaProjectModelModifier method findNewExternalDependencyByExaminingPath.

/**
   * Gradle dependencies are stored in following path:  xxx/:groupId/:artifactId/:version/xxx/:artifactId-:version.jar
   * therefor, if we can't get the artifact information from model, then try to extract from path.
   */
@Nullable
private static ArtifactDependencySpec findNewExternalDependencyByExaminingPath(@NotNull Library library) {
    VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
    if (files.length == 0) {
        return null;
    }
    File file = virtualToIoFile(files[0]);
    String libraryName = library.getName();
    if (libraryName == null) {
        return null;
    }
    List<String> pathSegments = splitPath(file.getPath());
    for (int i = 1; i < pathSegments.size() - 2; i++) {
        if (libraryName.startsWith(pathSegments.get(i))) {
            String groupId = pathSegments.get(i - 1);
            String artifactId = pathSegments.get(i);
            String version = pathSegments.get(i + 1);
            if (libraryName.endsWith(version)) {
                return new ArtifactDependencySpec(artifactId, groupId, version);
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ArtifactDependencySpec

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.

the class AndroidGradleJavaProjectModelModifier method findNewExternalDependency.

@Nullable
private static ArtifactDependencySpec findNewExternalDependency(@NotNull Library library, @NotNull AndroidModuleModel androidModel) {
    GradleVersion modelVersion = androidModel.getModelVersion();
    JavaLibrary matchedLibrary = null;
    for (BaseArtifact testArtifact : androidModel.getTestArtifactsInSelectedVariant()) {
        matchedLibrary = findMatchedLibrary(library, testArtifact, modelVersion);
        if (matchedLibrary != null) {
            break;
        }
    }
    if (matchedLibrary == null) {
        Variant selectedVariant = androidModel.getSelectedVariant();
        matchedLibrary = findMatchedLibrary(library, selectedVariant.getMainArtifact(), modelVersion);
    }
    if (matchedLibrary == null) {
        return null;
    }
    // TODO use getRequestedCoordinates once the interface is fixed.
    MavenCoordinates coordinates = matchedLibrary.getResolvedCoordinates();
    if (coordinates == null) {
        return null;
    }
    return new ArtifactDependencySpec(coordinates.getArtifactId(), coordinates.getGroupId(), coordinates.getVersion());
}
Also used : GradleVersion(com.android.ide.common.repository.GradleVersion) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ArtifactDependencySpec

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.

the class GradleOperations method removeDependencies.

@Override
public void removeDependencies(@NotNull Module module, @NotNull DeveloperServiceMetadata metadata) {
    final GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (buildModel != null) {
        boolean dependenciesChanged = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
                ArtifactDependencySpec spec = ArtifactDependencySpec.create(dependency);
                for (String dependencyValue : metadata.getDependencies()) {
                    if (spec.equals(ArtifactDependencySpec.create(dependencyValue))) {
                        dependenciesModel.remove(dependency);
                        dependenciesChanged = true;
                        break;
                    }
                }
            }
        }
        final Project project = module.getProject();
        if (dependenciesChanged) {
            new WriteCommandAction.Simple(project, "Uninstall " + metadata.getName()) {

                @Override
                public void run() {
                    buildModel.applyChanges();
                }
            }.execute();
        }
        GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, null);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

Example 4 with ArtifactDependencySpec

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.

the class AddGradleDependencyTest method testAddLibDependencyDeclaredInJavaProject.

@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddLibDependencyDeclaredInJavaProject() throws IOException {
    guiTest.importProjectAndWaitForProjectSyncToFinish("MultiModule");
    GradleBuildModelFixture library3BuildModel = guiTest.ideFrame().parseBuildFileForModule("library3");
    ArtifactDependencySpec gson = new ArtifactDependencySpec("gson", "com.google.code.gson", "2.4");
    library3BuildModel.getTarget().dependencies().addArtifact(COMPILE, gson);
    library3BuildModel.applyChanges();
    guiTest.ideFrame().requestProjectSync().waitForGradleProjectSyncToFinish();
    EditorFixture editor = guiTest.ideFrame().getEditor().open("app/src/main/java/com/android/multimodule/MainActivity.java");
    String classToImport = "com.google.gson.Gson";
    addImport(editor, classToImport);
    editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
    moveCaretToClassName(editor, classToImport);
    editor.invokeQuickfixAction("Add library 'gson-2.4' to classpath");
    guiTest.ideFrame().waitForGradleProjectSyncToFinish();
    editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
    GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
    appBuildModel.requireDependency(COMPILE, gson);
    verifyUndo(editor, 1);
}
Also used : EditorFixture(com.android.tools.idea.tests.gui.framework.fixture.EditorFixture) GradleBuildModelFixture(com.android.tools.idea.tests.gui.framework.fixture.gradle.GradleBuildModelFixture) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec) Test(org.junit.Test)

Example 5 with ArtifactDependencySpec

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.

the class AddGradleDependencyTest method testAddLibDependencyDeclaredInAndroidProject.

@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddLibDependencyDeclaredInAndroidProject() throws IOException {
    guiTest.importProjectAndWaitForProjectSyncToFinish("MultiModule");
    GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
    ArtifactDependencySpec gson = new ArtifactDependencySpec("gson", "com.google.code.gson", "2.4");
    appBuildModel.getTarget().dependencies().addArtifact(COMPILE, gson);
    appBuildModel.applyChanges();
    guiTest.ideFrame().requestProjectSync().waitForGradleProjectSyncToFinish();
    EditorFixture editor = guiTest.ideFrame().getEditor().open("library3/src/main/java/com/example/MyLibrary.java");
    String classToImport = "com.google.gson.Gson";
    addImport(editor, classToImport);
    editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
    moveCaretToClassName(editor, classToImport);
    editor.invokeQuickfixAction("Add library 'gson-2.4' to classpath");
    guiTest.ideFrame().waitForGradleProjectSyncToFinish();
    editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
    verifyUndo(editor, 1);
}
Also used : EditorFixture(com.android.tools.idea.tests.gui.framework.fixture.EditorFixture) GradleBuildModelFixture(com.android.tools.idea.tests.gui.framework.fixture.gradle.GradleBuildModelFixture) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec) Test(org.junit.Test)

Aggregations

ArtifactDependencySpec (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)11 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)4 EditorFixture (com.android.tools.idea.tests.gui.framework.fixture.EditorFixture)4 GradleBuildModelFixture (com.android.tools.idea.tests.gui.framework.fixture.gradle.GradleBuildModelFixture)4 Test (org.junit.Test)4 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)3 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GradleVersion (com.android.ide.common.repository.GradleVersion)2 Project (com.intellij.openapi.project.Project)2 File (java.io.File)2 Nullable (org.jetbrains.annotations.Nullable)2 GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)1 GradleBuildModel.parseBuildFile (com.android.tools.idea.gradle.dsl.model.GradleBuildModel.parseBuildFile)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1 IOException (java.io.IOException)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1