Search in sources :

Example 1 with CustomZipOutputStream

use of com.facebook.buck.zip.CustomZipOutputStream in project buck by facebook.

the class ProjectFilesystem method createZip.

/**
   * Takes a sequence of paths relative to the project root and writes a zip file to {@code out}
   * with the contents and structure that matches that of the specified paths.
   */
public void createZip(Collection<Path> pathsToIncludeInZip, Path out) throws IOException {
    try (CustomZipOutputStream zip = ZipOutputStreams.newOutputStream(out)) {
        for (Path path : pathsToIncludeInZip) {
            boolean isDirectory = isDirectory(path);
            CustomZipEntry entry = new CustomZipEntry(path, isDirectory);
            // We want deterministic ZIPs, so avoid mtimes.
            entry.setFakeTime();
            entry.setExternalAttributes(getFileAttributesForZipEntry(path));
            zip.putNextEntry(entry);
            if (!isDirectory) {
                try (InputStream input = newFileInputStream(path)) {
                    ByteStreams.copy(input, zip);
                }
            }
            zip.closeEntry();
        }
    }
}
Also used : Path(java.nio.file.Path) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) CustomZipEntry(com.facebook.buck.zip.CustomZipEntry)

Example 2 with CustomZipOutputStream

use of com.facebook.buck.zip.CustomZipOutputStream in project buck by facebook.

the class Jsr199Javac method buildWithClasspath.

@Override
public int buildWithClasspath(JavacExecutionContext context, BuildTarget invokingRule, ImmutableList<String> options, ImmutableList<ResolvedJavacPluginProperties> annotationProcessors, ImmutableSortedSet<Path> javaSourceFilePaths, Path pathToSrcsList, Optional<Path> workingDirectory, JavacOptions.AbiGenerationMode abiGenerationMode) {
    JavaCompiler compiler = createCompiler(context);
    CustomZipOutputStream jarOutputStream = null;
    StandardJavaFileManager fileManager = null;
    JavaInMemoryFileManager inMemoryFileManager = null;
    try {
        fileManager = compiler.getStandardFileManager(null, null, null);
        Supplier<ImmutableSet<String>> alreadyAddedFilesAvailableAfterCompilation = Suppliers.ofInstance(ImmutableSet.of());
        if (context.getDirectToJarOutputSettings().isPresent()) {
            Path path = context.getProjectFilesystem().getPathForRelativePath(context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath());
            jarOutputStream = ZipOutputStreams.newOutputStream(path, ZipOutputStreams.HandleDuplicates.APPEND_TO_ZIP);
            inMemoryFileManager = new JavaInMemoryFileManager(fileManager, path, jarOutputStream, context.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar());
            alreadyAddedFilesAvailableAfterCompilation = inMemoryFileManager::getEntries;
            fileManager = inMemoryFileManager;
        }
        Iterable<? extends JavaFileObject> compilationUnits;
        try {
            compilationUnits = createCompilationUnits(fileManager, context.getProjectFilesystem()::resolve, javaSourceFilePaths);
        } catch (IOException e) {
            LOG.warn(e, "Error building compilation units");
            return 1;
        }
        try {
            int result = buildWithClasspath(context, invokingRule, options, annotationProcessors, javaSourceFilePaths, pathToSrcsList, compiler, fileManager, compilationUnits, abiGenerationMode);
            if (result != 0 || !context.getDirectToJarOutputSettings().isPresent()) {
                return result;
            }
            return JarDirectoryStepHelper.createJarFile(context.getProjectFilesystem(), context.getDirectToJarOutputSettings().get().getDirectToJarOutputPath(), Preconditions.checkNotNull(jarOutputStream), context.getDirectToJarOutputSettings().get().getEntriesToJar(), alreadyAddedFilesAvailableAfterCompilation.get(), context.getDirectToJarOutputSettings().get().getMainClass(), context.getDirectToJarOutputSettings().get().getManifestFile(), /* mergeManifests */
            true, /* blacklist */
            ImmutableSet.of(), context.getEventSink(), context.getStdErr());
        } finally {
            close(compilationUnits);
        }
    } catch (IOException e) {
        LOG.warn(e, "Unable to create jarOutputStream");
    } finally {
        closeResources(fileManager, inMemoryFileManager, jarOutputStream);
    }
    return 1;
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) IOException(java.io.IOException)

