Search in sources :

Example 6 with DependenciesModel

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

the class InferSupportAnnotationsAction method checkModules.

// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
    Set<Module> modulesWithoutAnnotations = new HashSet<>();
    Set<Module> modulesWithLowVersion = new HashSet<>();
    for (Module module : modules.keySet()) {
        AndroidModuleInfo info = AndroidModuleInfo.get(module);
        if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
            modulesWithLowVersion.add(module);
        }
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
            continue;
        }
        boolean dependencyFound = false;
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
                String notation = dependency.compactNotation().value();
                if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
                    dependencyFound = true;
                    break;
                }
            }
        }
        if (!dependencyFound) {
            modulesWithoutAnnotations.add(module);
        }
    }
    if (!modulesWithLowVersion.isEmpty()) {
        Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
        return false;
    }
    if (modulesWithoutAnnotations.isEmpty()) {
        return true;
    }
    String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
    int count = modulesWithoutAnnotations.size();
    String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
    if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
        LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
        try {
            new WriteCommandAction(project, ADD_DEPENDENCY) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    RepositoryUrlManager manager = RepositoryUrlManager.get();
                    String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
                    for (Module module : modulesWithoutAnnotations) {
                        addDependency(module, annotationsLibraryCoordinate);
                    }
                    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
                    GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {

                        @Override
                        public void syncSucceeded(@NotNull Project project) {
                            restartAnalysis(project, scope);
                        }
                    });
                }
            }.execute();
        } finally {
            action.finish();
        }
    }
    return false;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) LocalHistoryAction(com.intellij.history.LocalHistoryAction) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Module(com.intellij.openapi.module.Module)

Example 7 with DependenciesModel

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

the class BuildScriptModelTest method testParseDependencies.

public void testParseDependencies() throws IOException {
    String text = "buildscript {\n" + "  dependencies {\n" + "    classpath 'com.android.tools.build:gradle:2.0.0-alpha2'\n" + "  }\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    DependenciesModel dependenciesModel = buildModel.buildscript().dependencies();
    List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(1);
    ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
    expected.assertMatches(dependencies.get(0));
}
Also used : ExpectedArtifactDependency(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyTest.ExpectedArtifactDependency) 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)

Example 8 with DependenciesModel

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

the class BuildScriptModelTest method testEditDependency.

public void testEditDependency() throws IOException {
    String text = "buildscript {\n" + "  dependencies {\n" + "    classpath 'com.android.tools.build:gradle:2.0.0-alpha2'\n" + "  }\n" + "}";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    DependenciesModel dependenciesModel = buildModel.buildscript().dependencies();
    List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(1);
    ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
    ArtifactDependencyModel actual = dependencies.get(0);
    expected.assertMatches(actual);
    actual.setVersion("2.0.1");
    expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.1");
    expected.assertMatches(actual);
    assertTrue(buildModel.isModified());
    applyChanges(buildModel);
    assertFalse(buildModel.isModified());
    buildModel.reparse();
    dependencies = buildModel.buildscript().dependencies().artifacts();
    assertThat(dependencies).hasSize(1);
    expected.assertMatches(dependencies.get(0));
}
Also used : ExpectedArtifactDependency(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyTest.ExpectedArtifactDependency) 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)

Example 9 with DependenciesModel

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

the class BuildScriptModelTest method testAddDependency.

public void testAddDependency() throws IOException {
    String text = "";
    writeToBuildFile(text);
    GradleBuildModel buildModel = getGradleBuildModel();
    BuildScriptModel buildScriptModel = buildModel.buildscript();
    DependenciesModel dependenciesModel = buildScriptModel.dependencies();
    assertFalse(buildScriptModel.hasValidPsiElement());
    assertFalse(dependenciesModel.hasValidPsiElement());
    assertThat(dependenciesModel.artifacts()).isEmpty();
    dependenciesModel.addArtifact("classpath", "com.android.tools.build:gradle:2.0.0-alpha2");
    List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(1);
    ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
    expected.assertMatches(dependencies.get(0));
    assertTrue(buildModel.isModified());
    applyChanges(buildModel);
    assertFalse(buildModel.isModified());
    buildModel.reparse();
    buildScriptModel = buildModel.buildscript();
    dependenciesModel = buildScriptModel.dependencies();
    assertTrue(buildScriptModel.hasValidPsiElement());
    assertTrue(dependenciesModel.hasValidPsiElement());
    dependencies = dependenciesModel.artifacts();
    assertThat(dependencies).hasSize(1);
    expected.assertMatches(dependencies.get(0));
}
Also used : ExpectedArtifactDependency(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyTest.ExpectedArtifactDependency) 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)

Example 10 with DependenciesModel

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

the class BuildScriptModel method dependencies.

@NotNull
public DependenciesModel dependencies() {
    DependenciesDslElement dependenciesDslElement = myDslElement.getPropertyElement(DEPENDENCIES_BLOCK_NAME, DependenciesDslElement.class);
    if (dependenciesDslElement == null) {
        dependenciesDslElement = new DependenciesDslElement(myDslElement);
        myDslElement.setNewElement(DEPENDENCIES_BLOCK_NAME, dependenciesDslElement);
    }
    return new DependenciesModel(dependenciesDslElement);
}
Also used : DependenciesDslElement(com.android.tools.idea.gradle.dsl.parser.dependencies.DependenciesDslElement) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)20 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)13 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)12 Project (com.intellij.openapi.project.Project)6 NotNull (org.jetbrains.annotations.NotNull)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Result (com.intellij.openapi.application.Result)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArtifactDependencySpec (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)3 ExpectedArtifactDependency (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyTest.ExpectedArtifactDependency)3 Module (com.intellij.openapi.module.Module)3 GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)2 GradleVersion (com.android.ide.common.repository.GradleVersion)2 ModuleDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel)2 DependenciesDslElement (com.android.tools.idea.gradle.dsl.parser.dependencies.DependenciesDslElement)2 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)2 RepositoryUrlManager (com.android.tools.idea.templates.RepositoryUrlManager)2 LocalHistoryAction (com.intellij.history.LocalHistoryAction)2 File (java.io.File)2 Nullable (org.jetbrains.annotations.Nullable)2