Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit bae1f42

Browse files
irenewchenfacebook-github-bot
authored andcommitted
Add BuildableCommandExecutionStep
Summary: Migrated buildables that implement `BuildableWithExternalAction` will return a `BuildableCommand` according to the interface. That `BuildableCommand` will then get wrapped into this `BuildableCommandExecutionStep` and get passed to the external binary. This step is essentially a bridge between the current execution flow and the external binary Side note: constants are split out from the other parts of `external/utils` to avoid a circular dependency Reviewed By: bobyangyf fbshipit-source-id: ed5586e0fcb766b774993e985f78d89240ad7e5e
1 parent 058958e commit bae1f42

File tree

14 files changed

+376
-7
lines changed

14 files changed

+376
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
java_library(
2+
name = "constants",
3+
srcs = glob(["*.java"]),
4+
visibility = [
5+
"PUBLIC",
6+
],
7+
deps = [
8+
"//src/com/facebook/buck/step/isolatedsteps:isolatedsteps",
9+
],
10+
)

src/com/facebook/buck/external/utils/ExternalBinaryBuckConstants.java renamed to src/com/facebook/buck/external/constants/ExternalBinaryBuckConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.facebook.buck.external.utils;
17+
package com.facebook.buck.external.constants;
1818

1919
import com.facebook.buck.core.build.execution.context.IsolatedExecutionContext;
2020

src/com/facebook/buck/external/parser/BUCK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ java_immutables_library(
1010
deps = [
1111
"//src-gen:buildable-command-model",
1212
"//src/com/facebook/buck/downwardapi/utils:utils",
13+
"//src/com/facebook/buck/external/constants:constants",
1314
"//src/com/facebook/buck/external/model:model",
14-
"//src/com/facebook/buck/external/utils:utils",
1515
"//src/com/facebook/buck/rules/modern:modern",
1616
"//src/com/facebook/buck/step/isolatedsteps:isolatedsteps",
1717
"//third-party/java/guava:guava",

src/com/facebook/buck/external/parser/ParsedEnvVars.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.facebook.buck.core.model.BuildId;
2121
import com.facebook.buck.core.util.immutables.BuckStyleValue;
2222
import com.facebook.buck.downwardapi.utils.DownwardApiConstants;
23-
import com.facebook.buck.external.utils.ExternalBinaryBuckConstants;
23+
import com.facebook.buck.external.constants.ExternalBinaryBuckConstants;
2424
import com.facebook.buck.util.Verbosity;
2525
import com.google.common.base.Preconditions;
2626
import com.google.common.collect.ImmutableMap;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
java_library(
2+
name = "buildables",
3+
srcs = glob(["*.java"]),
4+
visibility = ["PUBLIC"],
5+
deps = [
6+
"//src-gen:buildable-command-model",
7+
"//src/com/facebook/buck/downwardapi/processexecutor:processexecutor",
8+
"//src/com/facebook/buck/external/constants:constants",
9+
"//src/com/facebook/buck/external/model:model",
10+
"//src/com/facebook/buck/step:step",
11+
"//src/com/facebook/buck/step/isolatedsteps:isolatedsteps",
12+
],
13+
)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

test/com/facebook/buck/external/main/BUCK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ java_test(
1818
":testutil",
1919
"//src-gen:buildable-command-model",
2020
"//src/com/facebook/buck/downwardapi/processexecutor:processexecutor",
21-
"//src/com/facebook/buck/external/utils:utils",
21+
"//src/com/facebook/buck/external/constants:constants",
2222
"//src/com/facebook/buck/rules/modern:modern",
2323
"//test/com/facebook/buck/event:testutil",
2424
"//test/com/facebook/buck/testutil:testutil",

test/com/facebook/buck/external/main/ExternalActionsIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.facebook.buck.event.BuckEventBus;
2727
import com.facebook.buck.event.BuckEventBusForTests;
2828
import com.facebook.buck.event.StepEvent;
29-
import com.facebook.buck.external.utils.ExternalBinaryBuckConstants;
29+
import com.facebook.buck.external.constants.ExternalBinaryBuckConstants;
3030
import com.facebook.buck.rules.modern.model.BuildableCommand;
3131
import com.facebook.buck.testutil.TemporaryPaths;
3232
import com.facebook.buck.testutil.TestConsole;

test/com/facebook/buck/external/parser/BUCK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ standard_java_test(
55
deps = [
66
"//src-gen:buildable-command-model",
77
"//src/com/facebook/buck/downwardapi/utils:utils",
8+
"//src/com/facebook/buck/external/constants:constants",
89
"//src/com/facebook/buck/external/model:model",
910
"//src/com/facebook/buck/external/parser:parser",
10-
"//src/com/facebook/buck/external/utils:utils",
1111
"//src/com/facebook/buck/rules/modern:modern",
1212
"//src/com/facebook/buck/step/isolatedsteps:isolatedsteps",
1313
"//test/com/facebook/buck/testutil:testutil",

test/com/facebook/buck/external/parser/ParsedEnvVarsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.facebook.buck.core.filesystems.AbsPath;
2424
import com.facebook.buck.core.model.BuildId;
2525
import com.facebook.buck.downwardapi.utils.DownwardApiConstants;
26-
import com.facebook.buck.external.utils.ExternalBinaryBuckConstants;
26+
import com.facebook.buck.external.constants.ExternalBinaryBuckConstants;
2727
import com.facebook.buck.util.Verbosity;
2828
import com.google.common.collect.ImmutableMap;
2929
import java.nio.file.Paths;

0 commit comments

Comments
 (0)