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 static void unexcludeRootIfNeccessary(
     @NotNull VirtualFile root, @NotNull ModuleRootManager manager) {
   Set<VirtualFile> excludedRoots =
       new HashSet<VirtualFile>(Arrays.asList(manager.getExcludeRoots()));
   VirtualFile excludedRoot = root;
   while (excludedRoot != null && !excludedRoots.contains(excludedRoot)) {
     excludedRoot = excludedRoot.getParent();
   }
   if (excludedRoot == null) {
     return;
   }
   Set<VirtualFile> rootsToExclude = new HashSet<VirtualFile>();
   collectChildrenRecursively(excludedRoot, root, rootsToExclude);
   final ModifiableRootModel model = manager.getModifiableModel();
   ContentEntry contentEntry = findContentEntryForRoot(model, excludedRoot);
   if (contentEntry != null) {
     ExcludeFolder excludedFolder = null;
     for (ExcludeFolder folder : contentEntry.getExcludeFolders()) {
       if (folder.getFile() == excludedRoot) {
         excludedFolder = folder;
         break;
       }
     }
     if (excludedFolder != null) {
       contentEntry.removeExcludeFolder(excludedFolder);
     }
     for (VirtualFile rootToExclude : rootsToExclude) {
       if (!excludedRoots.contains(rootToExclude)) {
         contentEntry.addExcludeFolder(rootToExclude);
       }
     }
   }
   ApplicationManager.getApplication()
       .runWriteAction(
           new Runnable() {
             @Override
             public void run() {
               model.commit();
             }
           });
 }