Search in sources :

Example 1 with ContextualProcessExecutor

use of com.facebook.buck.util.ContextualProcessExecutor in project buck by facebook.

the class CachingBuildEngine method executePostBuildSteps.

private void executePostBuildSteps(BuildRule rule, Iterable<Step> postBuildSteps, ExecutionContext context) throws InterruptedException, StepFailedException {
    LOG.debug("Running post-build steps for %s", rule);
    Optional<BuildTarget> optionalTarget = Optional.of(rule.getBuildTarget());
    for (Step step : postBuildSteps) {
        stepRunner.runStepForBuildTarget(context.withProcessExecutor(new ContextualProcessExecutor(context.getProcessExecutor(), ImmutableMap.of(BUILD_RULE_TYPE_CONTEXT_KEY, rule.getType(), STEP_TYPE_CONTEXT_KEY, StepType.POST_BUILD_STEP.toString()))), step, optionalTarget);
        // Check for interruptions that may have been ignored by step.
        if (Thread.interrupted()) {
            Thread.currentThread().interrupt();
            throw new InterruptedException();
        }
    }
    LOG.debug("Finished running post-build steps for %s", rule);
}
Also used : ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) BuildTarget(com.facebook.buck.model.BuildTarget) Step(com.facebook.buck.step.Step)

Example 2 with ContextualProcessExecutor

use of com.facebook.buck.util.ContextualProcessExecutor in project buck by facebook.

the class CachingBuildEngine method executeCommandsNowThatDepsAreBuilt.

/**
   * Execute the commands for this build rule. Requires all dependent rules are already built
   * successfully.
   */
private void executeCommandsNowThatDepsAreBuilt(BuildRule rule, BuildEngineBuildContext buildContext, ExecutionContext executionContext, BuildableContext buildableContext) throws InterruptedException, StepFailedException {
    LOG.debug("Building locally: %s", rule);
    // Attempt to get an approximation of how long it takes to actually run the command.
    @SuppressWarnings("PMD.PrematureDeclaration") long start = System.nanoTime();
    buildContext.getEventBus().post(BuildRuleEvent.willBuildLocally(rule));
    cachingBuildEngineDelegate.onRuleAboutToBeBuilt(rule);
    // Get and run all of the commands.
    List<Step> steps = rule.getBuildSteps(buildContext.getBuildContext(), buildableContext);
    Optional<BuildTarget> optionalTarget = Optional.of(rule.getBuildTarget());
    for (Step step : steps) {
        stepRunner.runStepForBuildTarget(executionContext.withProcessExecutor(new ContextualProcessExecutor(executionContext.getProcessExecutor(), ImmutableMap.of(BUILD_RULE_TYPE_CONTEXT_KEY, rule.getType(), STEP_TYPE_CONTEXT_KEY, StepType.BUILD_STEP.toString()))), step, optionalTarget);
        // Check for interruptions that may have been ignored by step.
        if (Thread.interrupted()) {
            Thread.currentThread().interrupt();
            throw new InterruptedException();
        }
    }
    long end = System.nanoTime();
    LOG.debug("Build completed: %s %s (%dns)", rule.getType(), rule.getFullyQualifiedName(), end - start);
}
Also used : ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) BuildTarget(com.facebook.buck.model.BuildTarget) Step(com.facebook.buck.step.Step)

Example 3 with ContextualProcessExecutor

use of com.facebook.buck.util.ContextualProcessExecutor in project buck by facebook.

the class JavacExecutionContextSerializerTest method testSerializingAndDeserializing.

