// 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());
          }
        }
    );


  }
}

@@ -2163,25 +2163,16 @@
 ter(
-%0A        
 () -%3E %7B%0A
     
@@ -2167,20 +2167,16 @@
 () -%3E %7B%0A
-    
       if
@@ -2217,20 +2217,16 @@
         
-   
- 
 LOG.info
@@ -2314,32 +2314,24 @@
 ;%0A      
-    %7D%0A   
+%7D%0A
       
- 
 final bo
@@ -2421,28 +2421,24 @@
  %3E 0;%0A      
-    
 if (!haveMod
@@ -2462,20 +2462,16 @@
         
-   
- 
 return;%0A
@@ -2476,20 +2476,16 @@
 ;%0A      
-    
 %7D%0A%0A     
@@ -2489,22 +2489,14 @@
     
-  
-  
 try %7B%0A
-    
     
@@ -2603,20 +2603,16 @@
         
-   
- 
 ProjectS
@@ -2643,28 +2643,24 @@
 SystemId();%0A
-    
           fo
@@ -2753,20 +2753,16 @@
         
-   
- 
 DataNode
@@ -2842,20 +2842,16 @@
         
-    
 if (proj
@@ -2888,20 +2888,16 @@
         
-  
-  
 Collecti
@@ -3022,20 +3022,16 @@
         
-    
 service.
@@ -3098,34 +3098,26 @@
             
-    %7D%0A    
+%7D%0A
           %7D%0A
@@ -3120,34 +3120,26 @@
   %7D%0A        
-  
-  %7D%0A    
+%7D%0A
       %7D fina
@@ -3140,28 +3140,24 @@
 %7D finally %7B%0A
-    
         Exte
@@ -3240,32 +3240,40 @@
     
-    
 %7D%0A    
- 
-   %7D%0A    );%0A%0A
+%7D, project.getDisposed());
 %0A  %7D

fix already disposed