public static void extractEntry(
      ZipEntry entry, final InputStream inputStream, File outputDir, boolean overwrite)
      throws IOException {
    final boolean isDirectory = entry.isDirectory();

    String entryName = entry.getName();
    String relativeName = entryName.substring(JAVASCRIPT_GEN_DIR.length() + 1, entryName.length());

    final File file = new File(outputDir, relativeName);
    if (file.exists() && !overwrite) return;

    FileUtil.createParentDirs(file);
    if (isDirectory) {
      file.mkdir();
    } else {
      final BufferedInputStream is = new BufferedInputStream(inputStream);
      final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
      try {
        FileUtil.copy(is, os);
      } finally {
        os.close();
        is.close();
      }
    }
  }
 private static FileOutputStream openOutputStream(@NotNull final File file) throws IOException {
   try {
     return new FileOutputStream(file);
   } catch (FileNotFoundException e) {
     final File parentFile = file.getParentFile();
     if (parentFile == null) {
       throw new IOException("Parent file is null for " + file.getPath(), e);
     }
     createParentDirs(file);
     return new FileOutputStream(file);
   }
 }
  private static void writeToFile(
      @NotNull File file, @NotNull byte[] text, int off, int len, boolean append)
      throws IOException {
    createParentDirs(file);

    OutputStream stream = new FileOutputStream(file, append);
    try {
      stream.write(text, off, len);
    } finally {
      stream.close();
    }
  }
  @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;
  }
예제 #5
0
 /**
  * It's not possible to store username/password in the test file, this cretentials are stored in a
  * properties file under user home directory.
  *
  * <p>This method would be used to fetch parameters for the test and allow to avoid committing
  * createntials with source file.
  *
  * @return username, repo, password
  */
 @NotNull
 public static Properties readGitHubAccount() {
   File propsFile = new File(System.getenv("USERPROFILE"), ".github.test.account");
   System.out.println("Loading properites from: " + propsFile);
   try {
     if (!propsFile.exists()) {
       FileUtil.createParentDirs(propsFile);
       Properties ps = new Properties();
       ps.setProperty(URL, "https://api.github.com");
       ps.setProperty(USERNAME, "jonnyzzz");
       ps.setProperty(REPOSITORY, "TeamCity.GitHub");
       ps.setProperty(PASSWORD_REV, rewind("some-password-written-end-to-front"));
       PropertiesUtil.storeProperties(ps, propsFile, "mock properties");
       return ps;
     } else {
       return PropertiesUtil.loadProperties(propsFile);
     }
   } catch (IOException e) {
     throw new RuntimeException("Could not read Amazon Access properties: " + e.getMessage(), e);
   }
 }
  @Nullable
  private Pair<String, String> moveToRealLocation(
      String tempOutputDir,
      String pathToClass,
      VirtualFile sourceFile,
      final List<File> filesToRefresh) {
    final Module module = myCompileContext.getModuleByFile(sourceFile);
    if (module == null) {
      final String message =
          "Cannot determine module for source file: "
              + sourceFile.getPresentableUrl()
              + ";\nCorresponding output file: "
              + pathToClass;
      LOG.info(message);
      myCompileContext.addMessage(
          CompilerMessageCategory.WARNING, message, sourceFile.getUrl(), -1, -1);
      // do not move: looks like source file has been invalidated, need recompilation
      return new Pair<String, String>(tempOutputDir, pathToClass);
    }
    final String realOutputDir;
    if (myCompileContext.isInTestSourceContent(sourceFile)) {
      realOutputDir = getTestsOutputDir(module);
      LOG.assertTrue(realOutputDir != null);
    } else {
      realOutputDir = getOutputDir(module);
      LOG.assertTrue(realOutputDir != null);
    }

    if (FileUtil.pathsEqual(tempOutputDir, realOutputDir)) { // no need to move
      filesToRefresh.add(new File(pathToClass));
      return new Pair<String, String>(realOutputDir, pathToClass);
    }

    final String realPathToClass = realOutputDir + pathToClass.substring(tempOutputDir.length());
    final File fromFile = new File(pathToClass);
    final File toFile = new File(realPathToClass);

    boolean success = fromFile.renameTo(toFile);
    if (!success) {
      // assuming cause of the fail: intermediate dirs do not exist
      FileUtil.createParentDirs(toFile);
      // retry after making non-existent dirs
      success = fromFile.renameTo(toFile);
    }
    if (!success) { // failed to move the file: e.g. because source and destination reside on
                    // different mountpoints.
      try {
        FileUtil.copy(fromFile, toFile);
        FileUtil.delete(fromFile);
        success = true;
      } catch (IOException e) {
        LOG.info(e);
        success = false;
      }
    }
    if (success) {
      filesToRefresh.add(toFile);
      return new Pair<String, String>(realOutputDir, realPathToClass);
    }
    return null;
  }