Search in sources :

Example 1 with GradleSyncListener

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);
}
Also used : Project(com.intellij.openapi.project.Project) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener)

Example 2 with GradleSyncListener

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);
}
Also used : GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener)

Example 3 with GradleSyncListener

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);
    }
}
Also used : Projects.isLegacyIdeaAndroidProject(com.android.tools.idea.gradle.util.Projects.isLegacyIdeaAndroidProject) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) ConfigurationException(com.intellij.openapi.options.ConfigurationException) GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter) IOException(java.io.IOException)

Example 4 with GradleSyncListener

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);
    }
}
Also used : GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter) IOException(java.io.IOException) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) LanguageLevel(com.intellij.pom.java.LanguageLevel) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File)

Example 5 with GradleSyncListener

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) Library(com.intellij.openapi.roots.libraries.Library) CountDownLatch(java.util.concurrent.CountDownLatch) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GradleSyncListener (com.android.tools.idea.gradle.project.sync.GradleSyncListener)6 Project (com.intellij.openapi.project.Project)4 GradleProjectImporter (com.android.tools.idea.gradle.project.importing.GradleProjectImporter)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 IOException (java.io.IOException)2 GradleSyncInvoker (com.android.tools.idea.gradle.project.sync.GradleSyncInvoker)1 Projects.isLegacyIdeaAndroidProject (com.android.tools.idea.gradle.util.Projects.isLegacyIdeaAndroidProject)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 Library (com.intellij.openapi.roots.libraries.Library)1 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)1 Ref (com.intellij.openapi.util.Ref)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 NotNull (org.jetbrains.annotations.NotNull)1