@Test
public void testSerializingAndDeserializing() throws Exception {
    Path tmp = Files.createTempDirectory("junit-temp-path").toRealPath();
    JavacEventSink eventSink = new JavacEventSinkToBuckEventBusBridge(BuckEventBusFactory.newInstance());
    PrintStream stdErr = new PrintStream(new ByteArrayOutputStream());
    ClassLoaderCache classLoaderCache = new ClassLoaderCache();
    ObjectMapper objectMapper = new ObjectMapper();
    Verbosity verbosity = Verbosity.COMMANDS_AND_OUTPUT;
    DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(Paths.get("/some/cell/path/resolver/path"), ImmutableMap.of("key1", Paths.get("/path/1")));
    DefaultJavaPackageFinder javaPackageFinder = new DefaultJavaPackageFinder(ImmutableSortedSet.of("paths", "from", "root"), ImmutableSet.of("path", "elements"));
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp);
    NoOpClassUsageFileWriter classUsageFileWriter = NoOpClassUsageFileWriter.instance();
    ImmutableMap<String, String> environment = ImmutableMap.of("k1", "v1", "k2", "v2");
    ImmutableMap<String, String> processExecutorContext = ImmutableMap.of("pek1", "pev1", "pek2", "pev2");
    ProcessExecutor processExecutor = new ContextualProcessExecutor(new DefaultProcessExecutor(new TestConsole()), processExecutorContext);
    ImmutableList<Path> pathToInputs = ImmutableList.of(Paths.get("/path/one"), Paths.get("/path/two"));
    DirectToJarOutputSettings directToJarOutputSettings = DirectToJarOutputSettings.of(Paths.get("/some/path"), ImmutableSet.of(Pattern.compile("[a-z]"), Pattern.compile("[0-9]", Pattern.MULTILINE)), ImmutableSortedSet.of(Paths.get("some/path"), Paths.get("/other path/")), Optional.of("hello I am main class"), Optional.of(Paths.get("/MANIFEST/FILE.TXT")));
    JavacExecutionContext input = JavacExecutionContext.of(eventSink, stdErr, classLoaderCache, objectMapper, verbosity, cellPathResolver, javaPackageFinder, projectFilesystem, classUsageFileWriter, environment, processExecutor, pathToInputs, Optional.of(directToJarOutputSettings));
    Map<String, Object> data = JavacExecutionContextSerializer.serialize(input);
    JavacExecutionContext output = JavacExecutionContextSerializer.deserialize(data, eventSink, stdErr, classLoaderCache, objectMapper, new TestConsole());
    assertThat(output.getEventSink(), Matchers.equalTo(eventSink));
    assertThat(output.getStdErr(), Matchers.equalTo(stdErr));
    assertThat(output.getClassLoaderCache(), Matchers.equalTo(classLoaderCache));
    assertThat(output.getObjectMapper(), Matchers.equalTo(objectMapper));
    assertThat(output.getVerbosity(), Matchers.equalTo(verbosity));
    assertThat(output.getCellPathResolver(), Matchers.instanceOf(DefaultCellPathResolver.class));
    DefaultCellPathResolver outCellPathResolver = (DefaultCellPathResolver) output.getCellPathResolver();
    assertThat(outCellPathResolver.getRoot(), Matchers.equalToObject(cellPathResolver.getRoot()));
    assertThat(outCellPathResolver.getCellPaths(), Matchers.equalToObject(cellPathResolver.getCellPaths()));
    assertThat(output.getProcessExecutor(), Matchers.instanceOf(ContextualProcessExecutor.class));
    ContextualProcessExecutor contextualProcessExecutor = (ContextualProcessExecutor) output.getProcessExecutor();
    assertThat(contextualProcessExecutor.getContext(), Matchers.equalToObject(processExecutorContext));
    assertThat(output.getJavaPackageFinder(), Matchers.instanceOf(DefaultJavaPackageFinder.class));
    DefaultJavaPackageFinder outputJavaPackageFinder = (DefaultJavaPackageFinder) output.getJavaPackageFinder();
    assertThat(outputJavaPackageFinder.getPathsFromRoot(), Matchers.equalToObject(javaPackageFinder.getPathsFromRoot()));
    assertThat(outputJavaPackageFinder.getPathElements(), Matchers.equalToObject(javaPackageFinder.getPathElements()));
    assertThat(output.getProjectFilesystem().getRootPath(), Matchers.equalToObject(projectFilesystem.getRootPath()));
    assertThat(output.getUsedClassesFileWriter(), Matchers.instanceOf(NoOpClassUsageFileWriter.class));
    assertThat(output.getEnvironment(), Matchers.equalToObject(environment));
    assertThat(output.getAbsolutePathsForInputs(), Matchers.equalToObject(pathToInputs));
    assertThat(output.getDirectToJarOutputSettings().get().getDirectToJarOutputPath(), Matchers.equalToObject(directToJarOutputSettings.getDirectToJarOutputPath()));
    assertThat(output.getDirectToJarOutputSettings().get().getEntriesToJar(), Matchers.equalToObject(directToJarOutputSettings.getEntriesToJar()));
    assertThat(output.getDirectToJarOutputSettings().get().getMainClass(), Matchers.equalToObject(directToJarOutputSettings.getMainClass()));
    assertThat(output.getDirectToJarOutputSettings().get().getManifestFile(), Matchers.equalToObject(directToJarOutputSettings.getManifestFile()));
    assertThat(output.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar().size(), Matchers.equalToObject(directToJarOutputSettings.getClassesToRemoveFromJar().size()));
    ImmutableList<Pattern> inputPatterns = directToJarOutputSettings.getClassesToRemoveFromJar().asList();
    ImmutableList<Pattern> outputPatterns = output.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar().asList();
    for (int i = 0; i < inputPatterns.size(); i++) {
        Pattern inputPattern = inputPatterns.get(i);
        Pattern outputPattern = outputPatterns.get(i);
        assertThat(outputPattern.pattern(), Matchers.equalToObject(inputPattern.pattern()));
        assertThat(outputPattern.flags(), Matchers.equalTo(inputPattern.flags()));
    }
}
Also used : DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Verbosity(com.facebook.buck.util.Verbosity) ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) Pattern(java.util.regex.Pattern) DefaultCellPathResolver(com.facebook.buck.rules.DefaultCellPathResolver) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ClassLoaderCache(com.facebook.buck.util.ClassLoaderCache) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

ContextualProcessExecutor (com.facebook.buck.util.ContextualProcessExecutor)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 Step (com.facebook.buck.step.Step)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 DefaultCellPathResolver (com.facebook.buck.rules.DefaultCellPathResolver)1 TestConsole (com.facebook.buck.testutil.TestConsole)1 ClassLoaderCache (com.facebook.buck.util.ClassLoaderCache)1 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)1 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)1 Verbosity (com.facebook.buck.util.Verbosity)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.Test)1