private static void addDependencyOnDartPackagesLibrary(
      @NotNull final Module module, @NotNull final Library library) {
    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())) {
          return; // dependency already exists
        }
      }

      modifiableModel.addLibraryEntry(library);

      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                @Override
                public void run() {
                  modifiableModel.commit();
                }
              });
    } finally {
      if (!modifiableModel.isDisposed()) {
        modifiableModel.dispose();
      }
    }
  }
 public static void addSourceRoot(
     final ModuleRootManager manager, @NotNull final VirtualFile root) {
   final ModifiableRootModel model = manager.getModifiableModel();
   ContentEntry contentEntry = findContentEntryForRoot(model, root);
   if (contentEntry == null) {
     contentEntry = model.addContentEntry(root);
   }
   contentEntry.addSourceFolder(root, false);
   model.commit();
 }
 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 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);
                              }
                            }
                          });
                }
              });
    }
  }
 @NotNull
 private static ContentEntry findContentEntryWithAssertion(
     ModifiableRootModel model, VirtualFile dir) {
   ContentEntry entry = findContentEntry(model, dir);
   if (entry == null) {
     throw new RuntimeException(
         dir + " is not under content roots: " + Arrays.toString(model.getContentRoots()));
   }
   return entry;
 }
 @NotNull
 private static Set<String> resolveModuleDeps(
     @NotNull ModifiableRootModel rootModel,
     @NotNull ImportedOtpApp importedOtpApp,
     @Nullable Sdk projectSdk,
     @NotNull Set<String> allImportedAppNames) {
   HashSet<String> unresolvedAppNames = ContainerUtil.newHashSet();
   for (String depAppName : importedOtpApp.getDeps()) {
     if (allImportedAppNames.contains(depAppName)) {
       rootModel.addInvalidModuleEntry(depAppName);
     } else if (projectSdk != null && isSdkOtpApp(depAppName, projectSdk)) {
       // SDK is already a dependency
     } else {
       rootModel.addInvalidModuleEntry(depAppName);
       unresolvedAppNames.add(depAppName);
     }
   }
   return unresolvedAppNames;
 }
  public static void multiCommit(
      @NotNull Collection<ModifiableRootModel> rootModels,
      @NotNull ModifiableModuleModel moduleModel) {
    ApplicationManager.getApplication().assertWriteAccessAllowed();

    final List<RootModelImpl> modelsToCommit = getSortedChangedModels(rootModels, moduleModel);

    final List<ModifiableRootModel> modelsToDispose = ContainerUtil.newArrayList(rootModels);
    modelsToDispose.removeAll(modelsToCommit);

    ModuleManagerImpl.commitModelWithRunnable(
        moduleModel,
        () -> {
          for (RootModelImpl model : modelsToCommit) {
            ModuleRootManagerImpl.doCommit(model);
          }
          for (ModifiableRootModel model : modelsToDispose) {
            model.dispose();
          }
        });
  }
 @Nullable
 public static ContentEntry findContentEntryForRoot(
     @NotNull ModifiableRootModel model, @NotNull VirtualFile root) {
   ContentEntry contentEntry = null;
   for (ContentEntry candidate : model.getContentEntries()) {
     VirtualFile contentRoot = candidate.getFile();
     if (contentRoot != null && VfsUtilCore.isAncestor(contentRoot, root, false)) {
       contentEntry = candidate;
     }
   }
   return contentEntry;
 }
 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);
       }
     }
   }
 }
 private void importMissing(
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull LibraryTable moduleLibraryTable,
     @NotNull LibraryTable libraryTable,
     @NotNull Module module) {
   for (LibraryDependencyData dependencyData : toImport) {
     LibraryData libraryData = dependencyData.getTarget();
     if (libraryData.isUnresolved()) {
       continue;
     }
     switch (dependencyData.getLevel()) {
       case MODULE:
         @SuppressWarnings("ConstantConditions")
         Library moduleLib = moduleLibraryTable.createLibrary(dependencyData.getName());
         Library.ModifiableModel libModel = moduleLib.getModifiableModel();
         try {
           Map<OrderRootType, Collection<File>> files =
               myLibraryManager.prepareLibraryFiles(libraryData);
           myLibraryManager.registerPaths(files, libModel, dependencyData.getName());
         } finally {
           libModel.commit();
         }
         break;
       case PROJECT:
         final Library projectLib = libraryTable.getLibraryByName(dependencyData.getName());
         if (projectLib == null) {
           assert false;
           continue;
         }
         LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
         LOG.info(
             String.format(
                 "Adding library dependency '%s' to module '%s'",
                 projectLib.getName(), module.getName()));
         orderEntry.setExported(dependencyData.isExported());
         orderEntry.setScope(dependencyData.getScope());
         LOG.info(
             String.format(
                 "Configuring library dependency '%s' of module '%s' to be%s exported and have scope %s",
                 projectLib.getName(),
                 module.getName(),
                 dependencyData.isExported() ? " not" : "",
                 dependencyData.getScope()));
     }
   }
 }
 private void importMissing(
     @NotNull IdeModifiableModelsProvider modelsProvider,
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap,
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull LibraryTable moduleLibraryTable,
     @NotNull Module module) {
   for (final LibraryDependencyData dependencyData : toImport) {
     final LibraryData libraryData = dependencyData.getTarget();
     final String libraryName = libraryData.getInternalName();
     switch (dependencyData.getLevel()) {
       case MODULE:
         final Library moduleLib;
         if (libraryName.isEmpty()) {
           moduleLib = moduleLibraryTable.createLibrary();
         } else {
           moduleLib = moduleLibraryTable.createLibrary(libraryName);
         }
         final LibraryOrderEntry existingLibraryDependency =
             syncExistingLibraryDependency(
                 modelsProvider, dependencyData, moduleLib, moduleRootModel, module);
         orderEntryDataMap.put(existingLibraryDependency, dependencyData);
         break;
       case PROJECT:
         final Library projectLib = modelsProvider.getLibraryByName(libraryName);
         if (projectLib == null) {
           final LibraryOrderEntry existingProjectLibraryDependency =
               syncExistingLibraryDependency(
                   modelsProvider,
                   dependencyData,
                   moduleLibraryTable.createLibrary(libraryName),
                   moduleRootModel,
                   module);
           orderEntryDataMap.put(existingProjectLibraryDependency, dependencyData);
           break;
         }
         LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
         orderEntryDataMap.put(orderEntry, dependencyData);
         setLibraryScope(orderEntry, projectLib, module, dependencyData);
     }
   }
 }
 @Nullable
 private static LibraryOrderEntry findLibraryOrderEntry(
     @NotNull ModifiableRootModel moduleRootModel,
     @NotNull Library library,
     @NotNull DependencyScope scope) {
   LibraryOrderEntry candidate = null;
   for (OrderEntry orderEntry : moduleRootModel.getOrderEntries()) {
     if (orderEntry instanceof LibraryOrderEntry) {
       final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
       if (library == libraryOrderEntry.getLibrary()) {
         return libraryOrderEntry;
       }
       if (library.equals(libraryOrderEntry.getLibrary())) {
         if (libraryOrderEntry.getScope() == scope) {
           return libraryOrderEntry;
         } else {
           candidate = libraryOrderEntry;
         }
       }
     }
   }
   return candidate;
 }
  @Override
  protected Map<OrderEntry, OrderAware> importData(
      @NotNull final Collection<DataNode<LibraryDependencyData>> nodesToImport,
      @NotNull final Module module,
      @NotNull final IdeModifiableModelsProvider modelsProvider) {
    // The general idea is to import all external project library dependencies and module libraries
    // which don't present at the
    // ide side yet and remove all project library dependencies and module libraries which present
    // at the ide but not at
    // the given collection.
    // The trick is that we should perform module settings modification inside try/finally block
    // against target root model.
    // That means that we need to prepare all necessary data, obtain a model and modify it as
    // necessary.
    final Map<Set<String> /* library paths */, LibraryDependencyData> moduleLibrariesToImport =
        ContainerUtilRt.newHashMap();
    final Map<String /* library name + scope */, LibraryDependencyData> projectLibrariesToImport =
        ContainerUtilRt.newHashMap();
    final Set<LibraryDependencyData> toImport = ContainerUtilRt.newLinkedHashSet();
    final Map<OrderEntry, OrderAware> orderEntryDataMap = ContainerUtil.newLinkedHashMap();

    boolean hasUnresolved = false;
    for (DataNode<LibraryDependencyData> dependencyNode : nodesToImport) {
      LibraryDependencyData dependencyData = dependencyNode.getData();
      LibraryData libraryData = dependencyData.getTarget();
      hasUnresolved |= libraryData.isUnresolved();
      switch (dependencyData.getLevel()) {
        case MODULE:
          Set<String> paths = ContainerUtilRt.newHashSet();
          for (String path : libraryData.getPaths(LibraryPathType.BINARY)) {
            paths.add(
                ExternalSystemApiUtil.toCanonicalPath(path) + dependencyData.getScope().name());
          }
          moduleLibrariesToImport.put(paths, dependencyData);
          toImport.add(dependencyData);
          break;
        case PROJECT:
          projectLibrariesToImport.put(
              libraryData.getInternalName() + dependencyData.getScope().name(), dependencyData);
          toImport.add(dependencyData);
      }
    }

    final boolean finalHasUnresolved = hasUnresolved;

    final ModifiableRootModel modifiableRootModel = modelsProvider.getModifiableRootModel(module);
    LibraryTable moduleLibraryTable = modifiableRootModel.getModuleLibraryTable();
    syncExistingAndRemoveObsolete(
        modelsProvider,
        moduleLibrariesToImport,
        projectLibrariesToImport,
        toImport,
        orderEntryDataMap,
        modifiableRootModel,
        finalHasUnresolved);
    // Import missing library dependencies.
    if (!toImport.isEmpty()) {
      importMissing(
          modelsProvider,
          toImport,
          orderEntryDataMap,
          modifiableRootModel,
          moduleLibraryTable,
          module);
    }
    return orderEntryDataMap;
  }
  @Override
  public List<Module> commit(
      @NotNull Project project,
      @Nullable ModifiableModuleModel moduleModel,
      @NotNull ModulesProvider modulesProvider,
      @Nullable ModifiableArtifactModel modifiableArtifactModel) {
    Set<String> selectedAppNames = ContainerUtil.newHashSet();
    for (ImportedOtpApp importedOtpApp : mySelectedOtpApps) {
      selectedAppNames.add(importedOtpApp.getName());
    }
    Sdk projectSdk = fixProjectSdk(project);
    List<Module> createdModules = new ArrayList<Module>();
    final List<ModifiableRootModel> createdRootModels = new ArrayList<ModifiableRootModel>();
    final ModifiableModuleModel obtainedModuleModel =
        moduleModel != null ? moduleModel : ModuleManager.getInstance(project).getModifiableModel();
    for (ImportedOtpApp importedOtpApp : mySelectedOtpApps) {
      VirtualFile ideaModuleDir = importedOtpApp.getRoot();
      String ideaModuleFile =
          ideaModuleDir.getCanonicalPath() + File.separator + importedOtpApp.getName() + ".iml";
      Module module =
          obtainedModuleModel.newModule(ideaModuleFile, ErlangModuleType.getInstance().getId());
      createdModules.add(module);
      importedOtpApp.setModule(module);
      if (importedOtpApp.getIdeaModuleFile() == null) {
        ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
        // Make it inherit SDK from the project.
        rootModel.inheritSdk();
        // Initialize source and test paths.
        ContentEntry content = rootModel.addContentEntry(importedOtpApp.getRoot());
        addSourceDirToContent(content, ideaModuleDir, "src", false);
        addSourceDirToContent(content, ideaModuleDir, "test", true);
        addIncludeDirectories(content, importedOtpApp);
        // Exclude standard folders
        excludeDirFromContent(content, ideaModuleDir, "doc");
        // Initialize output paths according to Rebar conventions.
        CompilerModuleExtension compilerModuleExt =
            rootModel.getModuleExtension(CompilerModuleExtension.class);
        compilerModuleExt.inheritCompilerOutputPath(false);
        compilerModuleExt.setCompilerOutputPath(ideaModuleDir + File.separator + "ebin");
        compilerModuleExt.setCompilerOutputPathForTests(ideaModuleDir + File.separator + ".eunit");
        createdRootModels.add(rootModel);
        // Set inter-module dependencies
        resolveModuleDeps(rootModel, importedOtpApp, projectSdk, selectedAppNames);
      }
    }
    // Commit project structure.
    LOG.info("Commit project structure");
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                for (ModifiableRootModel rootModel : createdRootModels) {
                  rootModel.commit();
                }
                obtainedModuleModel.commit();
              }
            });

    addErlangFacets(mySelectedOtpApps);
    RebarSettings.getInstance(project).setRebarPath(myRebarPath);
    if (myIsImportingProject) {
      ErlangCompilerSettings.getInstance(project).setUseRebarCompilerEnabled(true);
    }
    CompilerWorkspaceConfiguration.getInstance(project).CLEAR_OUTPUT_DIRECTORY = false;

    return createdModules;
  }