use of com.android.tools.idea.gradle.project.sync.GradleSyncListener in project android by JetBrains.
the class GradleProjectImporterTest method testImportProjectWithDefaultSettings.
public void testImportProjectWithDefaultSettings() throws Exception {
Project newProject = getProject();
when(myProjectSetup.createProject(myProjectName, myProjectFolderPath.getPath())).thenReturn(newProject);
GradleSyncListener syncListener = mock(GradleSyncListener.class);
myProjectImporter.importProject(myProjectName, myProjectFolderPath, syncListener);
// Verify project setup before syncing.
verifyProjectFilesCreation();
verifyProjectCreation(times(1));
verifyProjectPreparation(null);
verifyGradleVmOptionsCleanup(times(1));
// Verify sync.
verifyGradleSyncInvocation(new GradleProjectImporter.Request(), syncListener);
}
use of com.android.tools.idea.gradle.project.sync.GradleSyncListener in project android by JetBrains.
the class GradleProjectImporterTest method testImportProjectWithNonNullProject.
public void testImportProjectWithNonNullProject() throws Exception {
GradleProjectImporter.Request importSettings = new GradleProjectImporter.Request();
importSettings.setProject(getProject()).setLanguageLevel(JDK_1_8);
GradleSyncListener syncListener = mock(GradleSyncListener.class);
myProjectImporter.importProject(myProjectName, myProjectFolderPath, importSettings, syncListener);
// Verify project setup before syncing.
verifyProjectFilesCreation();
verifyProjectCreation(never());
verifyProjectPreparation(JDK_1_8);
verifyGradleVmOptionsCleanup(never());
// Verify sync.
verifyGradleSyncInvocation(importSettings, syncListener);
}
use of com.android.tools.idea.gradle.project.sync.GradleSyncListener in project android by JetBrains.
the class AndroidGradleTestCase method importProject.
protected void importProject(@NotNull String projectName, @NotNull File projectRoot, @Nullable GradleSyncListener listener) throws Exception {
Ref<Throwable> throwableRef = new Ref<>();
SyncListener syncListener = new SyncListener();
Project project = getProject();
GradleSyncState.subscribe(project, syncListener);
runWriteCommandAction(project, () -> {
try {
// When importing project for tests we do not generate the sources as that triggers a compilation which finishes asynchronously.
// This causes race conditions and intermittent errors. If a test needs source generation this should be handled separately.
GradleProjectImporter.Request request = new GradleProjectImporter.Request();
request.setProject(project).setGenerateSourcesOnSuccess(false);
GradleProjectImporter.getInstance().importProject(projectName, projectRoot, request, listener);
} catch (Throwable e) {
throwableRef.set(e);
}
});
Throwable throwable = throwableRef.get();
if (throwable != null) {
if (throwable instanceof IOException) {
throw (IOException) throwable;
} else if (throwable instanceof ConfigurationException) {
throw (ConfigurationException) throwable;
} else {
throw new RuntimeException(throwable);
}
}
syncListener.await();
if (syncListener.failureMessage != null && listener == null) {
fail(syncListener.failureMessage);
}
}
use of com.android.tools.idea.gradle.project.sync.GradleSyncListener in project android by JetBrains.
the class NewProjectWizardDynamic method runFinish.
private void runFinish() {
if (ApplicationManager.getApplication().isUnitTestMode()) {
return;
}
GradleProjectImporter projectImporter = GradleProjectImporter.getInstance();
String rootPath = getState().get(PROJECT_LOCATION_KEY);
if (rootPath == null) {
LOG.error("No root path specified for project");
return;
}
File rootLocation = new File(rootPath);
File wrapperPropertiesFilePath = GradleWrapper.getDefaultPropertiesFilePath(rootLocation);
try {
GradleWrapper.get(wrapperPropertiesFilePath).updateDistributionUrl(SdkConstants.GRADLE_LATEST_VERSION);
} catch (IOException e) {
// Unlikely to happen. Continue with import, the worst-case scenario is that sync fails and the error message has a "quick fix".
LOG.warn("Failed to update Gradle wrapper file", e);
}
String projectName = getState().get(APPLICATION_NAME_KEY);
if (projectName == null) {
projectName = "Unnamed Project";
}
// Pick the highest language level of all the modules/form factors.
// We have to pick the language level up front while creating the project rather than
// just reacting to it during sync, because otherwise the user gets prompted with
// a changing-language-level-requires-reopening modal dialog box and have to reload
// the project
LanguageLevel initialLanguageLevel = null;
for (FormFactor factor : FormFactor.values()) {
Object version = getState().get(FormFactorUtils.getLanguageLevelKey(factor));
if (version != null) {
LanguageLevel level = LanguageLevel.parse(version.toString());
if (level != null && (initialLanguageLevel == null || level.isAtLeast(initialLanguageLevel))) {
initialLanguageLevel = level;
}
}
}
// This is required for Android plugin in IDEA
if (!IdeInfo.getInstance().isAndroidStudio()) {
final Sdk jdk = IdeSdks.getInstance().getJdk();
if (jdk != null) {
ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManager.getInstance(myProject).setProjectSdk(jdk));
}
}
try {
GradleSyncListener listener = new PostStartupGradleSyncListener(() -> {
Iterable<File> targetFiles = myState.get(TARGET_FILES_KEY);
assert targetFiles != null;
TemplateUtils.reformatAndRearrange(myProject, targetFiles);
Collection<File> filesToOpen = myState.get(FILES_TO_OPEN_KEY);
assert filesToOpen != null;
TemplateUtils.openEditors(myProject, filesToOpen, true);
});
GradleProjectImporter.Request request = new GradleProjectImporter.Request();
request.setLanguageLevel(initialLanguageLevel).setProject(myProject);
projectImporter.importProject(projectName, rootLocation, request, listener);
} catch (IOException | ConfigurationException e) {
Messages.showErrorDialog(e.getMessage(), ERROR_MSG_TITLE);
LOG.error(e);
}
}
use of com.android.tools.idea.gradle.project.sync.GradleSyncListener in project android by JetBrains.
the class TestArtifactSearchScopesTest method testNotExcludeLibrariesInMainArtifact.
public void testNotExcludeLibrariesInMainArtifact() throws Exception {
TestArtifactSearchScopes scopes = loadMultiProjectAndTestScopes();
LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myFixture.getProject());
Library gson = libraryTable.getLibraryByName("gson-2.2.4");
// In the beginning only unit test exclude gson
assertScopeContainsLibrary(scopes.getUnitTestExcludeScope(), gson, true);
assertScopeContainsLibrary(scopes.getAndroidTestExcludeScope(), gson, false);
// Now add gson to unit test dependencies as well
VirtualFile buildFile = getGradleBuildFile(scopes.getModule());
assertNotNull(buildFile);
appendToFile(virtualToIoFile(buildFile), "\n\ndependencies { compile 'com.google.code.gson:gson:2.2.4' }\n");
final CountDownLatch latch = new CountDownLatch(1);
GradleSyncListener postSetupListener = new GradleSyncListener.Adapter() {
@Override
public void syncSucceeded(@NotNull Project project) {
latch.countDown();
}
@Override
public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
latch.countDown();
}
};
GradleSyncState.subscribe(getProject(), postSetupListener);
runWriteCommandAction(getProject(), () -> {
GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
GradleSyncInvoker.getInstance().requestProjectSync(getProject(), request, null);
});
latch.await();
// Now both test should not exclude gson
scopes = TestArtifactSearchScopes.get(scopes.getModule());
assertNotNull(scopes);
gson = libraryTable.getLibraryByName("gson-2.2.4");
assertScopeContainsLibrary(scopes.getUnitTestExcludeScope(), gson, false);
assertScopeContainsLibrary(scopes.getAndroidTestExcludeScope(), gson, false);
}
Aggregations