Search in sources :

Example 1 with CompositeStep

use of com.facebook.buck.step.CompositeStep in project buck by facebook.

the class SmartDexingStep method createDxStepForDxPseudoRule.

/**
   * The step to produce the .dex file will be determined by the file extension of outputPath, much
   * as {@code dx} itself chooses whether to embed the dex inside a jar/zip based on the destination
   * file passed to it.  We also create a ".meta" file that contains information about the
   * compressed and uncompressed size of the dex; this information is useful later, in applications,
   * when unpacking.
   */
static Step createDxStepForDxPseudoRule(ProjectFilesystem filesystem, Collection<Path> filesToDex, Path outputPath, EnumSet<Option> dxOptions, Optional<Integer> xzCompressionLevel, Optional<String> dxMaxHeapSize) {
    String output = outputPath.toString();
    List<Step> steps = Lists.newArrayList();
    if (DexStore.XZ.matchesPath(outputPath)) {
        Path tempDexJarOutput = Paths.get(output.replaceAll("\\.jar\\.xz$", ".tmp.jar"));
        steps.add(new DxStep(filesystem, tempDexJarOutput, filesToDex, dxOptions, dxMaxHeapSize));
        // We need to make sure classes.dex is STOREd in the .dex.jar file, otherwise .XZ
        // compression won't be effective.
        Path repackedJar = Paths.get(output.replaceAll("\\.xz$", ""));
        steps.add(new RepackZipEntriesStep(filesystem, tempDexJarOutput, repackedJar, ImmutableSet.of("classes.dex"), ZipCompressionLevel.MIN_COMPRESSION_LEVEL));
        steps.add(new RmStep(filesystem, tempDexJarOutput));
        steps.add(new DexJarAnalysisStep(filesystem, repackedJar, repackedJar.resolveSibling(repackedJar.getFileName() + ".meta")));
        if (xzCompressionLevel.isPresent()) {
            steps.add(new XzStep(filesystem, repackedJar, xzCompressionLevel.get().intValue()));
        } else {
            steps.add(new XzStep(filesystem, repackedJar));
        }
    } else if (DexStore.XZS.matchesPath(outputPath)) {
        // Essentially the same logic as the XZ case above, except we compress later.
        // The differences in output file names make it worth separating into a different case.
        // Ensure classes.dex is stored.
        Path tempDexJarOutput = Paths.get(output.replaceAll("\\.jar\\.xzs\\.tmp~$", ".tmp.jar"));
        steps.add(new DxStep(filesystem, tempDexJarOutput, filesToDex, dxOptions, dxMaxHeapSize));
        steps.add(new RepackZipEntriesStep(filesystem, tempDexJarOutput, outputPath, ImmutableSet.of("classes.dex"), ZipCompressionLevel.MIN_COMPRESSION_LEVEL));
        steps.add(new RmStep(filesystem, tempDexJarOutput));
        // Write a .meta file.
        steps.add(new DexJarAnalysisStep(filesystem, outputPath, outputPath.resolveSibling(outputPath.getFileName() + ".meta")));
    } else if (DexStore.JAR.matchesPath(outputPath) || DexStore.RAW.matchesPath(outputPath) || output.endsWith("classes.dex")) {
        steps.add(new DxStep(filesystem, outputPath, filesToDex, dxOptions, dxMaxHeapSize));
        if (DexStore.JAR.matchesPath(outputPath)) {
            steps.add(new DexJarAnalysisStep(filesystem, outputPath, outputPath.resolveSibling(outputPath.getFileName() + ".meta")));
        }
    } else {
        throw new IllegalArgumentException(String.format("Suffix of %s does not have a corresponding DexStore type.", outputPath));
    }
    return new CompositeStep(steps);
}
Also used : Path(java.nio.file.Path) RmStep(com.facebook.buck.step.fs.RmStep) XzStep(com.facebook.buck.step.fs.XzStep) RmStep(com.facebook.buck.step.fs.RmStep) Step(com.facebook.buck.step.Step) RepackZipEntriesStep(com.facebook.buck.zip.RepackZipEntriesStep) CompositeStep(com.facebook.buck.step.CompositeStep) XzStep(com.facebook.buck.step.fs.XzStep) WriteFileStep(com.facebook.buck.step.fs.WriteFileStep) RepackZipEntriesStep(com.facebook.buck.zip.RepackZipEntriesStep) CompositeStep(com.facebook.buck.step.CompositeStep)

Example 2 with CompositeStep

