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();
}
}
}
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;
}
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);
}
}
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);
}
}
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();
}
Aggregations