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);
}
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();
});
}
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;
}
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() + "'");
}
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 + "'");
}
Aggregations