Search in sources :

Example 16 with DependenciesModel

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

the class AndroidGradleJavaProjectModelModifier method addExternalLibraryDependency.

@Nullable
private static Promise<Void> addExternalLibraryDependency(@NotNull Collection<Module> modules, @NotNull ArtifactDependencySpec dependencySpec, @NotNull DependencyScope scope) {
    Module firstModule = Iterables.getFirst(modules, null);
    if (firstModule == null) {
        return null;
    }
    Project project = firstModule.getProject();
    VirtualFile openedFile = FileEditorManagerEx.getInstanceEx(firstModule.getProject()).getCurrentFile();
    List<GradleBuildModel> buildModelsToUpdate = Lists.newArrayList();
    for (Module module : modules) {
        GradleBuildModel buildModel = GradleBuildModel.get(module);
        if (buildModel == null) {
            return null;
        }
        String configurationName = getConfigurationName(module, scope, openedFile);
        DependenciesModel dependencies = buildModel.dependencies();
        dependencies.addArtifact(configurationName, dependencySpec);
        buildModelsToUpdate.add(buildModel);
    }
    new WriteCommandAction(project, "Add Gradle Library Dependency") {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            for (GradleBuildModel buildModel : buildModelsToUpdate) {
                buildModel.applyChanges();
            }
            registerUndoAction(project);
        }
    }.execute();
    return requestProjectSync(project);
}
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) Module(com.intellij.openapi.module.Module) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with DependenciesModel

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

the class GradleDependencyManager method addDependencies.

private static void addDependencies(@NotNull GradleBuildModel buildModel, @NotNull Module module, @NotNull List<GradleCoordinate> coordinates) {
    ModuleRootModificationUtil.updateModel(module, model -> {
        DependenciesModel dependenciesModel = buildModel.dependencies();
        for (GradleCoordinate coordinate : coordinates) {
            dependenciesModel.addArtifact(COMPILE, coordinate.toString());
        }
        buildModel.applyChanges();
    });
}
Also used : GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

Example 18 with DependenciesModel

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

Example 19 with DependenciesModel

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

the class GradleBuildModelFixture method requireDependency.

public void requireDependency(@NotNull String configurationName, @NotNull ArtifactDependencySpec expected) {
    DependenciesModel dependenciesModel = myTarget.dependencies();
    for (ArtifactDependencyModel dependency : dependenciesModel.artifacts()) {
        ArtifactDependencySpec actual = ArtifactDependencySpec.create(dependency);
        if (configurationName.equals(dependency.configurationName()) && expected.equals(actual)) {
            return;
        }
    }
    fail("Failed to find dependency '" + expected.compactNotation() + "'");
}
Also used : 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 20 with DependenciesModel

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

the class GradleBuildModelFixture method requireDependency.

public void requireDependency(@NotNull ExpectedModuleDependency expected) {
    DependenciesModel dependenciesModel = myTarget.dependencies();
    for (final ModuleDependencyModel dependency : dependenciesModel.modules()) {
        String path = GuiQuery.getNonNull(() -> dependency.path().value());
        if (expected.path.equals(path) && expected.configurationName.equals(dependency.configurationName())) {
            return;
        }
    }
    fail("Failed to find dependency '" + expected.path + "'");
}
Also used : ModuleDependencyModel(com.android.tools.idea.gradle.dsl.model.dependencies.ModuleDependencyModel) DependenciesModel(com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel)

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