Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Aspect templating - 3/n - pass 'Project' to AspectRepositoryPr… #6818

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void build(BlazeContext context, BlazeAndroidDeviceSelector.DeviceSession
Blaze.getBuildSystemProvider(project)
.getBuildSystem()
.getBuildInvoker(project, context, BlazeCommandName.MOBILE_INSTALL);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.MOBILE_INSTALL);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.MOBILE_INSTALL, project);

if (passAdbArgWithSerialToMi.getValue()) {
// Redundant, but we need this to get around bug in bazel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void build(BlazeContext context, BlazeAndroidDeviceSelector.DeviceSession

BuildInvoker invoker =
Blaze.getBuildSystemProvider(project).getBuildSystem().getBuildInvoker(project, context);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.BUILD);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project);
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);

// TODO(mathewi) we implicitly rely here on the fact that the getBuildInvoker() call above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void build(BlazeContext context, BlazeAndroidDeviceSelector.DeviceSession

BuildInvoker invoker =
Blaze.getBuildSystemProvider(project).getBuildSystem().getBuildInvoker(project, context);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.BUILD);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.BUILD, project);
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);

try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public void run(@NotNull BlazeLaunchContext launchContext)
Blaze.getBuildSystemProvider(project)
.getBuildSystem()
.getBuildInvoker(project, context),
BlazeCommandName.TEST)
BlazeCommandName.TEST,
project)
.addTargets(target);
// Build flags must match BlazeBeforeRunTask.
commandBuilder.addBlazeFlags(buildFlags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public void infoOutputGroups_renderResolveIsNotIncluded() {
assertThat(getOutputGroups(builder)).doesNotContain("intellij-render-resolve-android");
}

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> getOutputGroups(BlazeCommand.Builder builder) {
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private ProcessHandler createProcess(CommandLineState state, List<String> extraB
BlazeCommand.Builder commandBuilder =
BlazeCommand.builder(
Blaze.getBuildSystemProvider(project).getBinaryPath(project),
handlerState.getCommandState().getCommand())
handlerState.getCommandState().getCommand(),
project)
.addTargets(configuration.getTargets())
.addBlazeFlags(extraBlazeFlags)
.addBlazeFlags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider;
import com.google.idea.testing.runfiles.Runfiles;
import com.intellij.openapi.project.Project;

import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

public class TestAspectRepositoryProvider implements AspectRepositoryProvider {

@Override
public Optional<File> aspectDirectory() {
public Optional<File> aspectDirectory(Project project) {
return Optional.of(Runfiles.runfilesPath("aspect")).map(Path::toFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project
}
String xcodeVersion = versions.xcodeVersion.get();
String developerDir = XcodeCompilerSettingsProviderImpl.queryDeveloperDir(context, invoker,
workspaceRoot, xcodeVersion);
workspaceRoot, xcodeVersion, project);
String sdkVersionString = String.format("MacOSX%s.sdk", macosSdkVersion);
settings = Optional.of(XcodeCompilerSettings.create(
Path.of(developerDir),
Expand Down Expand Up @@ -160,8 +160,8 @@ public boolean processLine(String line) {
* https://github.com/bazelbuild/bazel/blob/1811e82ca4e68c2dd52eed7907c3d1926237e18a/src/main/java/com/google/devtools/build/lib/exec/local/XcodeLocalEnvProvider.java#L241
*/
private static String queryDeveloperDir(BlazeContext context, BuildInvoker invoker,
WorkspaceRoot workspaceRoot, String xcodeVersion) throws XcodeCompilerSettingsException {
BlazeCommand.Builder runXcodeLocator = BlazeCommand.builder(invoker, BlazeCommandName.RUN);
WorkspaceRoot workspaceRoot, String xcodeVersion, Project project) throws XcodeCompilerSettingsException {
BlazeCommand.Builder runXcodeLocator = BlazeCommand.builder(invoker, BlazeCommandName.RUN, project);
runXcodeLocator.addTargets(
Label.fromStringSafe("@bazel_tools//tools/osx:xcode-locator"));
runXcodeLocator.addExeFlags(xcodeVersion).build();
Expand Down
Loading
Loading