Example 3 with CustomZipOutputStream

use of com.facebook.buck.zip.CustomZipOutputStream in project buck by facebook.

the class DefaultDefectReporter method writeReport.

private void writeReport(DefectReport defectReport, OutputStream outputStream) throws IOException {
    try (BufferedOutputStream baseOut = new BufferedOutputStream(outputStream);
        CustomZipOutputStream out = ZipOutputStreams.newOutputStream(baseOut, APPEND_TO_ZIP)) {
        if (defectReport.getSourceControlInfo().isPresent() && defectReport.getSourceControlInfo().get().getDiff().isPresent()) {
            addStringsAsFilesToArchive(out, ImmutableMap.of(DIFF_FILE_NAME, defectReport.getSourceControlInfo().get().getDiff().get()));
        }
        addFilesToArchive(out, defectReport.getIncludedPaths());
        out.putNextEntry(new CustomZipEntry(REPORT_FILE_NAME));
        objectMapper.writeValue(out, defectReport);
    }
}
Also used : CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) CustomZipEntry(com.facebook.buck.zip.CustomZipEntry) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with CustomZipOutputStream

use of com.facebook.buck.zip.CustomZipOutputStream in project buck by facebook.

the class JarDirectoryStepTest method entriesFromTheGivenManifestShouldOverrideThoseInTheJars.

@Test
public void entriesFromTheGivenManifestShouldOverrideThoseInTheJars() throws IOException {
    String expected = "1.4";
    // Write the manifest, setting the implementation version
    Path tmp = folder.newFolder();
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), expected);
    Path manifestFile = tmp.resolve("manifest");
    try (OutputStream fos = Files.newOutputStream(manifestFile)) {
        manifest.write(fos);
    }
    // Write another manifest, setting the implementation version to something else
    manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), "1.0");
    Path input = tmp.resolve("input.jar");
    try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(input)) {
        ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
        out.putNextEntry(entry);
        manifest.write(out);
    }
    Path output = tmp.resolve("output.jar");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(tmp), output, ImmutableSortedSet.of(Paths.get("input.jar")), /* main class */
    null, tmp.resolve("manifest"), /* merge manifest */
    true, /* blacklist */
    ImmutableSet.of());
    ExecutionContext context = TestExecutionContext.newInstance();
    assertEquals(0, step.execute(context).getExitCode());
    try (Zip zip = new Zip(output, false)) {
        byte[] rawManifest = zip.readFully("META-INF/MANIFEST.MF");
        manifest = new Manifest(new ByteArrayInputStream(rawManifest));
        String version = manifest.getMainAttributes().getValue(IMPLEMENTATION_VERSION);
        assertEquals(expected, version);
    }
}
Also used : Path(java.nio.file.Path) Zip(com.facebook.buck.testutil.Zip) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) ZipEntry(java.util.zip.ZipEntry) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 5 with CustomZipOutputStream

use of com.facebook.buck.zip.CustomZipOutputStream in project buck by facebook.

the class ProGuardObfuscateStep method createEmptyZip.

@VisibleForTesting
static void createEmptyZip(Path file) throws IOException {
    Files.createDirectories(file.getParent());
    CustomZipOutputStream out = ZipOutputStreams.newOutputStream(file);
    // Sun's java 6 runtime doesn't allow us to create a truly empty zip, but this should be enough
    // to pass through dx/split-zip without any issue.
    // ...and Sun's java 7 runtime doesn't let us use an empty string for the zip entry name.
    out.putNextEntry(new ZipEntry("proguard_no_result"));
    out.close();
}
Also used : ZipEntry(java.util.zip.ZipEntry) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

CustomZipOutputStream (com.facebook.buck.zip.CustomZipOutputStream)7 Path (java.nio.file.Path)5 CustomZipEntry (com.facebook.buck.zip.CustomZipEntry)3 ZipEntry (java.util.zip.ZipEntry)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 BorrowablePath (com.facebook.buck.io.BorrowablePath)1 LazyPath (com.facebook.buck.io.LazyPath)1 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)1 PathSourcePath (com.facebook.buck.rules.PathSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 ExecutionContext (com.facebook.buck.step.ExecutionContext)1 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 Zip (com.facebook.buck.testutil.Zip)1 Clock (com.facebook.buck.timing.Clock)1 FakeClock (com.facebook.buck.timing.FakeClock)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1