// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.externalSystem.service.project.manage;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.externalSystem.ExternalSystemManager;
import com.intellij.openapi.externalSystem.model.DataNode;
import com.intellij.openapi.externalSystem.model.ExternalProjectInfo;
import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.project.ContentRootData;
import com.intellij.openapi.externalSystem.model.project.ProjectData;
import com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl;
import com.intellij.openapi.externalSystem.service.project.ProjectDataManager;
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

import static com.intellij.openapi.externalSystem.model.ProjectKeys.CONTENT_ROOT;

public class ReprocessContentRootDataActivity implements StartupActivity, DumbAware {

  private static final Logger LOG = Logger.getInstance(ReprocessContentRootDataActivity.class);

  @Override
  public void runActivity(@NotNull Project project) {

    final ProjectDataManager dataManager = ProjectDataManager.getInstance();
    final ContentRootDataService service = new ContentRootDataService();
    final IdeModifiableModelsProviderImpl modifiableModelsProvider = new IdeModifiableModelsProviderImpl(project);

    final Application application = ApplicationManager.getApplication();
    if (application.isUnitTestMode()) {
      LOG.info("Adding 'reprocess content root data' activity to 'runWhenSmart' queue in project [hash=" + project.hashCode() + "]");
    }
    application.invokeLater(() -> {
      if (application.isUnitTestMode()) {
        LOG.info("Reprocessing content root data for project [hash=" + project.hashCode() + "]" );
      }
      final boolean haveModulesToProcess = ModuleManager.getInstance(project).getModules().length > 0;
      if (!haveModulesToProcess) {
        return;
      }

      try {
        for (ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
          ProjectSystemId id = manager.getSystemId();
          for (ExternalProjectInfo info : dataManager.getExternalProjectsData(project, id)) {
            DataNode<ProjectData> projectStructure = info.getExternalProjectStructure();
            if (projectStructure != null) {
              Collection<DataNode<ContentRootData>> roots = ExternalSystemApiUtil.findAllRecursively(projectStructure, CONTENT_ROOT);
              service.importData(roots, null, project, modifiableModelsProvider);
            }
          }
        }
      } finally {
        ExternalSystemApiUtil.doWriteAction(() -> modifiableModelsProvider.commit());
      }
    }, project.getDisposed());
  }
}

@@ -207,61 +207,8 @@
 e;%0A%0A
-import com.intellij.openapi.application.Application;%0A
 impo
@@ -1833,131 +1833,19 @@
     
-final Application application = ApplicationManager.getApplication();%0A    if (application.isUnitTestMode()) %7B%0A      LOG.info
+logUnitTest
 (%22Ad
@@ -1968,15 +1968,31 @@
     
-%7D%0A    a
+ApplicationManager.getA
 ppli
@@ -1997,16 +1997,18 @@
 lication
+()
 .invokeL
@@ -2030,60 +2030,19 @@
     
-if (application.isUnitTestMode()) %7B%0A        LOG.info
+logUnitTest
 (%22Re
@@ -2120,19 +2120,11 @@
  %22%5D%22
- 
 );%0A
-      %7D
 %0A   
@@ -2254,24 +2254,88 @@
 oProcess) %7B%0A
+        logUnitTest(%22Have zero modules to process, returning%22);%0A
         retu
@@ -2346,17 +2346,16 @@
       %7D%0A
-%0A
       tr
@@ -2359,36 +2359,48 @@
  try %7B%0A        f
-or (
+inal Collection%3C
 ExternalSystemMa
@@ -2419,24 +2419,25 @@
 ?, ?, ?%3E
+%3E
  manager
  : Exter
@@ -2428,18 +2428,19 @@
  manager
- :
+s =
  Externa
@@ -2470,16 +2470,167 @@
 nagers()
+;%0A        logUnitTest(%22Found %5B%22 + managers.size() + %22%5D external system managers%22);%0A        for (ExternalSystemManager%3C?, ?, ?, ?, ?%3E manager : managers
 ) %7B%0A    
@@ -2682,36 +2682,48 @@
 d();%0A          f
-or (
+inal Collection%3C
 ExternalProjectI
@@ -2729,15 +2729,16 @@
 Info
- info :
+%3E data =
  dat
@@ -2786,98 +2786,518 @@
  id)
-) %7B%0A            DataNode%3CProjectData%3E projectStructure = info.getExternalProjectStructure(
+;%0A          logUnitTest(%22Found %5B%22 + data.size() + %22%5D external project infos using manager class=%5B%22 + dataManager.getClass().getCanonicalName() + %22%5D%22);%0A          for (ExternalProjectInfo info : data) %7B%0A            DataNode%3CProjectData%3E projectStructure = info.getExternalProjectStructure();%0A            logUnitTest(%22External data graph root is %22%0A                        + (projectStructure == null ? %22%22 : %22not%22)%0A                        + %22 null for external project path=%5B%22 + info.getExternalProjectPath() + %22%5D%22
 );%0A 
@@ -3737,11 +3737,158 @@
 ());%0A  %7D
+%0A%0A  protected void logUnitTest(String message) %7B%0A    if (ApplicationManager.getApplication().isUnitTestMode()) %7B%0A      LOG.info(message);%0A    %7D%0A  %7D
 %0A%7D%0A

add logs to track flaky test