@NotNull
  private static DartLibInfo collectPackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartSdk sdk) {
    final DartLibInfo libInfo = new DartLibInfo(false);

    final Collection<VirtualFile> pubspecYamlFiles =
        FilenameIndex.getVirtualFilesByName(
            project, PUBSPEC_YAML, GlobalSearchScope.projectScope(project));
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (VirtualFile pubspecFile : pubspecYamlFiles) {
      final VirtualFile dotPackagesFile =
          pubspecFile.getParent().findChild(DotPackagesFileUtil.DOT_PACKAGES);
      final Module module =
          dotPackagesFile == null ? null : fileIndex.getModuleForFile(dotPackagesFile);

      if (dotPackagesFile != null
          && !dotPackagesFile.isDirectory()
          && module != null
          && DartSdkGlobalLibUtil.isDartSdkEnabled(module)) {
        final Map<String, String> packagesMap = DotPackagesFileUtil.getPackagesMap(dotPackagesFile);
        if (packagesMap != null) {
          for (Map.Entry<String, String> entry : packagesMap.entrySet()) {
            final String packageName = entry.getKey();
            final String packagePath = entry.getValue();
            if (isPathOutsideProjectContent(fileIndex, packagePath)) {
              libInfo.addPackage(packageName, packagePath);
            }
          }
        }
      }
    }

    return libInfo;
  }
  @NotNull
  public static Library updatePackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartLibInfo libInfo) {
    final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
    final Library existingLibrary =
        projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME);
    final Library library =
        existingLibrary != null
            ? existingLibrary
            : ApplicationManager.getApplication()
                .runWriteAction(
                    new Computable<Library>() {
                      @Override
                      public Library compute() {
                        final LibraryTableBase.ModifiableModel libTableModel =
                            ProjectLibraryTable.getInstance(project).getModifiableModel();
                        final Library lib =
                            libTableModel.createLibrary(
                                DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME,
                                DartPackagesLibraryType.LIBRARY_KIND);
                        libTableModel.commit();
                        return lib;
                      }
                    });

    final String[] existingUrls = library.getUrls(OrderRootType.CLASSES);

    final Collection<String> libRootUrls = libInfo.getLibRootUrls();

    if ((!libInfo.isProjectWithoutPubspec()
            && isBrokenPackageMap(((LibraryEx) library).getProperties()))
        || existingUrls.length != libRootUrls.size()
        || !libRootUrls.containsAll(Arrays.asList(existingUrls))) {
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                @Override
                public void run() {
                  final LibraryEx.ModifiableModelEx model =
                      (LibraryEx.ModifiableModelEx) library.getModifiableModel();
                  for (String url : existingUrls) {
                    model.removeRoot(url, OrderRootType.CLASSES);
                  }

                  for (String url : libRootUrls) {
                    model.addRoot(url, OrderRootType.CLASSES);
                  }

                  final DartPackagesLibraryProperties libraryProperties =
                      new DartPackagesLibraryProperties();
                  libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap());
                  model.setProperties(libraryProperties);

                  model.commit();
                }
              });
    }

    return library;
  }
  @Nullable
  private static Library actualizePackagesLibrary(
      @NotNull final Project project, @Nullable final DartSdk sdk) {
    if (sdk == null || StringUtil.compareVersionNumbers(sdk.getVersion(), "1.12") < 0) return null;

    final DartLibInfo libInfo = collectPackagesLibraryRoots(project, sdk);

    if (libInfo.getLibRootUrls().isEmpty()) {
      return null;
    } else {
      return updatePackagesLibraryRoots(project, libInfo);
    }
  }