Search in sources :

Example 1 with DependenciesModel

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

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

the class GradleOperations method containsAllDependencies.

@Override
public boolean containsAllDependencies(@NotNull Module module, @NotNull DeveloperServiceMetadata metadata) {
    // Consider ourselves installed if this service's dependencies are already found in the current
    // module.
    // TODO: Flesh this simplistic approach out more. We would like to have a way to say a service
    // isn't installed even if its dependency happens to be added to the project. For example,
    // multiple services might share a dependency but have additional settings that indicate some
    // are installed and others aren't.
    List<String> moduleDependencyNames = Lists.newArrayList();
    GradleBuildModel buildModel = GradleBuildModel.get(module);
    if (buildModel != null) {
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
                String name = dependency.name().value();
                moduleDependencyNames.add(name);
            }
        }
    }
    boolean allDependenciesFound = true;
    for (String serviceDependency : metadata.getDependencies()) {
        boolean thisDependencyFound = false;
        for (String moduleDependencyName : moduleDependencyNames) {
            if (serviceDependency.contains(moduleDependencyName)) {
                thisDependencyFound = true;
                break;
            }
        }
        if (!thisDependencyFound) {
            allDependenciesFound = false;
            break;
        }
    }
    return allDependenciesFound;
}
Also used : 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 3 with DependenciesModel

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

the class GradleBuildModel method dependencies.

public DependenciesModel dependencies() {
    DependenciesDslElement dependenciesDslElement = myGradleDslFile.getPropertyElement(DEPENDENCIES_BLOCK_NAME, DependenciesDslElement.class);
    if (dependenciesDslElement == null) {
        dependenciesDslElement = new DependenciesDslElement(myGradleDslFile);
        myGradleDslFile.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)

Example 4 with DependenciesModel

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

the class GradleDependencyManager method updateDependencies.

private static void updateDependencies(@NotNull GradleBuildModel buildModel, @NotNull Module module, @NotNull List<GradleCoordinate> coordinates) {
    ModuleRootModificationUtil.updateModel(module, model -> {
        DependenciesModel dependenciesModel = buildModel.dependencies();
        for (GradleCoordinate gc : coordinates) {
            List<ArtifactDependencyModel> artifacts = Lists.newArrayList(dependenciesModel.artifacts());
            for (ArtifactDependencyModel m : artifacts) {
                if (gc.getGroupId() != null && gc.getGroupId().equals(m.group().value()) && gc.getArtifactId() != null && gc.getArtifactId().equals(m.name().value()) && !gc.getRevision().equals(m.version().value())) {
                    dependenciesModel.remove(m);
                    dependenciesModel.addArtifact(m.configurationName(), gc.toString());
                }
            }
        }
        buildModel.applyChanges();
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ArtifactDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

Example 5 with DependenciesModel

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

the class GradleProjectDependencyParser method parse.

@NotNull
private static Set<String> parse(@NotNull VirtualFile moduleRoot, @NotNull Project project) {
    VirtualFile buildFile = moduleRoot.findChild(FN_BUILD_GRADLE);
    if (buildFile == null) {
        return Collections.emptySet();
    } else {
        Set<String> result = Sets.newHashSet();
        GradleBuildModel buildModel = parseBuildFile(buildFile, project);
        DependenciesModel dependenciesModel = buildModel.dependencies();
        if (dependenciesModel != null) {
            for (ModuleDependencyModel dependency : dependenciesModel.modules()) {
                String modulePath = dependency.path().value();
                result.add(modulePath);
            }
        }
        return result;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) ModuleDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel) 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