use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class InferSupportAnnotationsAction method checkModules.
// For Android we need to check SDK version and possibly update the gradle project file
protected boolean checkModules(@NotNull Project project, @NotNull AnalysisScope scope, @NotNull Map<Module, PsiFile> modules) {
Set<Module> modulesWithoutAnnotations = new HashSet<>();
Set<Module> modulesWithLowVersion = new HashSet<>();
for (Module module : modules.keySet()) {
AndroidModuleInfo info = AndroidModuleInfo.get(module);
if (info != null && info.getBuildSdkVersion() != null && info.getBuildSdkVersion().getFeatureLevel() < MIN_SDK_WITH_NULLABLE) {
modulesWithLowVersion.add(module);
}
GradleBuildModel buildModel = GradleBuildModel.get(module);
if (buildModel == null) {
Logger.getInstance(InferSupportAnnotationsAction.class).warn("Unable to find Gradle build model for module " + module.getModuleFilePath());
continue;
}
boolean dependencyFound = false;
DependenciesModel dependenciesModel = buildModel.dependencies();
if (dependenciesModel != null) {
for (ArtifactDependencyModel dependency : dependenciesModel.artifacts(COMPILE)) {
String notation = dependency.compactNotation().value();
if (notation.startsWith(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.SUPPORT_LIB_ARTIFACT) || notation.startsWith(SdkConstants.ANNOTATIONS_LIB_ARTIFACT)) {
dependencyFound = true;
break;
}
}
}
if (!dependencyFound) {
modulesWithoutAnnotations.add(module);
}
}
if (!modulesWithLowVersion.isEmpty()) {
Messages.showErrorDialog(project, String.format("Infer Support Annotations requires the project sdk level be set to %1$d or greater.", MIN_SDK_WITH_NULLABLE), "Infer Support Annotations");
return false;
}
if (modulesWithoutAnnotations.isEmpty()) {
return true;
}
String moduleNames = StringUtil.join(modulesWithoutAnnotations, Module::getName, ", ");
int count = modulesWithoutAnnotations.size();
String message = String.format("The %1$s %2$s %3$sn't refer to the existing '%4$s' library with Android nullity annotations. \n\n" + "Would you like to add the %5$s now?", pluralize("module", count), moduleNames, count > 1 ? "do" : "does", SupportLibrary.SUPPORT_ANNOTATIONS.getArtifactId(), pluralize("dependency", count));
if (Messages.showOkCancelDialog(project, message, "Infer Nullity Annotations", Messages.getErrorIcon()) == Messages.OK) {
LocalHistoryAction action = LocalHistory.getInstance().startAction(ADD_DEPENDENCY);
try {
new WriteCommandAction(project, ADD_DEPENDENCY) {
@Override
protected void run(@NotNull Result result) throws Throwable {
RepositoryUrlManager manager = RepositoryUrlManager.get();
String annotationsLibraryCoordinate = manager.getLibraryStringCoordinate(SupportLibrary.SUPPORT_ANNOTATIONS, true);
for (Module module : modulesWithoutAnnotations) {
addDependency(module, annotationsLibraryCoordinate);
}
GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
GradleSyncInvoker.getInstance().requestProjectSync(project, request, new GradleSyncListener.Adapter() {
@Override
public void syncSucceeded(@NotNull Project project) {
restartAnalysis(project, scope);
}
});
}
}.execute();
} finally {
action.finish();
}
}
return false;
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class BuildScriptModelTest method testParseDependencies.
public void testParseDependencies() throws IOException {
String text = "buildscript {\n" + " dependencies {\n" + " classpath 'com.android.tools.build:gradle:2.0.0-alpha2'\n" + " }\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
DependenciesModel dependenciesModel = buildModel.buildscript().dependencies();
List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
assertThat(dependencies).hasSize(1);
ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
expected.assertMatches(dependencies.get(0));
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class BuildScriptModelTest method testEditDependency.
public void testEditDependency() throws IOException {
String text = "buildscript {\n" + " dependencies {\n" + " classpath 'com.android.tools.build:gradle:2.0.0-alpha2'\n" + " }\n" + "}";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
DependenciesModel dependenciesModel = buildModel.buildscript().dependencies();
List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
assertThat(dependencies).hasSize(1);
ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
ArtifactDependencyModel actual = dependencies.get(0);
expected.assertMatches(actual);
actual.setVersion("2.0.1");
expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.1");
expected.assertMatches(actual);
assertTrue(buildModel.isModified());
applyChanges(buildModel);
assertFalse(buildModel.isModified());
buildModel.reparse();
dependencies = buildModel.buildscript().dependencies().artifacts();
assertThat(dependencies).hasSize(1);
expected.assertMatches(dependencies.get(0));
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class BuildScriptModelTest method testAddDependency.
public void testAddDependency() throws IOException {
String text = "";
writeToBuildFile(text);
GradleBuildModel buildModel = getGradleBuildModel();
BuildScriptModel buildScriptModel = buildModel.buildscript();
DependenciesModel dependenciesModel = buildScriptModel.dependencies();
assertFalse(buildScriptModel.hasValidPsiElement());
assertFalse(dependenciesModel.hasValidPsiElement());
assertThat(dependenciesModel.artifacts()).isEmpty();
dependenciesModel.addArtifact("classpath", "com.android.tools.build:gradle:2.0.0-alpha2");
List<ArtifactDependencyModel> dependencies = dependenciesModel.artifacts();
assertThat(dependencies).hasSize(1);
ExpectedArtifactDependency expected = new ExpectedArtifactDependency("classpath", "gradle", "com.android.tools.build", "2.0.0-alpha2");
expected.assertMatches(dependencies.get(0));
assertTrue(buildModel.isModified());
applyChanges(buildModel);
assertFalse(buildModel.isModified());
buildModel.reparse();
buildScriptModel = buildModel.buildscript();
dependenciesModel = buildScriptModel.dependencies();
assertTrue(buildScriptModel.hasValidPsiElement());
assertTrue(dependenciesModel.hasValidPsiElement());
dependencies = dependenciesModel.artifacts();
assertThat(dependencies).hasSize(1);
expected.assertMatches(dependencies.get(0));
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.DependenciesModel in project android by JetBrains.
the class BuildScriptModel method dependencies.
@NotNull
public DependenciesModel dependencies() {
DependenciesDslElement dependenciesDslElement = myDslElement.getPropertyElement(DEPENDENCIES_BLOCK_NAME, DependenciesDslElement.class);
if (dependenciesDslElement == null) {
dependenciesDslElement = new DependenciesDslElement(myDslElement);
myDslElement.setNewElement(DEPENDENCIES_BLOCK_NAME, dependenciesDslElement);
}
return new DependenciesModel(dependenciesDslElement);
}
Aggregations