|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.facebook.buck.step.buildables; |
| 18 | + |
| 19 | +import com.facebook.buck.core.build.execution.context.IsolatedExecutionContext; |
| 20 | +import com.facebook.buck.core.filesystems.AbsPath; |
| 21 | +import com.facebook.buck.downwardapi.processexecutor.DownwardApiProcessExecutor; |
| 22 | +import com.facebook.buck.external.constants.ExternalBinaryBuckConstants; |
| 23 | +import com.facebook.buck.io.filesystem.ProjectFilesystem; |
| 24 | +import com.facebook.buck.rules.modern.model.BuildableCommand; |
| 25 | +import com.facebook.buck.step.Step; |
| 26 | +import com.facebook.buck.step.StepExecutionResult; |
| 27 | +import com.facebook.buck.step.isolatedsteps.IsolatedStep; |
| 28 | +import com.facebook.buck.util.ProcessExecutor; |
| 29 | +import com.facebook.buck.util.ProcessExecutorParams; |
| 30 | +import com.google.common.annotations.VisibleForTesting; |
| 31 | +import com.google.common.collect.ImmutableList; |
| 32 | +import com.google.common.collect.ImmutableMap; |
| 33 | +import com.google.common.io.Resources; |
| 34 | +import java.io.FileOutputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.net.URISyntaxException; |
| 38 | +import java.net.URL; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | + |
| 42 | +/** |
| 43 | + * {@link Step} that wraps a {@link BuildableCommand}. Writes the {@link BuildableCommand} into a |
| 44 | + * file, then invokes the given jar with the given main entry point, which can then read the {@link |
| 45 | + * BuildableCommand} file and execute its associated steps. Used as a bridge between {@link |
| 46 | + * com.facebook.buck.rules.modern.Buildable} and {@link |
| 47 | + * com.facebook.buck.rules.modern.BuildableWithExternalAction}. |
| 48 | + */ |
| 49 | +public class BuildableCommandExecutionStep extends IsolatedStep { |
| 50 | + |
| 51 | + private static final String EXTERNAL_ACTIONS_RESOURCE_JAR = |
| 52 | + "com/facebook/buck/external/main/external_actions_bin.jar"; |
| 53 | + private static final String EXTERNAL_ACTIONS_MAIN = |
| 54 | + "com.facebook.buck.external.main.ExternalActionsExecutable"; |
| 55 | + private static final ImmutableList<String> COMMAND_PREFIX = ImmutableList.of("java", "-cp"); |
| 56 | + private static final String TEMP_FILE_NAME_PREFIX = "buildable_command_"; |
| 57 | + |
| 58 | + private final AbsPath jarPath; |
| 59 | + private final String mainToInvoke; |
| 60 | + private final BuildableCommand buildableCommand; |
| 61 | + private final ProjectFilesystem projectFilesystem; |
| 62 | + |
| 63 | + public BuildableCommandExecutionStep( |
| 64 | + BuildableCommand buildableCommand, ProjectFilesystem projectFilesystem) { |
| 65 | + this(getJarPath(), EXTERNAL_ACTIONS_MAIN, buildableCommand, projectFilesystem); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Used for testing only. Production code should use the constructor {@link |
| 70 | + * #BuildableCommandExecutionStep(BuildableCommand, ProjectFilesystem)}. |
| 71 | + */ |
| 72 | + @VisibleForTesting |
| 73 | + public BuildableCommandExecutionStep( |
| 74 | + AbsPath jarPath, |
| 75 | + String mainToInvoke, |
| 76 | + BuildableCommand buildableCommand, |
| 77 | + ProjectFilesystem projectFilesystem) { |
| 78 | + this.jarPath = jarPath; |
| 79 | + this.mainToInvoke = mainToInvoke; |
| 80 | + this.buildableCommand = buildableCommand; |
| 81 | + this.projectFilesystem = projectFilesystem; |
| 82 | + } |
| 83 | + |
| 84 | + private static AbsPath getJarPath() { |
| 85 | + try { |
| 86 | + URL binary = Resources.getResource(EXTERNAL_ACTIONS_RESOURCE_JAR); |
| 87 | + return AbsPath.of(Paths.get(binary.toURI())); |
| 88 | + } catch (URISyntaxException e) { |
| 89 | + throw new IllegalStateException("Failed to retrieve external actions binary", e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public StepExecutionResult executeIsolatedStep(IsolatedExecutionContext context) |
| 95 | + throws IOException, InterruptedException { |
| 96 | + Path buildableCommandPath = writeBuildableCommandAndGetPath(); |
| 97 | + ProcessExecutor downwardApiProcessExecutor = |
| 98 | + context |
| 99 | + .getProcessExecutor() |
| 100 | + .withDownwardAPI(DownwardApiProcessExecutor.FACTORY, context.getIsolatedEventBus()); |
| 101 | + return StepExecutionResult.of( |
| 102 | + downwardApiProcessExecutor.launchAndExecute( |
| 103 | + ProcessExecutorParams.builder() |
| 104 | + .addAllCommand(getCommand(buildableCommandPath)) |
| 105 | + .setEnvironment(getEnvs()) |
| 106 | + .build(), |
| 107 | + ImmutableMap.of())); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getShortName() { |
| 112 | + return "buildable_command"; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getIsolatedStepDescription(IsolatedExecutionContext context) { |
| 117 | + return "running buildable command: " + buildableCommand; |
| 118 | + } |
| 119 | + |
| 120 | + private Path writeBuildableCommandAndGetPath() throws IOException { |
| 121 | + Path buildableCommandPath = projectFilesystem.createTempFile(TEMP_FILE_NAME_PREFIX, ""); |
| 122 | + try (OutputStream outputStream = new FileOutputStream(buildableCommandPath.toFile())) { |
| 123 | + buildableCommand.writeTo(outputStream); |
| 124 | + } |
| 125 | + return buildableCommandPath; |
| 126 | + } |
| 127 | + |
| 128 | + private ImmutableList<String> getCommand(Path buildableCommandPath) { |
| 129 | + return ImmutableList.<String>builderWithExpectedSize( |
| 130 | + COMMAND_PREFIX.size() + buildableCommand.getArgsList().size()) |
| 131 | + .addAll(COMMAND_PREFIX) |
| 132 | + .add(jarPath.toString()) |
| 133 | + .add(mainToInvoke) |
| 134 | + .add(buildableCommandPath.toString()) |
| 135 | + .build(); |
| 136 | + } |
| 137 | + |
| 138 | + private ImmutableMap<String, String> getEnvs() { |
| 139 | + return ImmutableMap.of( |
| 140 | + ExternalBinaryBuckConstants.ENV_RULE_CELL_ROOT, projectFilesystem.getRootPath().toString()); |
| 141 | + } |
| 142 | +} |
0 commit comments