Search in sources :

Example 11 with DependenciesModel

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

the class PsModule method addLibraryDependencyToParsedModel.

protected void addLibraryDependencyToParsedModel(@NotNull List<String> configurationNames, @NotNull String compactNotation) {
    GradleBuildModel parsedModel = getParsedModel();
    if (parsedModel != null) {
        DependenciesModel dependencies = parsedModel.dependencies();
        configurationNames.forEach(configurationName -> dependencies.addArtifact(configurationName, compactNotation));
        getParsedDependencies().reset(getParsedModel());
    }
}
Also used : GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

Example 12 with DependenciesModel

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

the class AndroidPluginInfo method searchInBuildFiles.

@NotNull
private static BuildFileSearchResult searchInBuildFiles(@NotNull Project project, boolean searchForAppModule) {
    BuildFileSearchResult result = new BuildFileSearchResult();
    BuildFileProcessor.getInstance().processRecursively(project, buildModel -> {
        boolean keepSearchingForAppModule = searchForAppModule && result.appVirtualFile == null;
        if (keepSearchingForAppModule) {
            List<String> pluginIds = getValues(buildModel.appliedPlugins());
            for (AndroidPluginGeneration generation : AndroidPluginGeneration.values()) {
                if (generation.isApplicationPluginIdIn(pluginIds)) {
                    result.appVirtualFile = buildModel.getVirtualFile();
                    result.pluginGeneration = generation;
                    keepSearchingForAppModule = false;
                    break;
                }
            }
        }
        boolean keepSearchingForPluginVersion = result.pluginVersion == null;
        if (keepSearchingForPluginVersion) {
            DependenciesModel dependencies = buildModel.buildscript().dependencies();
            for (ArtifactDependencyModel dependency : dependencies.artifacts(CLASSPATH)) {
                for (AndroidPluginGeneration generation : AndroidPluginGeneration.values()) {
                    if (generation.isAndroidPlugin(dependency.name().value(), dependency.group().value())) {
                        String version = dependency.version().value();
                        if (isNotEmpty(version)) {
                            result.pluginVirtualFile = buildModel.getVirtualFile();
                            result.pluginVersion = version;
                        }
                        keepSearchingForPluginVersion = false;
                        break;
                    }
                }
                if (!keepSearchingForPluginVersion) {
                    break;
                }
            }
        }
        return keepSearchingForAppModule || keepSearchingForPluginVersion;
    });
    return result;
}
Also used : ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with DependenciesModel

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

the class AndroidPluginVersionUpdater method updatePluginVersion.

/**
   * Updates the plugin version and, optionally, the Gradle version used by the project.
   *
   * @param pluginVersion the plugin version to update to.
   * @param gradleVersion the version of Gradle to update to (optional.)
   * @return the result of the update operation.
   */
@NotNull
public UpdateResult updatePluginVersion(@NotNull GradleVersion pluginVersion, @Nullable GradleVersion gradleVersion) {
    List<GradleBuildModel> modelsToUpdate = Lists.newArrayList();
    BuildFileProcessor.getInstance().processRecursively(myProject, buildModel -> {
        DependenciesModel dependencies = buildModel.buildscript().dependencies();
        for (ArtifactDependencyModel dependency : dependencies.artifacts(CLASSPATH)) {
            String artifactId = dependency.name().value();
            String groupId = dependency.group().value();
            if (AndroidPluginGeneration.find(artifactId, groupId) != null) {
                String versionValue = dependency.version().value();
                if (isEmpty(versionValue) || pluginVersion.compareTo(versionValue) != 0) {
                    dependency.setVersion(pluginVersion.toString());
                    modelsToUpdate.add(buildModel);
                }
                break;
            }
        }
        return true;
    });
    UpdateResult result = new UpdateResult();
    boolean updateModels = !modelsToUpdate.isEmpty();
    if (updateModels) {
        try {
            runWriteCommandAction(myProject, new ThrowableComputable<Void, RuntimeException>() {

                @Override
                public Void compute() {
                    for (GradleBuildModel buildModel : modelsToUpdate) {
                        buildModel.applyChanges();
                    }
                    result.pluginVersionUpdated();
                    return null;
                }
            });
        } catch (Throwable e) {
            result.setPluginVersionUpdateError(e);
        }
    }
    if (gradleVersion != null) {
        String basePath = myProject.getBasePath();
        if (basePath != null) {
            try {
                File wrapperPropertiesFilePath = getDefaultPropertiesFilePath(new File(basePath));
                GradleWrapper gradleWrapper = GradleWrapper.get(wrapperPropertiesFilePath);
                String current = gradleWrapper.getGradleVersion();
                GradleVersion parsedCurrent = null;
                if (current != null) {
                    parsedCurrent = GradleVersion.tryParse(current);
                }
                if (parsedCurrent != null && !isSupportedGradleVersion(parsedCurrent)) {
                    gradleWrapper.updateDistributionUrl(gradleVersion.toString());
                    result.gradleVersionUpdated();
                }
            } catch (Throwable e) {
                result.setGradleVersionUpdateError(e);
            }
        }
    }
    return result;
}
Also used : ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) GradleVersion(com.android.ide.common.repository.GradleVersion) GradleUtil.isSupportedGradleVersion(com.android.tools.idea.gradle.util.GradleUtil.isSupportedGradleVersion) File(java.io.File) GradleWrapper(com.android.tools.idea.gradle.util.GradleWrapper) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with DependenciesModel

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

the class AndroidInferNullityAnnotationAction 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) {
            LOG.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 Nullity Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Nullity 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 15 with DependenciesModel

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

the class AndroidGradleJavaProjectModelModifier method addModuleDependency.

@Nullable
@Override
public Promise<Void> addModuleDependency(@NotNull Module from, @NotNull Module to, @NotNull DependencyScope scope) {
    Project project = from.getProject();
    VirtualFile openedFile = FileEditorManagerEx.getInstanceEx(from.getProject()).getCurrentFile();
    String gradlePath = getGradlePath(to);
    GradleBuildModel buildModel = GradleBuildModel.get(from);
    if (buildModel != null && gradlePath != null) {
        DependenciesModel dependencies = buildModel.dependencies();
        String configurationName = getConfigurationName(from, scope, openedFile);
        dependencies.addModule(configurationName, gradlePath, null);
        new WriteCommandAction(project, "Add Gradle Module Dependency") {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                buildModel.applyChanges();
                registerUndoAction(project);
            }
        }.execute();
        return requestProjectSync(project);
    }
    if ((buildModel == null) ^ (gradlePath == null)) {
        // If one of them is gradle module and one of them are not, reject since this is invalid dependency
        return Promises.rejectedPromise();
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

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