Search in sources :

Example 11 with StepExecutionResult

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

the class PreDexMerge method addMetadataWriteStep.

private void addMetadataWriteStep(final PreDexedFilesSorter.Result result, final ImmutableList.Builder<Step> steps, final Path metadataFilePath) {
    StringBuilder nameBuilder = new StringBuilder(30);
    final boolean isRootModule = result.apkModule.equals(apkModuleGraph.getRootAPKModule());
    final String storeId = result.apkModule.getName();
    nameBuilder.append("write_");
    if (!isRootModule) {
        nameBuilder.append(storeId);
        nameBuilder.append("_");
    }
    nameBuilder.append("metadata_txt");
    steps.add(new AbstractExecutionStep(nameBuilder.toString()) {

        @Override
        public StepExecutionResult execute(ExecutionContext executionContext) {
            Map<Path, DexWithClasses> metadataTxtEntries = result.metadataTxtDexEntries;
            List<String> lines = Lists.newArrayListWithCapacity(metadataTxtEntries.size());
            lines.add(".id " + storeId);
            if (isRootModule) {
                if (dexSplitMode.getDexStore() == DexStore.RAW) {
                    lines.add(".root_relative");
                }
            } else {
                for (APKModule dependency : apkModuleGraph.getGraph().getOutgoingNodesFor(result.apkModule)) {
                    lines.add(".requires " + dependency.getName());
                }
            }
            try {
                for (Map.Entry<Path, DexWithClasses> entry : metadataTxtEntries.entrySet()) {
                    Path pathToSecondaryDex = entry.getKey();
                    String containedClass = Iterables.get(entry.getValue().getClassNames(), 0);
                    containedClass = containedClass.replace('/', '.');
                    Sha1HashCode hash = getProjectFilesystem().computeSha1(pathToSecondaryDex);
                    lines.add(String.format("%s %s %s", pathToSecondaryDex.getFileName(), hash, containedClass));
                }
                getProjectFilesystem().writeLinesToPath(lines, metadataFilePath);
            } catch (IOException e) {
                executionContext.logError(e, "Failed when writing metadata.txt multi-dex.");
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    });
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) IOException(java.io.IOException) ExecutionContext(com.facebook.buck.step.ExecutionContext) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractMap(java.util.AbstractMap)

Example 12 with StepExecutionResult

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

the class UnzipAar method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), unpackDirectory));
    steps.add(new UnzipStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(aarFile), unpackDirectory));
    steps.add(new TouchStep(getProjectFilesystem(), getProguardConfig()));
    steps.add(new MkdirStep(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(getAssetsDirectory())));
    steps.add(new MkdirStep(getProjectFilesystem(), getNativeLibsDirectory()));
    steps.add(new TouchStep(getProjectFilesystem(), getTextSymbolsFile()));
    // We take the classes.jar file that is required to exist in an .aar and merge it with any
    // .jar files under libs/ into an "uber" jar. We do this for simplicity because we do not know
    // how many entries there are in libs/ at graph enhancement time, but we need to make sure
    // that all of the .class files in the .aar get packaged. As it is implemented today, an
    // android_library that depends on an android_prebuilt_aar can compile against anything in the
    // .aar's classes.jar or libs/.
    steps.add(new MkdirStep(getProjectFilesystem(), uberClassesJar.getParent()));
    steps.add(new AbstractExecutionStep("create_uber_classes_jar") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            Path libsDirectory = unpackDirectory.resolve("libs");
            boolean dirDoesNotExistOrIsEmpty;
            if (!getProjectFilesystem().exists(libsDirectory)) {
                dirDoesNotExistOrIsEmpty = true;
            } else {
                try {
                    dirDoesNotExistOrIsEmpty = getProjectFilesystem().getDirectoryContents(libsDirectory).isEmpty();
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
            }
            Path classesJar = unpackDirectory.resolve("classes.jar");
            JavacEventSinkToBuckEventBusBridge eventSink = new JavacEventSinkToBuckEventBusBridge(context.getBuckEventBus());
            if (!getProjectFilesystem().exists(classesJar)) {
                try {
                    JarDirectoryStepHelper.createEmptyJarFile(getProjectFilesystem(), classesJar, eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to create empty jar %s", classesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            if (dirDoesNotExistOrIsEmpty) {
                try {
                    getProjectFilesystem().copy(classesJar, uberClassesJar, ProjectFilesystem.CopySourceMode.FILE);
                } catch (IOException e) {
                    context.logError(e, "Failed to copy from %s to %s", classesJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            } else {
                // Glob all of the contents from classes.jar and the entries in libs/ into a single JAR.
                ImmutableSortedSet.Builder<Path> entriesToJarBuilder = ImmutableSortedSet.naturalOrder();
                entriesToJarBuilder.add(classesJar);
                try {
                    entriesToJarBuilder.addAll(getProjectFilesystem().getDirectoryContents(libsDirectory));
                } catch (IOException e) {
                    context.logError(e, "Failed to get directory contents of %s", libsDirectory);
                    return StepExecutionResult.ERROR;
                }
                ImmutableSortedSet<Path> entriesToJar = entriesToJarBuilder.build();
                try {
                    JarDirectoryStepHelper.createJarFile(getProjectFilesystem(), uberClassesJar, entriesToJar, /* mainClass */
                    Optional.empty(), /* manifestFile */
                    Optional.empty(), /* mergeManifests */
                    true, /* blacklist */
                    ImmutableSet.of(), eventSink, context.getStdErr());
                } catch (IOException e) {
                    context.logError(e, "Failed to jar %s into %s", entriesToJar, uberClassesJar);
                    return StepExecutionResult.ERROR;
                }
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTextSymbolsDir));
    steps.add(new ExtractFromAndroidManifestStep(getAndroidManifest(), getProjectFilesystem(), buildableContext, METADATA_KEY_FOR_R_DOT_JAVA_PACKAGE, pathToRDotJavaPackageFile));
    steps.add(CopyStep.forFile(getProjectFilesystem(), getTextSymbolsFile(), pathToTextSymbolsFile));
    buildableContext.recordArtifact(unpackDirectory);
    buildableContext.recordArtifact(uberClassesJar);
    buildableContext.recordArtifact(pathToTextSymbolsFile);
    buildableContext.recordArtifact(pathToRDotJavaPackageFile);
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) ExplicitBuildTargetSourcePath(com.facebook.buck.rules.ExplicitBuildTargetSourcePath) UnzipStep(com.facebook.buck.zip.UnzipStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) MkdirStep(com.facebook.buck.step.fs.MkdirStep) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) UnzipStep(com.facebook.buck.zip.UnzipStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) TouchStep(com.facebook.buck.step.fs.TouchStep) TouchStep(com.facebook.buck.step.fs.TouchStep) IOException(java.io.IOException) JavacEventSinkToBuckEventBusBridge(com.facebook.buck.jvm.java.JavacEventSinkToBuckEventBusBridge) ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep)

Example 13 with StepExecutionResult

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

the class GenerateCodeCoverageReportStepTest method testJarFileIsExtracted.

@Test
public void testJarFileIsExtracted() throws Throwable {
    final File[] extractedDir = new File[2];
    step = new GenerateCodeCoverageReportStep(new ExternalJavaRuntimeLauncher("/baz/qux/java"), filesystem, SOURCE_DIRECTORIES, jarFiles, Paths.get(OUTPUT_DIRECTORY), CoverageReportFormat.HTML, "TitleFoo", Optional.empty(), Optional.empty()) {

        @Override
        StepExecutionResult executeInternal(ExecutionContext context, Set<Path> jarFiles) {
            for (int i = 0; i < 2; i++) {
                extractedDir[i] = new ArrayList<>(jarFiles).get(i).toFile();
                assertTrue(extractedDir[i].isDirectory());
                assertTrue(new File(extractedDir[i], "com/facebook/testing/coverage/Foo.class").exists());
            }
            return null;
        }
    };
    step.execute(TestExecutionContext.newInstance());
    assertFalse(extractedDir[0].exists());
    assertFalse(extractedDir[1].exists());
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 14 with StepExecutionResult

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

the class GenerateCodeCoverageReportStepTest method testClassesDirIsUntouched.

@Test
public void testClassesDirIsUntouched() throws Throwable {
    final File classesDir = tmp.newFolder("classesDir");
    jarFiles.clear();
    jarFiles.add(classesDir.toPath());
    step = new GenerateCodeCoverageReportStep(new ExternalJavaRuntimeLauncher("/baz/qux/java"), filesystem, SOURCE_DIRECTORIES, jarFiles, Paths.get(OUTPUT_DIRECTORY), CoverageReportFormat.HTML, "TitleFoo", Optional.empty(), Optional.empty()) {

        @Override
        StepExecutionResult executeInternal(ExecutionContext context, Set<Path> jarFiles) {
            assertEquals(1, jarFiles.size());
            assertEquals(classesDir.toPath(), jarFiles.iterator().next());
            return null;
        }
    };
    step.execute(TestExecutionContext.newInstance());
    assertTrue(classesDir.exists());
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) File(java.io.File) Test(org.junit.Test)

Example 15 with StepExecutionResult

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

the class JavacStepTest method successfulCompileDoesNotSendStdoutAndStderrToConsole.

@Test
public void successfulCompileDoesNotSendStdoutAndStderrToConsole() throws Exception {
    FakeJavac fakeJavac = new FakeJavac();
    BuildRuleResolver buildRuleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(buildRuleResolver);
    SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
    ProjectFilesystem fakeFilesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    JavacOptions javacOptions = JavacOptions.builder().setSourceLevel("8.0").setTargetLevel("8.0").build();
    ClasspathChecker classpathChecker = new ClasspathChecker("/", ":", Paths::get, dir -> false, file -> false, (path, glob) -> ImmutableSet.of());
    JavacStep step = new JavacStep(Paths.get("output"), NoOpClassUsageFileWriter.instance(), Optional.empty(), ImmutableSortedSet.of(), Paths.get("pathToSrcsList"), ImmutableSortedSet.of(), fakeJavac, javacOptions, BuildTargetFactory.newInstance("//foo:bar"), Optional.empty(), sourcePathResolver, ruleFinder, fakeFilesystem, classpathChecker, Optional.empty());
    FakeProcess fakeJavacProcess = new FakeProcess(0, "javac stdout\n", "javac stderr\n");
    ExecutionContext executionContext = TestExecutionContext.newBuilder().setProcessExecutor(new FakeProcessExecutor(Functions.constant(fakeJavacProcess), new TestConsole())).build();
    BuckEventBusFactory.CapturingConsoleEventListener listener = new BuckEventBusFactory.CapturingConsoleEventListener();
    executionContext.getBuckEventBus().register(listener);
    StepExecutionResult result = step.execute(executionContext);
    // Note that we don't include stderr in the step result on success.
    assertThat(result, equalTo(StepExecutionResult.SUCCESS));
    assertThat(listener.getLogMessages(), empty());
}
Also used : BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) FakeProcess(com.facebook.buck.util.FakeProcess) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Paths(java.nio.file.Paths) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

StepExecutionResult (com.facebook.buck.step.StepExecutionResult)24 ExecutionContext (com.facebook.buck.step.ExecutionContext)18 Path (java.nio.file.Path)15 SourcePath (com.facebook.buck.rules.SourcePath)13 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)11 Step (com.facebook.buck.step.Step)11 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)11 ImmutableList (com.google.common.collect.ImmutableList)11 IOException (java.io.IOException)9 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)6 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)6 MkdirStep (com.facebook.buck.step.fs.MkdirStep)6 Test (org.junit.Test)6 BuckEventBusFactory (com.facebook.buck.event.BuckEventBusFactory)4 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)4 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)4 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)4 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)4 CopyStep (com.facebook.buck.step.fs.CopyStep)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4