/**
  * Makes a copy of the <code>file</code> in the <code>toDir</code> folder and returns it. Handles
  * both files and directories.
  *
  * @param requestor any object to control who called this method. Note that it is considered to be
  *     an external change if <code>requestor</code> is <code>null</code>. See {@link
  *     VirtualFileEvent#getRequestor}
  * @param file file or directory to make a copy of
  * @param toDir directory to make a copy in
  * @return a copy of the file
  * @throws IOException if file failed to be copied
  */
 public static VirtualFile copy(
     Object requestor, @NotNull VirtualFile file, @NotNull VirtualFile toDir) throws IOException {
   if (file.isDirectory()) {
     VirtualFile newDir = toDir.createChildDirectory(requestor, file.getName());
     copyDirectory(requestor, file, newDir, null);
     return newDir;
   } else {
     return copyFile(requestor, file, toDir);
   }
 }
 /**
  * Copies all files matching the <code>filter</code> from <code>fromDir</code> to <code>toDir
  * </code>.
  *
  * @param requestor any object to control who called this method. Note that it is considered to be
  *     an external change if <code>requestor</code> is <code>null</code>. See {@link
  *     VirtualFileEvent#getRequestor}
  * @param fromDir the directory to copy from
  * @param toDir the directory to copy to
  * @param filter {@link VirtualFileFilter}
  * @throws IOException if files failed to be copied
  */
 public static void copyDirectory(
     Object requestor,
     @NotNull VirtualFile fromDir,
     @NotNull VirtualFile toDir,
     @Nullable VirtualFileFilter filter)
     throws IOException {
   VirtualFile[] children = fromDir.getChildren();
   for (VirtualFile child : children) {
     if (filter == null || filter.accept(child)) {
       if (!child.isDirectory()) {
         copyFile(requestor, child, toDir);
       } else {
         VirtualFile newChild = toDir.findChild(child.getName());
         if (newChild == null) {
           newChild = toDir.createChildDirectory(requestor, child.getName());
         }
         copyDirectory(requestor, child, newChild, filter);
       }
     }
   }
 }
  public static VirtualFile createTestProjectStructure(
      String tempName,
      Module module,
      String rootPath,
      Collection<File> filesToDelete,
      boolean addProjectRoots)
      throws IOException {
    File dir = FileUtil.createTempDirectory(tempName, null, false);
    filesToDelete.add(dir);

    VirtualFile vDir =
        LocalFileSystem.getInstance()
            .refreshAndFindFileByPath(dir.getCanonicalPath().replace(File.separatorChar, '/'));
    assert vDir != null && vDir.isDirectory() : dir;
    PlatformTestCase.synchronizeTempDirVfs(vDir);

    EdtTestUtil.runInEdtAndWait(
        () -> {
          AccessToken token = WriteAction.start();
          try {
            if (rootPath != null) {
              VirtualFile vDir1 =
                  LocalFileSystem.getInstance()
                      .findFileByPath(rootPath.replace(File.separatorChar, '/'));
              if (vDir1 == null) {
                throw new Exception(rootPath + " not found");
              }
              VfsUtil.copyDirectory(null, vDir1, vDir, null);
            }

            if (addProjectRoots) {
              addSourceContentToRoots(module, vDir);
            }
          } finally {
            token.finish();
          }
        });
    return vDir;
  }
Example #4
0
 /**
  * Copies all files matching the <code>filter</code> from <code>fromDir</code> to <code>toDir
  * </code>. Symlinks end special files are ignored.
  *
  * @param requestor any object to control who called this method. Note that it is considered to be
  *     an external change if <code>requestor</code> is <code>null</code>. See {@link
  *     VirtualFileEvent#getRequestor}
  * @param fromDir the directory to copy from
  * @param toDir the directory to copy to
  * @param filter {@link VirtualFileFilter}
  * @throws IOException if files failed to be copied
  */
 public static void copyDirectory(
     Object requestor,
     @NotNull VirtualFile fromDir,
     @NotNull VirtualFile toDir,
     @Nullable VirtualFileFilter filter)
     throws IOException {
   @SuppressWarnings("UnsafeVfsRecursion")
   VirtualFile[] children = fromDir.getChildren();
   for (VirtualFile child : children) {
     if (!child.isSymLink()
         && !child.isSpecialFile()
         && (filter == null || filter.accept(child))) {
       if (!child.isDirectory()) {
         copyFile(requestor, child, toDir);
       } else {
         VirtualFile newChild = toDir.findChild(child.getName());
         if (newChild == null) {
           newChild = toDir.createChildDirectory(requestor, child.getName());
         }
         copyDirectory(requestor, child, newChild, filter);
       }
     }
   }
 }