@Override
  public int execute(ExecutionContext context) {
    File inputDirectory = inputDirectoryPath.toFile();
    Preconditions.checkState(
        inputDirectory.exists() && inputDirectory.isDirectory(),
        "%s must be a directory.",
        inputDirectoryPath);

    try {
      ImmutableMap.Builder<File, ZipEntry> zipEntriesBuilder = ImmutableMap.builder();
      addDirectoryToZipEntryList(inputDirectory, "", zipEntriesBuilder);
      ImmutableMap<File, ZipEntry> zipEntries = zipEntriesBuilder.build();

      if (!zipEntries.isEmpty()) {
        try (CustomZipOutputStream outputStream =
            ZipOutputStreams.newOutputStream(outputZipPath.toFile())) {
          for (Map.Entry<File, ZipEntry> zipEntry : zipEntries.entrySet()) {
            outputStream.putNextEntry(zipEntry.getValue());
            ByteStreams.copy(Files.newInputStreamSupplier(zipEntry.getKey()), outputStream);
            outputStream.closeEntry();
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace(context.getStdErr());
      return 1;
    }
    return 0;
  }
Example #2
0
 @Override
 public int execute(ExecutionContext context) {
   try {
     extractZipFile(
         pathToZipFile, pathToDestinationDirectory, filesToExtract, overwriteExistingFiles);
   } catch (IOException e) {
     e.printStackTrace(context.getStdErr());
     return 1;
   }
   return 0;
 }
Example #3
0
 @Override
 public int execute(ExecutionContext context) {
   try (InputStream sourceStream = source.openStream()) {
     context
         .getProjectFilesystem()
         .copyToPath(sourceStream, outputPath, StandardCopyOption.REPLACE_EXISTING);
     return 0;
   } catch (IOException e) {
     LOG.error(e, "Couldn't copy bytes to %s", outputPath);
     e.printStackTrace(context.getStdErr());
     return 1;
   }
 }
  @Override
  public int execute(ExecutionContext context) {
    List<String> lines = Lists.newArrayList();
    // Run with -e so the script will fail if any of the steps fail.
    lines.add("#!/bin/sh");
    lines.add("set -e");

    // Create a tmp directory that will be deleted when this script exits.
    lines.add("BUCK_PROJECT_ROOT=`mktemp -d -t sh_binary.XXXXXXXXXX`");
    lines.add(
        "trap \"chmod -R 755 $BUCK_PROJECT_ROOT "
            + "&& rm -rf $BUCK_PROJECT_ROOT\" EXIT HUP INT TERM");

    // Navigate to the tmp directory.
    lines.add("cd $BUCK_PROJECT_ROOT");

    // Symlink the resources to the $BUCK_PROJECT_ROOT directory.
    Function<String, String> pathRelativizer = context.getProjectFilesystem().getPathRelativizer();
    createSymlinkCommands(resources, pathRelativizer, lines);

    // Make everything in $BUCK_PROJECT_ROOT read-only.
    lines.add("find $BUCK_PROJECT_ROOT -type d -exec chmod 555 {} \\;");
    lines.add("find $BUCK_PROJECT_ROOT -type f -exec chmod 444 {} \\;");

    // Forward the args to this generated script to scriptToRun and execute it.
    lines.add(
        String.format(
            "BUCK_PROJECT_ROOT=$BUCK_PROJECT_ROOT %s \"$@\"",
            pathRelativizer.apply(scriptToRun.toString())));

    // Write the contents to the file.
    File output = context.getProjectFilesystem().getFileForRelativePath(outputFile.toString());
    try {
      Files.write(Joiner.on('\n').join(lines) + '\n', output, Charsets.UTF_8);
    } catch (IOException e) {
      e.printStackTrace(context.getStdErr());
      return 1;
    }

    // Make sure the file is executable.
    if (output.setExecutable(/* executable */ true, /* ownerOnly */ false)) {
      return 0;
    } else {
      context.getConsole().printErrorText("Failed to set file as executable: " + output);
      return 1;
    }
  }