Search in sources :

Example 6 with ArtifactDependencySpec

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

the class AndroidAddLibraryDependencyAction method addDependency.

/**
   * Adds the given dependency to the project.
   *
   * @param project
   * @param buildModel
   * @param coordinateString
   */
private static void addDependency(@NotNull final Project project, @NotNull final GradleBuildModel buildModel, @NotNull String coordinateString) {
    GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(coordinateString);
    if (coordinate == null || coordinate.getArtifactId() == null) {
        return;
    }
    final ArtifactDependencySpec newDependency = new ArtifactDependencySpec(coordinate.getArtifactId(), coordinate.getGroupId(), coordinate.getRevision());
    WriteCommandAction.runWriteCommandAction(project, new Runnable() {

        @Override
        public void run() {
            buildModel.dependencies().addArtifact(CommonConfigurationNames.COMPILE, newDependency);
            buildModel.applyChanges();
        }
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

Example 7 with ArtifactDependencySpec

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

the class FreelineUtil method installFreeline.

private static void installFreeline(final Project project, final FreelineStatus status, final PsiFile psiFile, final GradleDependencyEntity dependencyEntity) {
    needReformatCode = false;
    CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    if (!status.isExistClasspath()) {
                        Collection<VirtualFile> collection = status.getGradleBuildFiles();
                        if (dependencyEntity != null) {
                            for (VirtualFile file : collection) {
                                GradleBuildModel model = GradleBuildModel.parseBuildFile(file, project);
                                List<ArtifactDependencyModel> artifactDependencyModels = model.buildscript().dependencies().artifacts();
                                for (ArtifactDependencyModel model1 : artifactDependencyModels) {
                                    ArtifactDependencyModelWrapper wrapper = new ArtifactDependencyModelWrapper(model1);
                                    if (wrapper.group().equals(Constant.ANDROID_GRADLE_TOOL_GROUP_NAME)) {
                                        ArtifactDependencySpec spec = new ArtifactDependencySpec(dependencyEntity.getArtifactId(), dependencyEntity.getGroupId(), dependencyEntity.getNewestReleaseVersion());
                                        model.buildscript().dependencies().addArtifact("classpath", spec);
                                        model.applyChanges();
                                        needReformatCode = true;
                                        status.setClasspathFile(file);
                                        break;
                                    }
                                }
                                if (status.isExistClasspath()) {
                                    break;
                                }
                            }
                        }
                    }
                    if (!status.isExistPlugin()) {
                        if (psiFile != null && psiFile instanceof GroovyFile) {
                            GradleUtil.applyPlugin(project, (GroovyFile) psiFile, Constant.FREELINE_PLUGIN_ID);
                        }
                    }
                }
            });
        }
    });
    if (needReformatCode && status.getClasspathFile() != null) {
        DocumentUtil.reformatCode(project, status.getClasspathFile());
    }
    LogUtil.d("Sync Project Finish, start download freeline.zip.");
    initFreeline(project);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

Example 8 with ArtifactDependencySpec

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

the class AddGradleDependencyTest method testAddJetbrainsAnnotationDependency.

@Test
public void testAddJetbrainsAnnotationDependency() throws IOException {
    guiTest.importSimpleApplication();
    EditorFixture editor = guiTest.ideFrame().getEditor().open("app/src/main/java/google/simpleapplication/MyActivity.java");
    editor.moveBetween("onCreate(", "Bundle savedInstanceState) {");
    editor.enterText("\n@NotNull ");
    editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
    editor.moveBetween("@Not", "Null ");
    editor.invokeQuickfixAction("Add 'annotations-java5' to classpath");
    guiTest.ideFrame().waitForGradleProjectSyncToFinish();
    editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
    GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
    ArtifactDependencySpec expected = new ArtifactDependencySpec("annotations-java5", "org.jetbrains", "15.0");
    appBuildModel.requireDependency(COMPILE, expected);
    // Undo the import statement first
    editor.invokeAction(UNDO);
    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 9 with ArtifactDependencySpec

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

the class AddGradleDependencyTest method testAddJUnitDependency.

@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddJUnitDependency() throws IOException {
    EditorFixture editor = guiTest.importSimpleApplication().getEditor().open("app/build.gradle").moveBetween("testCompile", "").invokeAction(DELETE_LINE).getIdeFrame().requestProjectSync().getEditor().open("app/src/test/java/google/simpleapplication/UnitTest.java").waitForCodeAnalysisHighlightCount(ERROR, 6).moveBetween("@", "Test").invokeQuickfixAction("Add 'JUnit4' to classpath").getIdeFrame().waitForGradleProjectSyncToFinish().getEditor().waitForCodeAnalysisHighlightCount(ERROR, 0);
    GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
    ArtifactDependencySpec expected = new ArtifactDependencySpec("junit", "junit", "4.12");
    appBuildModel.requireDependency(TEST_COMPILE, expected);
    verifyUndo(editor, 6);
}
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 10 with ArtifactDependencySpec

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

the class DefaultRecipeExecutor method addClasspath.

@Override
public void addClasspath(@NotNull String mavenUrl) {
    mavenUrl = mavenUrl.trim();
    myReferences.addClasspath(mavenUrl);
    ArtifactDependencySpec toBeAddedDependency = ArtifactDependencySpec.create(mavenUrl);
    if (toBeAddedDependency == null) {
        throw new RuntimeException(mavenUrl + " is not a valid classpath dependency");
    }
    Project project = myContext.getProject();
    File rootBuildFile = getGradleBuildFilePath(getBaseDirPath(project));
    if (project.isInitialized()) {
        GradleBuildModel buildModel = getBuildModel(rootBuildFile, project);
        DependenciesModel buildscriptDependencies = buildModel.buildscript().dependencies();
        ArtifactDependencyModel targetDependencyModel = null;
        for (ArtifactDependencyModel dependencyModel : buildscriptDependencies.artifacts(CLASSPATH_CONFIGURATION_NAME)) {
            if (toBeAddedDependency.equalsIgnoreVersion(ArtifactDependencySpec.create(dependencyModel))) {
                targetDependencyModel = dependencyModel;
            }
        }
        if (targetDependencyModel == null) {
            buildscriptDependencies.addArtifact(CLASSPATH_CONFIGURATION_NAME, toBeAddedDependency);
        } else {
            GradleVersion toBeAddedDependencyVersion = GradleVersion.parse(nullToEmpty(toBeAddedDependency.version));
            GradleVersion existingDependencyVersion = GradleVersion.parse(nullToEmpty(targetDependencyModel.version().value()));
            if (toBeAddedDependencyVersion.compareTo(existingDependencyVersion) > 0) {
                targetDependencyModel.setVersion(nullToEmpty(toBeAddedDependency.version));
            }
        }
        myIO.applyChanges(buildModel);
    } else {
        String destinationContents = rootBuildFile.exists() ? nullToEmpty(readTextFile(rootBuildFile)) : "";
        String result = myIO.mergeGradleFiles(formatClasspath(mavenUrl), destinationContents, project, "");
        try {
            myIO.writeFile(this, result, rootBuildFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    myNeedsGradleSync = true;
}
Also used : 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) IOException(java.io.IOException) GradleVersion(com.android.ide.common.repository.GradleVersion) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleBuildModel.parseBuildFile(com.android.tools.idea.gradle.dsl.model.GradleBuildModel.parseBuildFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) ArtifactDependencySpec(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)

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