Search in sources :

Example 16 with ArtifactDependencyModel

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

the class PsJavaModule method addLibraryDependency.

public void addLibraryDependency(@NotNull String library, @NotNull List<String> scopesNames) {
    // Update/reset the "parsed" model.
    addLibraryDependencyToParsedModel(scopesNames, library);
    // Reset dependencies.
    myDependencyCollection = null;
    PsJavaDependencyCollection dependencyCollection = getOrCreateDependencyCollection();
    PsArtifactDependencySpec spec = PsArtifactDependencySpec.create(library);
    assert spec != null;
    PsParsedDependencies parsedDependencies = getParsedDependencies();
    List<ArtifactDependencyModel> matchingParsedDependencies = parsedDependencies.findLibraryDependencies(spec, null);
    for (ArtifactDependencyModel parsedDependency : matchingParsedDependencies) {
        dependencyCollection.addLibraryDependency(spec, parsedDependency);
    }
    fireLibraryDependencyAddedEvent(spec);
    setModified(true);
}
Also used : 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)

Example 17 with ArtifactDependencyModel

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

the class PsParsedDependencies method reset.

void reset(@Nullable GradleBuildModel parsedModel) {
    myParsedArtifactDependencies.clear();
    myParsedModuleDependencies.clear();
    if (parsedModel != null) {
        ApplicationManager.getApplication().runReadAction(() -> {
            for (DependencyModel parsedDependency : parsedModel.dependencies().all()) {
                if (parsedDependency instanceof ArtifactDependencyModel) {
                    ArtifactDependencyModel artifact = (ArtifactDependencyModel) parsedDependency;
                    myParsedArtifactDependencies.put(createIdFrom(artifact), artifact);
                } else if (parsedDependency instanceof ModuleDependencyModel) {
                    ModuleDependencyModel module = (ModuleDependencyModel) parsedDependency;
                    myParsedModuleDependencies.put(module.path().value(), module);
                }
            }
        });
    }
}
Also used : ModuleDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependencyModel) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) ModuleDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel)

Example 18 with ArtifactDependencyModel

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel 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 19 with ArtifactDependencyModel

use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel 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 20 with ArtifactDependencyModel

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

the class AndroidAddLibraryDependencyAction method findAllDependencies.

/**
   * Finds all the "extras repository" dependencies that haven't been already added to the project.
   */
@NotNull
private static ImmutableCollection<String> findAllDependencies(@NotNull GradleBuildModel buildModel) {
    HashSet<String> existingDependencies = Sets.newHashSet();
    for (ArtifactDependencyModel dependency : buildModel.dependencies().artifacts()) {
        existingDependencies.add(dependency.group().value() + ":" + dependency.name().value());
    }
    ImmutableList.Builder<String> dependenciesBuilder = ImmutableList.builder();
    RepositoryUrlManager repositoryUrlManager = RepositoryUrlManager.get();
    for (SupportLibrary library : SupportLibrary.values()) {
        // Coordinate for any version available
        GradleCoordinate libraryCoordinate = library.getGradleCoordinate("+");
        // Get from the library coordinate only the group and artifactId to check if we have already added it
        if (!existingDependencies.contains(libraryCoordinate.getId())) {
            GradleCoordinate coordinate = repositoryUrlManager.resolveDynamicCoordinate(libraryCoordinate, buildModel.getProject());
            if (coordinate != null) {
                dependenciesBuilder.add(coordinate.toString());
            }
        }
    }
    return dependenciesBuilder.build();
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ImmutableList(com.google.common.collect.ImmutableList) RepositoryUrlManager(com.android.tools.idea.templates.RepositoryUrlManager) SupportLibrary(com.android.tools.idea.templates.SupportLibrary) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)28 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)17 DependenciesModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)12 NotNull (org.jetbrains.annotations.NotNull)7 Project (com.intellij.openapi.project.Project)6 GradleVersion (com.android.ide.common.repository.GradleVersion)5 PsArtifactDependencySpec (com.android.tools.idea.gradle.structure.model.PsArtifactDependencySpec)5 File (java.io.File)5 ArtifactDependencySpec (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec)4 PsParsedDependencies (com.android.tools.idea.gradle.structure.model.PsParsedDependencies)4 RepositoryUrlManager (com.android.tools.idea.templates.RepositoryUrlManager)4 Module (com.intellij.openapi.module.Module)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GradleCoordinate (com.android.ide.common.repository.GradleCoordinate)3 ExpectedArtifactDependency (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyTest.ExpectedArtifactDependency)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 Nullable (org.jetbrains.annotations.Nullable)3 DependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.DependencyModel)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 PsProject (com.android.tools.idea.gradle.structure.model.PsProject)2