private static Promise<?> setupLibraryRoots(@NotNull LibraryEx library,
                                              RepositoryLibraryProperties properties,
                                              String[] annotationUrls,
                                              String[] excludedRootUrls,
                                              List<OrderRoot> roots) {
    return ReadAction.nonBlocking(() -> {
        if (library.isDisposed()) {
          return false;
        }

        return libraryRootsEqual(library, annotationUrls, excludedRootUrls, roots);
      })
      .submit(NonUrgentExecutor.getInstance())
      .thenAsync(rootsEqual -> {
        if (rootsEqual) {
          // Nothing to update
          return resolvedPromise();
        }

        AsyncPromise<?> result = new AsyncPromise<>();

        ApplicationManager.getApplication().invokeLaterOnWriteThread(
          () -> {
            try {
              ApplicationManager.getApplication().runWriteAction(() -> {
                if (library.isDisposed()) {
                  result.cancel();
                  return;
                }

                final NewLibraryEditor editor = new NewLibraryEditor(null, properties);
                editor.setKeepInvalidUrls(false);
                editor.removeAllRoots();
                editor.addRoots(roots);
                for (String url : annotationUrls) {
                  editor.addRoot(url, AnnotationOrderRootType.getInstance());
                }
                List<String> allRootUrls = editor.getOrderRootTypes().stream()
                  .flatMap(type -> Arrays.stream(editor.getUrls(type)))
                  .collect(Collectors.toList());
                for (String excludedRootUrl : excludedRootUrls) {
                  if (VfsUtilCore.isUnder(excludedRootUrl, allRootUrls)) {
                    editor.addExcludedRoot(excludedRootUrl);
                  }
                }
                final LibraryEx.ModifiableModelEx model = library.getModifiableModel();
                editor.applyTo(model);
                model.commit();

                result.setResult(null);
              });
            } catch (Throwable t) {
              LOG.warn("Unable to update project model for library '" + library.getName() + "'", t);
              result.setError(t);
            }
          });

        return result;
      });
  }
private static Promise<?> setupLibraryRoots(@NotNull LibraryEx library,
                                              RepositoryLibraryProperties properties,
                                              String[] annotationUrls,
                                              String[] excludedRootUrls,
                                              List<OrderRoot> roots)