use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AndroidAddLibraryDependencyAction method addDependency.
/**
* Adds the given dependency to the project.
*
* @param project
* @param buildModel
* @param coordinateString
*/
private static void addDependency(@NotNull final Project project, @NotNull final GradleBuildModel buildModel, @NotNull String coordinateString) {
GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(coordinateString);
if (coordinate == null || coordinate.getArtifactId() == null) {
return;
}
final ArtifactDependencySpec newDependency = new ArtifactDependencySpec(coordinate.getArtifactId(), coordinate.getGroupId(), coordinate.getRevision());
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
@Override
public void run() {
buildModel.dependencies().addArtifact(CommonConfigurationNames.COMPILE, newDependency);
buildModel.applyChanges();
}
});
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project freeline by alibaba.
the class FreelineUtil method installFreeline.
private static void installFreeline(final Project project, final FreelineStatus status, final PsiFile psiFile, final GradleDependencyEntity dependencyEntity) {
needReformatCode = false;
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
if (!status.isExistClasspath()) {
Collection<VirtualFile> collection = status.getGradleBuildFiles();
if (dependencyEntity != null) {
for (VirtualFile file : collection) {
GradleBuildModel model = GradleBuildModel.parseBuildFile(file, project);
List<ArtifactDependencyModel> artifactDependencyModels = model.buildscript().dependencies().artifacts();
for (ArtifactDependencyModel model1 : artifactDependencyModels) {
ArtifactDependencyModelWrapper wrapper = new ArtifactDependencyModelWrapper(model1);
if (wrapper.group().equals(Constant.ANDROID_GRADLE_TOOL_GROUP_NAME)) {
ArtifactDependencySpec spec = new ArtifactDependencySpec(dependencyEntity.getArtifactId(), dependencyEntity.getGroupId(), dependencyEntity.getNewestReleaseVersion());
model.buildscript().dependencies().addArtifact("classpath", spec);
model.applyChanges();
needReformatCode = true;
status.setClasspathFile(file);
break;
}
}
if (status.isExistClasspath()) {
break;
}
}
}
}
if (!status.isExistPlugin()) {
if (psiFile != null && psiFile instanceof GroovyFile) {
GradleUtil.applyPlugin(project, (GroovyFile) psiFile, Constant.FREELINE_PLUGIN_ID);
}
}
}
});
}
});
if (needReformatCode && status.getClasspathFile() != null) {
DocumentUtil.reformatCode(project, status.getClasspathFile());
}
LogUtil.d("Sync Project Finish, start download freeline.zip.");
initFreeline(project);
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AddGradleDependencyTest method testAddJetbrainsAnnotationDependency.
@Test
public void testAddJetbrainsAnnotationDependency() throws IOException {
guiTest.importSimpleApplication();
EditorFixture editor = guiTest.ideFrame().getEditor().open("app/src/main/java/google/simpleapplication/MyActivity.java");
editor.moveBetween("onCreate(", "Bundle savedInstanceState) {");
editor.enterText("\n@NotNull ");
editor.waitForCodeAnalysisHighlightCount(ERROR, 1);
editor.moveBetween("@Not", "Null ");
editor.invokeQuickfixAction("Add 'annotations-java5' to classpath");
guiTest.ideFrame().waitForGradleProjectSyncToFinish();
editor.waitForCodeAnalysisHighlightCount(ERROR, 0);
GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
ArtifactDependencySpec expected = new ArtifactDependencySpec("annotations-java5", "org.jetbrains", "15.0");
appBuildModel.requireDependency(COMPILE, expected);
// Undo the import statement first
editor.invokeAction(UNDO);
verifyUndo(editor, 1);
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec in project android by JetBrains.
the class AddGradleDependencyTest method testAddJUnitDependency.
@RunIn(TestGroup.UNRELIABLE)
@Test
public void testAddJUnitDependency() throws IOException {
EditorFixture editor = guiTest.importSimpleApplication().getEditor().open("app/build.gradle").moveBetween("testCompile", "").invokeAction(DELETE_LINE).getIdeFrame().requestProjectSync().getEditor().open("app/src/test/java/google/simpleapplication/UnitTest.java").waitForCodeAnalysisHighlightCount(ERROR, 6).moveBetween("@", "Test").invokeQuickfixAction("Add 'JUnit4' to classpath").getIdeFrame().waitForGradleProjectSyncToFinish().getEditor().waitForCodeAnalysisHighlightCount(ERROR, 0);
GradleBuildModelFixture appBuildModel = guiTest.ideFrame().parseBuildFileForModule("app");
ArtifactDependencySpec expected = new ArtifactDependencySpec("junit", "junit", "4.12");
appBuildModel.requireDependency(TEST_COMPILE, expected);
verifyUndo(editor, 6);
}
use of com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencySpec 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;
}
Aggregations