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: copy some aspect files to the client side to prepare for templating #6842

Merged
merged 20 commits into from
Oct 9, 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
438 changes: 221 additions & 217 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ scalatest_repositories()

scalatest_toolchain()

load("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository")

local_repository(
name = "intellij_aspect_template",
path = "aspect_template"
)
1 change: 0 additions & 1 deletion aspect/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ filegroup(
"intellij_info_bundled.bzl",
"intellij_info_impl_bundled.bzl",
"java_classpath.bzl",
"java_info.bzl",
"make_variables.bzl",
":BUILD.bazel",
"//aspect/tools:CreateAar",
Expand Down
2 changes: 1 addition & 1 deletion aspect/fast_build_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load(
":intellij_info_impl.bzl",
"stringify_label",
)
load(":java_info.bzl", "get_java_info")
load("@intellij_aspect_template//:java_info.bzl", "get_java_info")

_DEP_ATTRS = ["deps", "exports", "runtime_deps", "_java_toolchain"]

Expand Down
9 changes: 1 addition & 8 deletions aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ load(
)
load(":flag_hack.bzl", "FlagHackInfo")

# BUNDLED-IGNORE-BEGIN
load(":java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
# BUNDLED-IGNORE-END

# BUNDLED-INCLUDE-BEGIN
##load(":java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
### load("@intellij_aspect_template//:java_info.bzl", "get_java_info")
# BUNDLED-INCLUDE-END
load("@intellij_aspect_template//:java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")

load(
":make_variables.bzl",
Expand Down
2 changes: 1 addition & 1 deletion aspect/java_classpath.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""An aspect which extracts the runtime classpath from a java target."""

load(":java_info.bzl", "get_java_info", "java_info_in_target")
load("@intellij_aspect_template//:java_info.bzl", "get_java_info", "java_info_in_target")

def _runtime_classpath_impl(target, ctx):
"""The top level aspect implementation function.
Expand Down
9 changes: 0 additions & 9 deletions aspect/java_info.bzl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ java_binary(
srcs = ["BazelInvokingIntegrationTestRunner.java"],
data = [
"//aspect:aspect_files",
"//aspect_template:aspect_files",
],
main_class = "com.google.idea.blaze.aspect.integration.BazelInvokingIntegrationTestRunner",
deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ public static void main(String[] a) throws Exception {
"%s=%s/%s/aspect",
AspectRepositoryProvider.OVERRIDE_REPOSITORY_FLAG,
System.getenv("TEST_SRCDIR"),
System.getenv("TEST_WORKSPACE")));
System.getenv("TEST_WORKSPACE")),
String.format(
"%s=%s/%s/aspect_template",
AspectRepositoryProvider.OVERRIDE_REPOSITORY_TEMPLATE_FLAG,
System.getenv("TEST_SRCDIR"),
System.getenv("TEST_WORKSPACE"))
);

if (bazelVersion.isAtLeast(6, 0, 0)
&& !aspectFlags.contains(
Expand Down
33 changes: 33 additions & 0 deletions aspect_template/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
filegroup(
name = "aspect_files",
srcs = [
"WORKSPACE",
"BUILD.bazel",
"java_info.bzl",
"java_info.template.bzl",
],
visibility = ["//visibility:public"],
)

genrule(
name = "generate_java_info_template",
outs = ["java_info.template.bzl"],
srcs = ["java_info.bzl"],
cmd = "cat $(SRCS) >$@ && " +
"sed -i -e '/TEMPLATE-IGNORE-BEGIN/,/TEMPLATE-IGNORE-END/d' $@ && " +
"sed -i -e '/TEMPLATE-INCLUDE-BEGIN/,/TEMPLATE-INCLUDE-END/{s/^[#][#]//;}' $@",
)

genrule(
name = "rename_files",
srcs = ["BUILD.aspect"],
outs = ["BUILD.bazel"],
cmd = "cp $< $@",
)

genrule(
name = "create_workspace_file",
outs = ["WORKSPACE"],
srcs = [],
cmd = r"""echo 'workspace(name = "intellij_aspect_template")' > $@"""
)
Empty file added aspect_template/BUILD.aspect
Empty file.
Empty file added aspect_template/MODULE.bazel
Empty file.
50 changes: 50 additions & 0 deletions aspect_template/java_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# TEMPLATE-INCLUDE-BEGIN
###if( $isJavaEnabled == "true" && $bazel8OrAbove == "true" )
##load("@rules_java//java/common:java_info.bzl", "JavaInfo")
###end
# TEMPLATE-INCLUDE-END

def java_info_in_target(target):
# TEMPLATE-IGNORE-BEGIN
return JavaInfo in target
# TEMPLATE-IGNORE-END

# TEMPLATE-INCLUDE-BEGIN
## #if( $isJavaEnabled == "true" )
## return JavaInfo in target
## #else
## return None
## #end
# TEMPLATE-INCLUDE-END

def get_java_info(target):
# TEMPLATE-IGNORE-BEGIN
if JavaInfo in target:
return target[JavaInfo]
else:
return None
# TEMPLATE-IGNORE-END

# TEMPLATE-INCLUDE-BEGIN
## #if( $isJavaEnabled == "true" )
## if JavaInfo in target:
## return target[JavaInfo]
## else:
## return None
## #else
## return None
## #end
# TEMPLATE-INCLUDE-END

def java_info_reference():
# TEMPLATE-IGNORE-BEGIN
return [JavaInfo]
# TEMPLATE-IGNORE-END

# TEMPLATE-INCLUDE-BEGIN
## #if( $isJavaEnabled == "true" )
## return [JavaInfo]
## #else
## return []
## #end
# TEMPLATE-INCLUDE-END
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public Builder(String binaryPath, BlazeCommandName name, Project project) {
// Tell forge what tool we used to call blaze so we can track usage.
addBlazeFlags(BlazeFlags.getToolTagFlag());

AspectRepositoryProvider.getOverrideFlag(project).ifPresent(this::addBlazeFlags);
Arrays.stream(AspectRepositoryProvider.getOverrideFlags(project)).filter(Optional::isPresent)
.map(Optional::get)
.forEach(this::addBlazeFlags);
}

private ImmutableList<String> getArguments() {
Expand Down
43 changes: 43 additions & 0 deletions base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
import com.google.idea.blaze.base.settings.BlazeUserSettings.FocusBehavior;
import com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException;
import com.google.idea.blaze.base.sync.SyncScope.SyncFailedException;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.google.idea.blaze.base.sync.projectview.SyncDirectoriesWarning;
import com.google.idea.blaze.base.sync.status.BlazeSyncStatus;
import com.google.idea.blaze.base.toolwindow.Task;
import com.google.idea.blaze.base.util.SaveUtil;
import com.google.idea.blaze.base.util.TemplateWriter;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.common.PrintOutput.OutputType;
Expand All @@ -61,7 +63,9 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.util.text.StringUtil;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand All @@ -73,6 +77,10 @@ public class BlazeSyncManager {

private final Project project;
private static final Logger logger = Logger.getInstance(BlazeSyncManager.class);
private final Map<LanguageClass, String> supportedLanguageAspectTemplate = Map.of(
LanguageClass.JAVA, "java_info.template.bzl",
LanguageClass.GENERIC, "java_info.template.bzl"
);

public BlazeSyncManager(Project project) {
this.project = project;
Expand All @@ -96,6 +104,14 @@ public void requestProjectSync(BlazeSyncParams syncParams) {
}
SaveUtil.saveAllFiles();

try {
AspectRepositoryProvider.copyAspectTemplatesIfNotExists(project);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}

prepareProjectAspect();

BlazeImportSettings importSettings =
BlazeImportSettingsManager.getInstance(project).getImportSettings();
if (importSettings == null) {
Expand Down Expand Up @@ -177,6 +193,33 @@ public void requestProjectSync(BlazeSyncParams syncParams) {
});
}

private void prepareProjectAspect() {
var manager =
BlazeProjectDataManager.getInstance(project);

if (manager == null) return;

var projectData = manager.getBlazeProjectData();
if (projectData == null) return;
var optionalAspectTemplateDir = AspectRepositoryProvider.getProjectAspectDirectory(project);
if (optionalAspectTemplateDir.isEmpty()) return;
var aspectTemplateDir = optionalAspectTemplateDir.get().toPath();
var templateWriter = new TemplateWriter(aspectTemplateDir);
var activeLanguages = projectData.getWorkspaceLanguageSettings().getActiveLanguages();
var supportedLanguages = activeLanguages.stream().filter(supportedLanguageAspectTemplate::containsKey);
var isAtLeastBazel8 = projectData.getBlazeVersionData().bazelIsAtLeastVersion(8, 0, 0);
var templateVariableMap = Map.of(
"bazel8OrAbove", isAtLeastBazel8 ? "true" : "false",
"isJavaEnabled", activeLanguages.contains(LanguageClass.JAVA) || activeLanguages.contains(LanguageClass.GENERIC) ? "true" : "false"
);
supportedLanguages.forEach(language -> {
var templateFileName = supportedLanguageAspectTemplate.get(language);
var realizedFileName = templateFileName.replace(".template.bzl", ".bzl");
var realizedFile = aspectTemplateDir.resolve(realizedFileName);
templateWriter.writeToFile(templateFileName, realizedFile, templateVariableMap);
});
}

@VisibleForTesting
boolean shouldForceFullSync(
BlazeProjectData oldProjectData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException;
import com.google.idea.blaze.base.sync.SyncScope.SyncFailedException;
import com.google.idea.blaze.base.sync.aspects.BlazeIdeInterface;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider;
import com.google.idea.blaze.base.sync.data.BlazeDataStorage;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.google.idea.blaze.base.sync.libraries.BlazeLibraryCollector;
Expand Down Expand Up @@ -77,6 +78,7 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,100 @@
import com.intellij.openapi.project.Project;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

public interface AspectRepositoryProvider {
ExtensionPointName<AspectRepositoryProvider> EP_NAME =
ExtensionPointName.create("com.google.idea.blaze.AspectRepositoryProvider");

String OVERRIDE_REPOSITORY_FLAG = "--override_repository=intellij_aspect";
String OVERRIDE_REPOSITORY_TEMPLATE_FLAG = "--override_repository=intellij_aspect_template";

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

static Optional<File> findAspectDirectory(Project project) {
default Optional<File> aspectTemplateDirectory() {
return Optional.empty();
}

public static Optional<File> getProjectAspectDirectory(Project project) {
String basePath = project.getBasePath();
return basePath == null ? Optional.empty() : Optional.of(Paths.get(basePath).resolve("aspect").toFile());
}

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

static Optional<String> getOverrideFlag(Project project) {
return findAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath());
private static Optional<File> findAspectTemplateDirectory() {
return EP_NAME.getExtensionsIfPointIsRegistered().stream()
.map(AspectRepositoryProvider::aspectTemplateDirectory)
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}

static Optional<String>[] getOverrideFlags(Project project) {
return new Optional[] {
getOverrideFlagForAspectDirectory(),
getOverrideFlagForProjectAspectDirectory(project),
};
}

private static Optional<String> getOverrideFlagForAspectDirectory() {
return findAspectDirectory().map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath());
}

private static Optional<String> getOverrideFlagForProjectAspectDirectory(Project project) {
return getProjectAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_TEMPLATE_FLAG + "=" + it.getPath());
}

static void copyAspectTemplatesIfNotExists(Project project) throws ExecutionException {
Path destinationAspectsPath = getProjectAspectDirectory(project).map(File::toPath).orElse(null);
if (destinationAspectsPath == null) {
throw new IllegalStateException("Missing project aspect directory");
}
if (!destinationAspectsPath.toFile().exists()) {
try {
copyAspectTemplatesFromResources(destinationAspectsPath);
} catch (IOException e) {
throw new ExecutionException(e);
}
}
}

private static void copyAspectTemplatesFromResources(Path destinationPath) throws IOException {
Path aspectPath = findAspectTemplateDirectory().map(File::toPath).orElse(null);
if (aspectPath != null && Files.isDirectory(aspectPath)) {
copyFileTree(aspectPath, destinationPath);
} else {
System.out.println("Missing aspects resource");
}
}

private static void copyFileTree(Path source, Path destination) throws IOException {
Stream<Path> paths = Files.walk(source);
paths.forEach(path -> {
try {
copyUsingRelativePath(source, path, destination);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private static void copyUsingRelativePath(Path sourcePrefix, Path source, Path destination) throws IOException {
// only interested in bzl files that are templates
if (source.endsWith(".bzl") && !source.endsWith("template.bzl")) return;
String sourceRelativePath = sourcePrefix.relativize(source).toString();
Path destinationAbsolutePath = Paths.get(destination.toString(), sourceRelativePath);
Files.copy(source, destinationAbsolutePath);
}
}
Loading
Loading