use of com.facebook.buck.step.CompositeStep in project buck by facebook.

the class SmartDexingStepTest method testCreateDxStepForDxPseudoRuleWithXzOutputNonDefaultCompression.

@Test
public void testCreateDxStepForDxPseudoRuleWithXzOutputNonDefaultCompression() throws IOException {
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    ImmutableList<Path> filesToDex = ImmutableList.of(Paths.get("foo.dex.jar"), Paths.get("bar.dex.jar"));
    Path outputPath = Paths.get("classes.dex.jar.xz");
    EnumSet<DxStep.Option> dxOptions = EnumSet.noneOf(DxStep.Option.class);
    Step dxStep = SmartDexingStep.createDxStepForDxPseudoRule(filesystem, filesToDex, outputPath, dxOptions, Optional.of(9), Optional.empty());
    assertTrue("Result should be a CompositeStep.", dxStep instanceof CompositeStep);
    List<Step> steps = ImmutableList.copyOf((CompositeStep) dxStep);
    MoreAsserts.assertSteps("Steps should repack zip entries and then compress using xz.", ImmutableList.of(Joiner.on(" ").join("(cd", filesystem.getRootPath(), "&&", Paths.get("/usr/bin/dx"), "--dex --output", filesystem.resolve("classes.dex.tmp.jar"), filesystem.resolve("foo.dex.jar"), filesystem.resolve("bar.dex.jar") + ")"), "repack classes.dex.tmp.jar in classes.dex.jar", "rm -f " + filesystem.resolve("classes.dex.tmp.jar"), "dex_meta dexPath:classes.dex.jar dexMetaPath:classes.dex.jar.meta", "xz -z -9 --check=crc32 classes.dex.jar"), steps, createMockedExecutionContext());
    verifyAll();
}
Also used : Path(java.nio.file.Path) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Step(com.facebook.buck.step.Step) CompositeStep(com.facebook.buck.step.CompositeStep) CompositeStep(com.facebook.buck.step.CompositeStep) Test(org.junit.Test)

Example 3 with CompositeStep

use of com.facebook.buck.step.CompositeStep in project buck by facebook.

the class SmartDexingStepTest method testCreateDxStepForDxPseudoRuleWithXzOutput.

@Test
public void testCreateDxStepForDxPseudoRuleWithXzOutput() throws IOException {
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    ImmutableList<Path> filesToDex = ImmutableList.of(Paths.get("foo.dex.jar"), Paths.get("bar.dex.jar"));
    Path outputPath = Paths.get("classes.dex.jar.xz");
    EnumSet<DxStep.Option> dxOptions = EnumSet.noneOf(DxStep.Option.class);
    Step dxStep = SmartDexingStep.createDxStepForDxPseudoRule(filesystem, filesToDex, outputPath, dxOptions, Optional.empty(), Optional.empty());
    assertTrue("Result should be a CompositeStep.", dxStep instanceof CompositeStep);
    List<Step> steps = ImmutableList.copyOf((CompositeStep) dxStep);
    MoreAsserts.assertSteps("Steps should repack zip entries and then compress using xz.", ImmutableList.of(Joiner.on(" ").join("(cd", filesystem.getRootPath(), "&&", Paths.get("/usr/bin/dx"), "--dex --output", filesystem.resolve("classes.dex.tmp.jar"), filesystem.resolve("foo.dex.jar"), filesystem.resolve("bar.dex.jar") + ")"), "repack classes.dex.tmp.jar in classes.dex.jar", "rm -f " + filesystem.resolve("classes.dex.tmp.jar"), "dex_meta dexPath:classes.dex.jar dexMetaPath:classes.dex.jar.meta", "xz -z -4 --check=crc32 classes.dex.jar"), steps, createMockedExecutionContext());
    verifyAll();
}
Also used : Path(java.nio.file.Path) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Step(com.facebook.buck.step.Step) CompositeStep(com.facebook.buck.step.CompositeStep) CompositeStep(com.facebook.buck.step.CompositeStep) Test(org.junit.Test)

Aggregations

CompositeStep (com.facebook.buck.step.CompositeStep)3 Step (com.facebook.buck.step.Step)3 Path (java.nio.file.Path)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)2 Test (org.junit.Test)2 RmStep (com.facebook.buck.step.fs.RmStep)1 WriteFileStep (com.facebook.buck.step.fs.WriteFileStep)1 XzStep (com.facebook.buck.step.fs.XzStep)1 RepackZipEntriesStep (com.facebook.buck.zip.RepackZipEntriesStep)1