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