private void syncExistingAndRemoveObsolete(
     @NotNull IdeModifiableModelsProvider modelsProvider,
     @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport,
     @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport,
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap,
     @NotNull ModifiableRootModel moduleRootModel,
     boolean hasUnresolvedLibraries) {
   for (OrderEntry entry : moduleRootModel.getOrderEntries()) {
     if (entry instanceof ModuleLibraryOrderEntryImpl) {
       ModuleLibraryOrderEntryImpl moduleLibraryOrderEntry = (ModuleLibraryOrderEntryImpl) entry;
       Library library = moduleLibraryOrderEntry.getLibrary();
       if (library == null) {
         LOG.warn(
             "Skipping module-level library entry because it doesn't have backing Library object. Entry: "
                 + entry);
         continue;
       }
       final VirtualFile[] libraryFiles = library.getFiles(OrderRootType.CLASSES);
       final Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(libraryFiles.length);
       for (VirtualFile file : libraryFiles) {
         moduleLibraryKey.add(
             ExternalSystemApiUtil.getLocalFileSystemPath(file)
                 + moduleLibraryOrderEntry.getScope().name());
       }
       LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey);
       if (existing == null || !StringUtil.equals(existing.getInternalName(), library.getName())) {
         moduleRootModel.removeOrderEntry(entry);
       } else {
         orderEntryDataMap.put(entry, existing);
         syncExistingLibraryDependency(
             modelsProvider,
             existing,
             library,
             moduleRootModel,
             moduleLibraryOrderEntry.getOwnerModule());
         toImport.remove(existing);
       }
     } else if (entry instanceof LibraryOrderEntry) {
       LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry;
       String libraryName = libraryOrderEntry.getLibraryName();
       LibraryDependencyData existing =
           projectLibrariesToImport.remove(libraryName + libraryOrderEntry.getScope().name());
       if (existing != null) {
         toImport.remove(existing);
         orderEntryDataMap.put(entry, existing);
       } else if (!hasUnresolvedLibraries) {
         // There is a possible case that a project has been successfully imported from external
         // model and after
         // that network/repo goes down. We don't want to drop existing binary mappings then.
         moduleRootModel.removeOrderEntry(entry);
       }
     }
   }
 }
  private static void removeDependencyOnDartPackagesLibrary(@NotNull final Module module) {
    final ModifiableRootModel modifiableModel =
        ModuleRootManager.getInstance(module).getModifiableModel();
    try {
      for (final OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
        if (orderEntry instanceof LibraryOrderEntry
            && LibraryTablesRegistrar.PROJECT_LEVEL.equals(
                ((LibraryOrderEntry) orderEntry).getLibraryLevel())
            && DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals(
                ((LibraryOrderEntry) orderEntry).getLibraryName())) {
          modifiableModel.removeOrderEntry(orderEntry);
        }
      }

      if (modifiableModel.isChanged()) {
        ApplicationManager.getApplication()
            .runWriteAction(
                new Runnable() {
                  @Override
                  public void run() {
                    modifiableModel.commit();
                  }
                });
      }
    } finally {
      if (!modifiableModel.isDisposed()) {
        modifiableModel.dispose();
      }
    }
  }
  private static void removeGenModule(@NotNull final Module libModule) {
    final String genModuleName = getGenModuleName(libModule);
    final ModuleManager moduleManager = ModuleManager.getInstance(libModule.getProject());
    final ModifiableRootModel model = ModuleRootManager.getInstance(libModule).getModifiableModel();

    for (OrderEntry entry : model.getOrderEntries()) {
      if (entry instanceof ModuleOrderEntry
          && genModuleName.equals(((ModuleOrderEntry) entry).getModuleName())) {
        model.removeOrderEntry(entry);
      }
    }

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                model.commit();
              }
            });

    final Module genModule = moduleManager.findModuleByName(genModuleName);
    if (genModule == null) {
      return;
    }
    moduleManager.disposeModule(genModule);

    final VirtualFile moduleFile = genModule.getModuleFile();

    if (moduleFile != null) {
      ApplicationManager.getApplication()
          .invokeLater(
              new Runnable() {
                @Override
                public void run() {
                  ApplicationManager.getApplication()
                      .runWriteAction(
                          new Runnable() {
                            @Override
                            public void run() {
                              try {
                                moduleFile.delete(libModule.getProject());
                              } catch (IOException e) {
                                LOG.error(e);
                              }
                            }
                          });
                }
              });
    }
  }
 private static void removeObsolete(
     @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport,
     @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport,
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull ModifiableRootModel moduleRootModel) {
   Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet();
   for (OrderEntry entry : moduleRootModel.getOrderEntries()) {
     if (entry instanceof ModuleLibraryOrderEntryImpl) {
       Library library = ((ModuleLibraryOrderEntryImpl) entry).getLibrary();
       if (library == null) {
         LOG.warn(
             "Skipping module-level library entry because it doesn't have backing Library object. Entry: "
                 + entry);
         continue;
       }
       moduleLibraryKey.clear();
       for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) {
         moduleLibraryKey.add(ExternalSystemApiUtil.getLocalFileSystemPath(file));
       }
       LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey);
       if (existing == null) {
         moduleRootModel.removeOrderEntry(entry);
       } else {
         toImport.remove(existing);
       }
     } else if (entry instanceof LibraryOrderEntry) {
       String libraryName = ((LibraryOrderEntry) entry).getLibraryName();
       LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName);
       if (existing == null) {
         moduleRootModel.removeOrderEntry(entry);
       } else {
         toImport.remove(existing);
       }
     }
   }
 }