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