Skip to content

Commit

Permalink
chore: Aspect templating - 3/n - pass 'Project' to AspectRepositoryPr…
Browse files Browse the repository at this point in the history
…ovider

We will need to have project-specific aspects
  • Loading branch information
tpasternak committed Oct 1, 2024
1 parent 6240f8d commit 732d9d4
Show file tree
Hide file tree
Showing 29 changed files with 61 additions and 47 deletions.
13 changes: 7 additions & 6 deletions base/src/com/google/idea/blaze/base/command/BlazeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider;
import com.intellij.openapi.project.Project;

import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -81,16 +82,16 @@ public String toString() {
return Joiner.on(' ').join(toList());
}

public static Builder builder(BuildInvoker invoker, BlazeCommandName name) {
return new Builder(invoker.getBinaryPath(), name);
public static Builder builder(BuildInvoker invoker, BlazeCommandName name, Project project) {
return new Builder(invoker.getBinaryPath(), name, project);
}

/**
* @deprecated Use {@link #builder(BuildInvoker, BlazeCommandName)} instead.
*/
@Deprecated
public static Builder builder(String binaryPath, BlazeCommandName name) {
return new Builder(binaryPath, name);
public static Builder builder(String binaryPath, BlazeCommandName name, Project project) {
return new Builder(binaryPath, name, project);
}

/** Builder for a blaze command */
Expand All @@ -104,14 +105,14 @@ public static class Builder {
private final ImmutableList.Builder<String> blazeCmdlineFlags = ImmutableList.builder();
private final ImmutableList.Builder<String> exeFlags = ImmutableList.builder();

public Builder(String binaryPath, BlazeCommandName name) {
public Builder(String binaryPath, BlazeCommandName name, Project project) {
this.binaryPath = binaryPath;
this.name = name;
this.invokeParallel = false;
// Tell forge what tool we used to call blaze so we can track usage.
addBlazeFlags(BlazeFlags.getToolTagFlag());

AspectRepositoryProvider.getOverrideFlag().ifPresent(this::addBlazeFlags);
AspectRepositoryProvider.getOverrideFlag(project).ifPresent(this::addBlazeFlags);
}

private ImmutableList<String> getArguments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ListenableFuture<byte[]> runBlazeInfoGetBytes(
return BlazeExecutor.getInstance()
.submit(
() -> {
BlazeCommand.Builder builder = BlazeCommand.builder(invoker, BlazeCommandName.INFO);
BlazeCommand.Builder builder = BlazeCommand.builder(invoker, BlazeCommandName.INFO, project);
builder.addBlazeFlags(blazeFlags);
if (keys != null) {
builder.addBlazeFlags(keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected ListenableFuture<byte[]> runBlazeModGetBytes(
.submit(
() -> {
BlazeCommand.Builder builder =
BlazeCommand.builder(invoker, BlazeCommandName.MOD).addBlazeFlags(flags);
BlazeCommand.builder(invoker, BlazeCommandName.MOD, project).addBlazeFlags(flags);

if (args != null) {
builder.addBlazeFlags(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static ImmutableList<TargetInfo> runQuery(
BuildSystem buildSystem = Blaze.getBuildSystemProvider(project).getBuildSystem();
BlazeCommand.Builder command =
BlazeCommand.builder(
buildSystem.getDefaultInvoker(project, context), BlazeCommandName.QUERY)
buildSystem.getDefaultInvoker(project, context), BlazeCommandName.QUERY, project)
.addBlazeFlags("--output=label_kind")
.addBlazeFlags("--keep_going")
.addBlazeFlags(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private static BlazeCommand.Builder getBlazeCommandBuilder(
: BlazeQueryOutputBaseProvider.getInstance(project).getOutputBaseFlag();
BuildInvoker buildInvoker =
Blaze.getBuildSystemProvider(project).getBuildSystem().getDefaultInvoker(project, context);
return BlazeCommand.builder(buildInvoker, BlazeCommandName.QUERY)
return BlazeCommand.builder(buildInvoker, BlazeCommandName.QUERY, project)
.addBlazeFlags(additionalBlazeFlags)
.addBlazeFlags("--keep_going")
.addBlazeFlags(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public AppInspectorInfo buildAppInspector(BlazeContext context, Set<Label> build
BlazeInvocationContext.OTHER_CONTEXT);

BlazeCommand.Builder builder =
BlazeCommand.builder(invoker, BlazeCommandName.BUILD)
BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project)
.addBlazeFlags(buildTargets.stream().map(Label::toString).collect(toImmutableList()))
.addBlazeFlags(buildResultHelper.getBuildFlags())
.addBlazeFlags(additionalBlazeFlags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public OutputInfo build(
BlazeInvocationContext.OTHER_CONTEXT);

BlazeCommand.Builder builder =
BlazeCommand.builder(invoker, BlazeCommandName.BUILD)
BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project)
.addBlazeFlags(buildTargets.stream().map(Label::toString).collect(toImmutableList()))
.addBlazeFlags(buildResultHelper.getBuildFlags())
.addBlazeFlags(additionalBlazeFlags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public QuerySummary runQuery(QuerySpec query, BlazeContext context)
"Running `%.200s` using invoker %s, runner %s",
query, invoker.getClass().getSimpleName(), commandRunner.getClass().getSimpleName()));

BlazeCommand.Builder commandBuilder = BlazeCommand.builder(invoker, BlazeCommandName.QUERY);
BlazeCommand.Builder commandBuilder = BlazeCommand.builder(invoker, BlazeCommandName.QUERY, project);
commandBuilder.addBlazeFlags(query.getQueryFlags());
commandBuilder.addBlazeFlags("--keep_going");
String queryExp = query.getQueryExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public RenderJarInfo buildRenderJar(BlazeContext context, Set<Label> buildTarget

// TODO(b/283283123): Refactor render jar functionality to build_dependencies.bzl
BlazeCommand.Builder builder =
BlazeCommand.builder(invoker, BlazeCommandName.BUILD)
BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project)
.addBlazeFlags(buildTargets.stream().map(Label::toString).collect(toImmutableList()))
.addBlazeFlags(buildResultHelper.getBuildFlags())
.addBlazeFlags(additionalBlazeFlags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ImmutableList<CandidatePackage> getCandidatePackages(

private ImmutableList<String> runQuery(Path path) throws BuildException {
BlazeCommand.Builder command =
BlazeCommand.builder(buildInvoker, BlazeCommandName.QUERY)
BlazeCommand.builder(buildInvoker, BlazeCommandName.QUERY, ideProject)
.addBlazeFlags("--output", "package")
.addBlazeFlags("//" + path + "/...");
try (BuildResultHelper helper = buildInvoker.createBuildResultHelper()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ project, new Task(project, TASK_TITLE, Task.Type.BEFORE_LAUNCH))
context.output(new StatusOutput(progressMessage));

BlazeCommand.Builder command =
BlazeCommand.builder(binaryPath, commandName)
BlazeCommand.builder(binaryPath, commandName, project)
.addTargets(targets)
.addBlazeFlags(overridableExtraBlazeFlags)
.addBlazeFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private BlazeCommand.Builder getBlazeCommand(
command = BlazeCommandName.COVERAGE;
}

return BlazeCommand.builder(invoker, command)
return BlazeCommand.builder(invoker, command, project)
.addTargets(configuration.getTargets())
.addBlazeFlags(
BlazeFlags.blazeFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static String getQuery(List<TargetIdeInfo> targets) {
private static ImmutableList<TargetInfo> runBlazeQuery(
Project project, String query, BlazeContext context) {
BlazeCommand command =
BlazeCommand.builder(getBlazeBinaryPath(project), BlazeCommandName.QUERY)
BlazeCommand.builder(getBlazeBinaryPath(project), BlazeCommandName.QUERY, project)
.addBlazeFlags("--output=label_kind")
.addBlazeFlags("--keep_going")
.addBlazeFlags(query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ private static BlazeBuildOutputs runBuildForTargets(

Path targetPatternFile = prepareTargetPatternFile(project, targets);
try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper()) {
BlazeCommand.Builder builder = BlazeCommand.builder(invoker, BlazeCommandName.BUILD);
BlazeCommand.Builder builder = BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project);
builder
.setInvokeParallel(invokeParallel)
.addBlazeFlags(BlazeFlags.TARGET_PATTERN_FILE, targetPatternFile.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.google.idea.blaze.base.sync.aspects.strategy;

import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;

import java.io.File;
import java.util.Optional;

Expand All @@ -10,17 +12,17 @@ public interface AspectRepositoryProvider {

String OVERRIDE_REPOSITORY_FLAG = "--override_repository=intellij_aspect";

Optional<File> aspectDirectory();
Optional<File> aspectDirectory(Project project);

static Optional<File> findAspectDirectory() {
static Optional<File> findAspectDirectory(Project project) {
return EP_NAME.getExtensionsIfPointIsRegistered().stream()
.map(AspectRepositoryProvider::aspectDirectory)
.map(aspectRepositoryProvider -> aspectRepositoryProvider.aspectDirectory(project))
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}

static Optional<String> getOverrideFlag() {
return findAspectDirectory().map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath());
static Optional<String> getOverrideFlag(Project project) {
return findAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginDescriptor;
import com.intellij.openapi.project.Project;

import java.io.File;
import java.util.Optional;
import javax.annotation.Nullable;
Expand All @@ -41,7 +43,7 @@ public AspectStrategy getStrategy(BlazeVersionData versionData) {

static final class RepositoryProvider implements AspectRepositoryProvider {
@Override
public Optional<File> aspectDirectory() {
public Optional<File> aspectDirectory(Project project) {
return Optional.ofNullable(PluginManager.getPluginByClass(AspectStrategy.class))
.map((it) -> new File(it.getPath(), "aspect"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static ExpandedTargetsResult queryIndividualTargets(
return new ExpandedTargetsResult(ImmutableList.of(), BuildResult.SUCCESS);
}
BlazeCommand.Builder builder =
BlazeCommand.builder(buildBinary, BlazeCommandName.QUERY)
BlazeCommand.builder(buildBinary, BlazeCommandName.QUERY, project)
.addBlazeFlags(BlazeFlags.KEEP_GOING)
.addBlazeFlags("--output=label_kind")
.addBlazeFlags(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void initTest(
@Test
public void addedFlagsShouldGoAtStart() {
List<String> flagsCommand =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN, project)
.addTargets(Label.create("//a:b"))
.addBlazeFlags("--flag1", "--flag2")
.addExeFlags("--exeFlag1", "--exeFlag2")
Expand All @@ -59,7 +59,7 @@ public void addedFlagsShouldGoAtStart() {
@Test
public void targetsShouldGoAfterBlazeFlagsAndDoubleHyphen() {
List<String> command =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN, project)
.addTargets(Label.create("//a:b"), Label.create("//c:d"))
.addBlazeFlags("--flag1", "--flag2")
.addExeFlags("--exeFlag1", "--exeFlag2")
Expand All @@ -74,7 +74,7 @@ public void targetsShouldGoAfterBlazeFlagsAndDoubleHyphen() {
@Test
public void exeFlagsShouldGoLast() {
List<String> command =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN, project)
.addTargets(Label.create("//a:b"), Label.create("//c:d"))
.addBlazeFlags("--flag1", "--flag2")
.addExeFlags("--exeFlag1", "--exeFlag2")
Expand All @@ -87,7 +87,7 @@ public void exeFlagsShouldGoLast() {
@Test
public void maintainUserOrderingOfTargets() {
List<String> command =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.RUN, project)
.addTargets(
Label.create("//a:b"),
TargetExpression.fromStringSafe("-//e:f"),
Expand Down Expand Up @@ -117,7 +117,7 @@ public void maintainUserOrderingOfTargets() {
@Test
public void binaryAndCommandShouldComeFirst() {
List<String> command =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD, project)
.addBlazeFlags("--flag")
.addExeFlags("--exeFlag")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public void testDirectDepsExperimentRespected() {
.containsExactly("intellij-info-generic", "intellij-info-java", "intellij-resolve-java");
}

private static BlazeCommand.Builder emptyBuilder() {
return BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD);
private BlazeCommand.Builder emptyBuilder() {
return BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD, getProject());
}

private static ImmutableList<String> getBlazeFlags(BlazeCommand.Builder builder) {
Expand Down
9 changes: 6 additions & 3 deletions gazelle/src/com/google/idea/blaze/gazelle/GazelleRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;

import java.util.Collection;
import java.util.List;
Expand All @@ -45,10 +46,11 @@ protected static BlazeCommand createGazelleRunCommand(
BuildInvoker invoker,
List<String> blazeFlags,
Label gazelleTarget,
Collection<WorkspacePath> directories
Collection<WorkspacePath> directories,
Project project
) {
BlazeCommand.Builder builder = BlazeCommand.builder(invoker,
BlazeCommandName.RUN);
BlazeCommandName.RUN, project);
builder.addBlazeFlags(blazeFlags);
builder.addTargets(gazelleTarget);
List<String> directoriesToRegenerate =
Expand All @@ -72,5 +74,6 @@ public abstract GazelleRunResult runBlazeGazelle(
List<String> blazeFlags,
Label gazelleTarget,
Collection<WorkspacePath> directories,
ImmutableList<Parser> issueParsers);
ImmutableList<Parser> issueParsers,
Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.intellij.openapi.project.Project;

import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.List;
Expand All @@ -42,9 +44,10 @@ public GazelleRunResult runBlazeGazelle(
List<String> blazeFlags,
Label gazelleTarget,
Collection<WorkspacePath> directories,
ImmutableList<Parser> issueParsers) {
ImmutableList<Parser> issueParsers,
Project project) {
BlazeCommand command = GazelleRunner.createGazelleRunCommand(invoker,
blazeFlags, gazelleTarget, directories);
blazeFlags, gazelleTarget, directories, project);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
int exitCode =
ExternalTask.builder(workspaceRoot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private GazelleRunResult doRunGazelle(
blazeFlags,
gazelleBinary,
importantDirectories,
issueParsers);
issueParsers,
project);
}

private ImmutableList<BlazeIssueParser.Parser> gazelleIssueParsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.idea.blaze.gazelle.GazelleRunResult;
import com.google.idea.blaze.gazelle.GazelleRunner;
import com.google.idea.blaze.gazelle.GazelleUserSettings;
import com.intellij.openapi.project.Project;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -146,12 +147,13 @@ public GazelleRunResult runBlazeGazelle(BlazeContext context,
List<String> blazeFlags,
Label gazelleTarget,
Collection<WorkspacePath> directories,
ImmutableList<BlazeIssueParser.Parser> issueParsers) {
ImmutableList<BlazeIssueParser.Parser> issueParsers,
Project project) {
// For now, we can't run bazel on tests, since some of these tests
// might be run remotely. Therefore, we just check that the Bazel
// command that would have been run has the right shape.
command = GazelleRunner.createGazelleRunCommand(invoker, blazeFlags,
gazelleTarget, directories);
gazelleTarget, directories, project);
return GazelleRunResult.SUCCESS;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private FastBuildState.BuildOutput buildDeployJar(
Stopwatch timer = Stopwatch.createStarted();

BlazeCommand.Builder command =
BlazeCommand.builder(buildParameters.blazeBinary(), BlazeCommandName.BUILD)
BlazeCommand.builder(buildParameters.blazeBinary(), BlazeCommandName.BUILD, project)
.addTargets(deployJarStrategy.getBuildTargets(label, blazeVersionData))
.addBlazeFlags(deployJarStrategy.getBuildFlags(blazeVersionData))
.addBlazeFlags(buildParameters.buildFlags())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static BlazeCommand.Builder getBlazeCommandBuilder(
blazeCommand = BlazeCommandName.COVERAGE;
}
BlazeCommand.Builder command =
BlazeCommand.builder(binaryPath, blazeCommand)
BlazeCommand.builder(binaryPath, blazeCommand, project)
.addTargets(configuration.getTargets())
.addBlazeFlags(
BlazeFlags.blazeFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void kotlinxCoroutinesJavaAgentShouldBeAddedAsJavaAgent() {
@Test
public void getBashCommandsToRunScript() throws Exception {
BlazeCommand.Builder commandBuilder =
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD)
BlazeCommand.builder("/usr/bin/blaze", BlazeCommandName.BUILD, getProject())
.addTargets(Label.create("//label:java_binary_rule"));
List<String> command =
HotSwapCommandBuilder.getBashCommandsToRunScript(getProject(), commandBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static int syncTsConfigTarget(
commandName = BlazeCommandName.RUN;
}
BlazeCommand command =
BlazeCommand.builder(binaryPath, commandName)
BlazeCommand.builder(binaryPath, commandName, project)
.addTargets(target)
.addBlazeFlags(
BlazeFlags.blazeFlags(
Expand Down
Loading

0 comments on commit 732d9d4

Please sign in to comment.