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