Skip to content

Commit

Permalink
Revert "Update plugin-building workflow to support remote builds"
Browse files Browse the repository at this point in the history
This reverts commit 8df6208.
  • Loading branch information
liucijus authored and WixBuildServer committed Dec 28, 2023
1 parent 3eda1b2 commit 3bc2aa1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public RunProfileState getState(Executor executor, ExecutionEnvironment env)
throw new ExecutionException("No sandbox specified for IntelliJ Platform Plugin SDK", e);
}
String buildNumber = IdeaJdkHelper.getBuildNumber(ideaJdk);
final BlazeIntellijPluginDeployer deployer = new BlazeIntellijPluginDeployer(sandboxHome);
final BlazeIntellijPluginDeployer deployer =
new BlazeIntellijPluginDeployer(sandboxHome, target);
env.putUserData(BlazeIntellijPluginDeployer.USER_DATA_KEY, deployer);

// copy license from running instance of idea
Expand All @@ -197,7 +198,7 @@ public RunProfileState getState(Executor executor, ExecutionEnvironment env)
return new JavaCommandLineState(env) {
@Override
protected JavaParameters createJavaParameters() throws ExecutionException {
DeployedPluginInfo deployedPluginInfo = deployer.deployNonBlocking(buildSystem);
DeployedPluginInfo deployedPluginInfo = deployer.deployNonBlocking();

final JavaParameters params = new JavaParameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@
*/
package com.google.idea.blaze.plugin.run;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.devtools.intellij.plugin.IntellijPluginTargetDeployInfo.IntellijPluginDeployFile;
import com.google.devtools.intellij.plugin.IntellijPluginTargetDeployInfo.IntellijPluginDeployInfo;
import com.google.idea.blaze.base.async.executor.BlazeExecutor;
import com.google.idea.blaze.base.command.buildresult.BlazeArtifact;
import com.google.idea.blaze.base.command.buildresult.BuildResultHelper;
import com.google.idea.blaze.base.command.buildresult.BuildResultHelper.GetArtifactsException;
import com.google.idea.blaze.base.command.buildresult.OutputArtifact;
import com.google.idea.blaze.base.sync.aspects.BlazeBuildOutputs;
import com.google.idea.blaze.base.io.FileOperationProvider;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.common.experiments.BoolExperiment;
import com.google.protobuf.TextFormat;
import com.intellij.concurrency.AsyncUtil;
import com.intellij.execution.ExecutionException;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Future;

Expand All @@ -59,64 +59,66 @@ class BlazeIntellijPluginDeployer {
Key.create(BlazeIntellijPluginDeployer.class.getName());

private final String sandboxHome;
private final Map<String, OutputArtifact> buildArtifactsMap = new HashMap<>();
private final List<OutputArtifact> deployInfoArtifacts = new ArrayList<>();
private final Map<OutputArtifact, File> filesToDeploy = Maps.newHashMap();
private final Label pluginTarget;
private final List<File> deployInfoFiles = new ArrayList<>();
private final Map<File, File> filesToDeploy = Maps.newHashMap();
private File executionRoot;

private Future<Void> fileCopyingTask;

BlazeIntellijPluginDeployer(String sandboxHome) {
BlazeIntellijPluginDeployer(String sandboxHome, Label pluginTarget) {
this.sandboxHome = sandboxHome;
this.pluginTarget = pluginTarget;
}

/**
* Clear data from the last build -- if this build fails, we don't want to silently launch the
* previously built plugin.
*/
void buildStarted() {
deployInfoArtifacts.clear();
buildArtifactsMap.clear();
executionRoot = null;
deployInfoFiles.clear();
}

void reportBuildComplete(BlazeBuildOutputs blazeBuildOutputs) throws GetArtifactsException {
ImmutableList<OutputArtifact> buildArtifacts =
blazeBuildOutputs.artifacts.values().stream()
.map(a -> a.artifact)
.collect(toImmutableList());
buildArtifactsMap.clear();
buildArtifacts.forEach(a -> buildArtifactsMap.put(a.getRelativePath(), a));

deployInfoArtifacts.clear();
deployInfoArtifacts.addAll(
buildArtifacts.stream()
.filter(a -> a.toString().endsWith(".intellij-plugin-debug-target-deploy-info"))
.collect(toImmutableList()));
void reportBuildComplete(File executionRoot, BuildResultHelper buildResultHelper)
throws GetArtifactsException {
this.executionRoot = executionRoot;
deployInfoFiles.clear();
ImmutableList<OutputArtifact> outputs =
buildResultHelper.getBuildArtifactsForTarget(
pluginTarget, file -> file.endsWith(".intellij-plugin-debug-target-deploy-info"));
deployInfoFiles.addAll(BlazeArtifact.getLocalFiles(outputs));
}

/**
* Returns information about which plugins will be deployed, and asynchronously copies the
* corresponding files to the sandbox.
*/
DeployedPluginInfo deployNonBlocking(String buildSystem) throws ExecutionException {
if (deployInfoArtifacts.isEmpty()) {
DeployedPluginInfo deployNonBlocking() throws ExecutionException {
if (deployInfoFiles.isEmpty()) {
throw new ExecutionException("No plugin files found. Did the build fail?");
}
List<IntellijPluginDeployInfo> deployInfoList = Lists.newArrayList();
for (OutputArtifact deployInfoFile : deployInfoArtifacts) {
for (File deployInfoFile : deployInfoFiles) {
deployInfoList.addAll(readDeployInfoFromFile(deployInfoFile));
}
ImmutableMap<OutputArtifact, File> filesToDeploy =
getFilesToDeploy(deployInfoList, buildSystem);
ImmutableMap<File, File> filesToDeploy = getFilesToDeploy(executionRoot, deployInfoList);
this.filesToDeploy.putAll(filesToDeploy);
ImmutableSet<File> javaAgentJars =
deployJavaAgents.getValue() ? listJavaAgentFiles(deployInfoList) : ImmutableSet.of();

for (File file : filesToDeploy.keySet()) {
if (!file.exists()) {
throw new ExecutionException(
String.format("Plugin file '%s' not found. Did the build fail?", file.getName()));
}
}
// kick off file copying task asynchronously, so it doesn't block the EDT.
fileCopyingTask =
BlazeExecutor.getInstance()
.submit(
() -> {
for (Map.Entry<OutputArtifact, File> entry : filesToDeploy.entrySet()) {
for (Map.Entry<File, File> entry : filesToDeploy.entrySet()) {
copyFileToSandbox(entry.getKey(), entry.getValue());
}
return null;
Expand All @@ -139,10 +141,10 @@ void deleteDeployment() {
}
}

private static ImmutableList<IntellijPluginDeployInfo> readDeployInfoFromFile(
OutputArtifact deployInfoArtifact) throws ExecutionException {
private static ImmutableList<IntellijPluginDeployInfo> readDeployInfoFromFile(File deployInfoFile)
throws ExecutionException {
ImmutableList.Builder<IntellijPluginDeployInfo> result = ImmutableList.builder();
try (InputStream inputStream = deployInfoArtifact.getInputStream()) {
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(deployInfoFile))) {
IntellijPluginDeployInfo.Builder builder = IntellijPluginDeployInfo.newBuilder();
TextFormat.Parser parser = TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build();
parser.merge(new InputStreamReader(inputStream, UTF_8), builder);
Expand All @@ -154,45 +156,24 @@ private static ImmutableList<IntellijPluginDeployInfo> readDeployInfoFromFile(
return result.build();
}

private ImmutableMap<OutputArtifact, File> getFilesToDeploy(
Collection<IntellijPluginDeployInfo> deployInfos, String buildSystem)
throws ExecutionException {
ImmutableMap.Builder<OutputArtifact, File> result = ImmutableMap.builder();
private ImmutableMap<File, File> getFilesToDeploy(
File executionRoot, Collection<IntellijPluginDeployInfo> deployInfos) {
ImmutableMap.Builder<File, File> result = ImmutableMap.builder();
for (IntellijPluginDeployInfo deployInfo : deployInfos) {
for (IntellijPluginDeployFile deployFile : deployInfo.getDeployFilesList()) {
result.put(
getArtifactFromDeployFile(deployFile, buildSystem),
new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation()));
File src = new File(executionRoot, deployFile.getExecutionPath());
File dest = new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation());
result.put(src, dest);
}
for (IntellijPluginDeployFile deployFile : deployInfo.getJavaAgentDeployFilesList()) {
result.put(
getArtifactFromDeployFile(deployFile, buildSystem),
new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation()));
File src = new File(executionRoot, deployFile.getExecutionPath());
File dest = new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation());
result.put(src, dest);
}
}
return result.build();
}

private OutputArtifact getArtifactFromDeployFile(
IntellijPluginDeployFile deployFile, String buildSystem) throws ExecutionException {
String relativePath =
buildArtifactsMap.keySet().stream()
.filter(
key ->
key.endsWith(
StringUtil.trimStart(
deployFile.getExecutionPath(),
String.format("%s-out/", buildSystem.toLowerCase(Locale.ROOT)))))
.findAny()
.orElseThrow(
() ->
new ExecutionException(
String.format(
"Plugin file '%s' not found. Did the build fail?",
deployFile.getExecutionPath())));
return buildArtifactsMap.get(relativePath);
}

private ImmutableSet<File> listJavaAgentFiles(Collection<IntellijPluginDeployInfo> deployInfos) {
ImmutableSet.Builder<File> result = ImmutableSet.builder();
for (IntellijPluginDeployInfo deployInfo : deployInfos) {
Expand All @@ -207,12 +188,10 @@ private static File sandboxPluginDirectory(String sandboxHome) {
return new File(sandboxHome, "plugins");
}

private static void copyFileToSandbox(OutputArtifact deployArtifact, File dest)
throws ExecutionException {
try (BufferedInputStream stream = deployArtifact.getInputStream()) {
boolean unused = dest.getParentFile().mkdirs();
Files.write(stream.readAllBytes(), dest);
unused = dest.setExecutable(true, true);
private static void copyFileToSandbox(File src, File dest) throws ExecutionException {
try {
dest.getParentFile().mkdirs();
FileOperationProvider.getInstance().copy(src, dest, StandardCopyOption.REPLACE_EXISTING);
dest.deleteOnExit();
} catch (IOException e) {
throw new ExecutionException("Error copying plugin file to sandbox", e);
Expand Down
Loading

0 comments on commit 3bc2aa1

Please sign in to comment.