Ejemplo n.º 1
0
 @Override
 public IFile createFile(String path) {
   File file = new File(context.absPath(path));
   String name = file.getName();
   String parentPath = file.getParent();
   try {
     VfsUtil.createDirectories(parentPath);
   } catch (IOException e) {
     Flog.error("Create directories error %s", e);
     context.errorMessage("The Floobits plugin was unable to create directories for file.");
     return null;
   }
   VirtualFile parent = LocalFileSystem.getInstance().findFileByPath(parentPath);
   if (parent == null) {
     Flog.error("Virtual file is null? %s", parentPath);
     return null;
   }
   VirtualFile newFile;
   try {
     newFile = parent.findOrCreateChildData(context, name);
   } catch (Throwable e) {
     Flog.error("Create file error %s", e);
     context.errorMessage(
         String.format("The Floobits plugin was unable to create a file: %s.", path));
     return null;
   }
   return new FileImpl(newFile);
 }
Ejemplo n.º 2
0
  @Override
  public IFile createDirectories(String path) {
    VirtualFile directory = null;
    try {
      directory = VfsUtil.createDirectories(path);
    } catch (IOException e) {
      Flog.error(e);
    }

    if (directory == null) {
      Flog.warn("Failed to create directories %s %s", path);
      return null;
    }
    return new FileImpl(directory);
  }
  @Override
  public PsiFile addFileToProject(
      @NonNls String rootPath, @NonNls final String relativePath, @NonNls final String fileText)
      throws IOException {
    final VirtualFile dir =
        VfsUtil.createDirectories(rootPath + "/" + PathUtil.getParentPath(relativePath));

    final VirtualFile[] virtualFile = new VirtualFile[1];
    new WriteCommandAction.Simple(getProject()) {
      @Override
      protected void run() throws Throwable {
        virtualFile[0] = dir.createChildData(this, StringUtil.getShortName(relativePath, '/'));
        VfsUtil.saveText(virtualFile[0], fileText);
      }
    }.execute();
    return ApplicationManager.getApplication()
        .runReadAction(
            new Computable<PsiFile>() {
              public PsiFile compute() {
                return PsiManager.getInstance(getProject()).findFile(virtualFile[0]);
              }
            });
  }