private static VirtualFile[] copyFiles(
      final VirtualFile[] fromFiles, final VirtualFile toDir, VirtualFile rawProjectRoot)
      throws IOException {
    final VirtualFile[] toFiles = new VirtualFile[fromFiles.length];
    final boolean rootSpecified = rawProjectRoot != null;
    for (int i = 0, n = fromFiles.length; i < n; i++) {
      VirtualFile fromFile = fromFiles[i];
      VirtualFile toP = toDir;
      if (rootSpecified) {
        final List<String> fromParents = new ArrayList<>(4);
        VirtualFile fromP = fromFile.getParent();
        if (!Comparing.equal(fromP, rawProjectRoot)) {
          do {
            fromParents.add(fromP.getName());
          } while (!Comparing.equal((fromP = fromP.getParent()), rawProjectRoot));

          for (int j = fromParents.size() - 1; j >= 0; j--) {
            toP = toP.createChildDirectory(null, fromParents.get(j));
          }
        }
      }
      final VirtualFile toFile = toP.createChildData(null, fromFile.getName());
      toFile.setBinaryContent(fromFile.contentsToByteArray());
      toFiles[i] = toFile;
    }

    return toFiles;
  }
 public void testPackageNamesInEmptyDirectory() throws IOException {
   VirtualFile dir = myFixture.getTempDirFixture().findOrCreateDir("directory-name");
   VirtualFile file = dir.createChildData(this, "test.go");
   VfsUtil.saveText(file, "package <caret>");
   myFixture.configureFromExistingVirtualFile(file);
   myFixture.completeBasic();
   List<String> strings = myFixture.getLookupElementStrings();
   assertNotNull(strings);
   assertSameElements(strings, "directory_name", "main");
 }
  @Nullable
  private VirtualFile createFileFromTemplate(
      @Nullable final Project project,
      String url,
      final String templateName,
      final boolean forceNew) {
    final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    final File file = new File(VfsUtilCore.urlToPath(url));
    VirtualFile existingFile = fileSystem.refreshAndFindFileByIoFile(file);
    if (existingFile != null) {
      existingFile.refresh(false, false);
      if (!existingFile.isValid()) {
        existingFile = null;
      }
    }

    if (existingFile != null && !forceNew) {
      return existingFile;
    }
    try {
      String text = getText(templateName, project);
      final VirtualFile childData;
      if (existingFile == null || existingFile.isDirectory()) {
        final VirtualFile virtualFile;
        if (!FileUtil.createParentDirs(file)
            || (virtualFile = fileSystem.refreshAndFindFileByIoFile(file.getParentFile()))
                == null) {
          throw new IOException(
              IdeBundle.message("error.message.unable.to.create.file", file.getPath()));
        }
        childData = virtualFile.createChildData(this, file.getName());
      } else {
        childData = existingFile;
      }
      VfsUtil.saveText(childData, text);
      return childData;
    } catch (final IOException e) {
      LOG.info(e);
      ApplicationManager.getApplication()
          .invokeLater(
              new Runnable() {
                @Override
                public void run() {
                  Messages.showErrorDialog(
                      IdeBundle.message(
                          "message.text.error.creating.deployment.descriptor",
                          e.getLocalizedMessage()),
                      IdeBundle.message("message.text.creating.deployment.descriptor"));
                }
              });
    }
    return null;
  }
 private VirtualFile createNlsByPath(@NotNull VirtualFile nlsRoot, @NotNull String[] chunks)
     throws IOException {
   VirtualFile currentFolder = nlsRoot;
   for (int i = 0; i < chunks.length - 1; i++) {
     String chunk = chunks[i];
     VirtualFile nextFolder = currentFolder.findChild(chunk);
     if (nextFolder == null) {
       nextFolder = currentFolder.createChildDirectory(this, chunk);
     }
     currentFolder = nextFolder;
   }
   //noinspection ConstantConditions
   return currentFolder.createChildData(this, ArrayUtil.getLastElement(chunks));
 }
  public void testRevertingRenameFromOldRevisionsWhenDirDoesNotExists() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");

    f.rename(this, "bar.txt");

    dir.delete(this);

    revertChange(1);

    dir = myRoot.findChild("dir");
    assertNotNull(dir);
    assertNotNull(dir.findChild("foo.txt"));
    assertNull(dir.findChild("bar.txt"));
  }
  public void testParentRename() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");
    f.setBinaryContent(new byte[] {123}, -1, 4000);

    dir.rename(this, "dir2");

    revertLastChange();

    assertNull(myRoot.findChild("dir2"));
    dir = myRoot.findChild("dir");
    f = dir.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(123, f.contentsToByteArray()[0]);
    assertEquals(4000, f.getTimeStamp());
  }
  public void testRevertingMoveFromOldRevisionsWhenDirDoesNotExists() throws Exception {
    VirtualFile dir1 = myRoot.createChildDirectory(this, "dir1");
    VirtualFile dir2 = myRoot.createChildDirectory(this, "dir2");
    VirtualFile f = dir1.createChildData(this, "foo.txt");

    f.move(this, dir2);

    dir1.delete(this);
    dir2.delete(this);

    revertChange(2);

    dir1 = myRoot.findChild("dir1");
    assertNotNull(dir1);
    assertNull(myRoot.findChild("dir2"));
    assertNotNull(dir1.findChild("foo.txt"));
  }
  public void testRevertingContentChangeFromOldRevisionsWhenDirDoesNotExists() throws Exception {
    VirtualFile dir = myRoot.createChildDirectory(this, "dir");
    VirtualFile f = dir.createChildData(this, "foo.txt");

    f.setBinaryContent(new byte[] {1}, -1, 1000);
    f.setBinaryContent(new byte[] {2}, -1, 2000);

    dir.delete(this);

    revertChange(1);

    dir = myRoot.findChild("dir");
    assertNotNull(dir);
    f = dir.findChild("foo.txt");
    assertNotNull(f);
    assertEquals(1, f.contentsToByteArray()[0]);
    assertEquals(1000, f.getTimeStamp());
  }
 @Nullable
 public static VirtualFile createFileFromTemplate(
     final String templateName, final VirtualFile parent, final String fileName) {
   parent.refresh(false, false);
   final FileTemplate template = FileTemplateManager.getInstance().getJ2eeTemplate(templateName);
   try {
     final String text =
         template.getText(FileTemplateManager.getInstance().getDefaultProperties());
     VirtualFile file = parent.findChild(fileName);
     if (file == null) {
       file = parent.createChildData(AppEngineSupportProvider.class, fileName);
     }
     VfsUtil.saveText(file, text);
     return file;
   } catch (IOException e) {
     LOG.error(e);
     return null;
   }
 }
  public void testCopyDir() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile dirToCopy = fromVDir.createChildDirectory(this, "dir");
    final VirtualFile file = dirToCopy.createChildData(this, "temp_file");
    file.setBinaryContent(new byte[] {0, 1, 2, 3});
    final String newName = "dir";
    final VirtualFile dirCopy = dirToCopy.copy(this, toVDir, newName);
    assertEquals(newName, dirCopy.getName());
    PlatformTestUtil.assertDirectoriesEqual(toVDir, fromVDir);
  }
  public void testCopyFile() throws Exception {
    File fromDir = createTempDirectory();
    File toDir = createTempDirectory();

    VirtualFile fromVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(fromDir.getPath().replace(File.separatorChar, '/'));
    VirtualFile toVDir =
        LocalFileSystem.getInstance()
            .findFileByPath(toDir.getPath().replace(File.separatorChar, '/'));
    assertNotNull(fromVDir);
    assertNotNull(toVDir);
    final VirtualFile fileToCopy = fromVDir.createChildData(this, "temp_file");
    final byte[] byteContent = {0, 1, 2, 3};
    fileToCopy.setBinaryContent(byteContent);
    final String newName = "new_temp_file";
    final VirtualFile copy = fileToCopy.copy(this, toVDir, newName);
    assertEquals(newName, copy.getName());
    assertTrue(Arrays.equals(byteContent, copy.contentsToByteArray()));
  }
 @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
 public static VirtualFile flushWindows(TaskFile taskFile, VirtualFile file) {
   VirtualFile taskDir = file.getParent();
   VirtualFile fileWindows = null;
   final Document document = FileDocumentManager.getInstance().getDocument(file);
   if (document == null) {
     LOG.debug("Couldn't flush windows");
     return null;
   }
   if (taskDir != null) {
     String name = file.getNameWithoutExtension() + "_windows";
     PrintWriter printWriter = null;
     try {
       fileWindows = taskDir.createChildData(taskFile, name);
       printWriter = new PrintWriter(new FileOutputStream(fileWindows.getPath()));
       for (TaskWindow taskWindow : taskFile.getTaskWindows()) {
         if (!taskWindow.isValid(document)) {
           continue;
         }
         int start = taskWindow.getRealStartOffset(document);
         String windowDescription =
             document.getText(new TextRange(start, start + taskWindow.getLength()));
         printWriter.println("#study_plugin_window = " + windowDescription);
       }
       ApplicationManager.getApplication()
           .runWriteAction(
               new Runnable() {
                 @Override
                 public void run() {
                   FileDocumentManager.getInstance().saveDocument(document);
                 }
               });
     } catch (IOException e) {
       LOG.error(e);
     } finally {
       closeSilently(printWriter);
       synchronize();
     }
   }
   return fileWindows;
 }
示例#13
0
 @NotNull
 private static VirtualFile getOrCreateVirtualFile(
     @Nullable Object requestor, @NotNull File ioFile) throws IOException {
   VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
   if (virtualFile == null) {
     File parentFile = ioFile.getParentFile();
     // need refresh if the directory has just been created
     VirtualFile parentVirtualFile =
         parentFile == null
             ? null
             : LocalFileSystem.getInstance().refreshAndFindFileByIoFile(parentFile);
     if (parentVirtualFile == null) {
       throw new IOException(
           ProjectBundle.message(
               "project.configuration.save.file.not.found",
               parentFile == null ? "" : parentFile.getPath()));
     }
     virtualFile = parentVirtualFile.createChildData(requestor, ioFile.getName());
   }
   return virtualFile;
 }