Esempio n. 1
0
  /**
   * This will copy the template directory, renaming files named {@code BUCK.test} to {@code BUCK}
   * in the process. Files whose names end in {@code .expected} will not be copied.
   */
  public void setUp() throws IOException {
    DefaultKnownBuildRuleTypes.resetInstance();

    MoreFiles.copyRecursively(templatePath, destPath, BUILD_FILE_RENAME);

    // If there's a local.properties in the host project but not in the destination, make a copy.
    Path localProperties = FileSystems.getDefault().getPath("local.properties");
    Path destLocalProperties = destPath.resolve(localProperties.getFileName());

    if (localProperties.toFile().exists() && !destLocalProperties.toFile().exists()) {
      java.nio.file.Files.copy(localProperties, destLocalProperties);
    }

    if (Platform.detect() == Platform.WINDOWS) {
      // Hack for symlinks on Windows.
      SimpleFileVisitor<Path> copyDirVisitor =
          new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
                throws IOException {
              // On Windows, symbolic links from git repository are checked out as normal files
              // containing a one-line path. In order to distinguish them, paths are read and
              // pointed
              // files are trued to locate. Once the pointed file is found, it will be copied to
              // target.
              // On NTFS length of path must be greater than 0 and less than 4096.
              if (attrs.size() > 0 && attrs.size() <= 4096) {
                File file = path.toFile();
                String linkTo = Files.toString(file, Charsets.UTF_8);
                File linkToFile = new File(templatePath.toFile(), linkTo);
                if (linkToFile.isFile()) {
                  java.nio.file.Files.copy(
                      linkToFile.toPath(), path, StandardCopyOption.REPLACE_EXISTING);
                } else if (linkToFile.isDirectory()) {
                  if (!file.delete()) {
                    throw new IOException();
                  }
                  MoreFiles.copyRecursively(linkToFile.toPath(), path);
                }
              }
              return FileVisitResult.CONTINUE;
            }
          };
      java.nio.file.Files.walkFileTree(destPath, copyDirVisitor);
    }
    isSetUp = true;
  }