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

try aspect templating #6868

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion aspect/fast_build_info.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""An aspect to gather info needed by the FastBuildService."""

load("@intellij_aspect_variadic//:java_info.bzl", "get_java_info")
load(
":artifacts.bzl",
"artifact_location",
Expand All @@ -10,7 +11,6 @@ load(
":intellij_info_impl.bzl",
"stringify_label",
)
load(":java_info.bzl", "get_java_info")

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

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_variadic//:java_info.bzl", "get_java_info", "java_info_in_target")

def _runtime_classpath_impl(target, ctx):
"""The top level aspect implementation function.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.google.idea.blaze.base.sync.aspects.strategy;

import com.google.idea.blaze.base.model.primitives.LanguageClass;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import org.apache.velocity.app.Velocity;

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

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_VARIADIC_FLAG = "--override_repository=intellij_aspect_variadic";

Optional<File> aspectDirectory(Project project);

Expand All @@ -22,7 +28,38 @@ static Optional<File> findAspectDirectory(Project project) {
.orElse(Optional.empty());
}

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

static Optional<List<String>> getOverrideFlag(Project project) {

var manager =
BlazeProjectDataManager.getInstance(project);

if(manager == null) {
return Optional.empty();
}

var projectData = manager.getBlazeProjectData();
if (projectData == null) {
return Optional.empty();
}
var languages = projectData.getWorkspaceLanguageSettings().getActiveLanguages();
// if (settings.isLanguageActive(LanguageClass.JAVA)) {
// System.out.println("Java supported");
// }

return findAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath())
.flatMap(it1 ->
findVariadicAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_VARIADIC_FLAG + "=" + it.getPath()).map(
it2 -> List.of(it1, it2)
));
}

Optional<File> variadicAspectDirectory(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
package com.google.idea.blaze.base.sync.aspects.strategy;

import com.google.common.annotations.VisibleForTesting;
import com.google.idea.blaze.base.command.info.BlazeInfoProvider;
import com.google.idea.blaze.base.model.BlazeVersionData;
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 org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import javax.annotation.Nullable;

Expand All @@ -36,19 +40,44 @@ static final class Provider implements AspectStrategyProvider {
@Nullable
public AspectStrategy getStrategy(BlazeVersionData versionData) {
return versionData.buildSystem() == BuildSystemName.Bazel
? new AspectStrategyBazel(versionData)
: null;
? new AspectStrategyBazel(versionData)
: null;
}
}

static final class RepositoryProvider implements AspectRepositoryProvider {
@Override
public Optional<File> aspectDirectory(Project project) {
return Optional.ofNullable(PluginManager.getPluginByClass(AspectStrategy.class))
.map((it) -> new File(it.getPath(), "aspect"));
.map((it) -> new File(it.getPath(), "aspect"));
}

@Override
public Optional<File> variadicAspectDirectory(Project project) {
try {
var aspectDirectory = aspectDirectory(project);
var basePath = project.getBasePath();
var variadicPath = Paths.get(basePath, "aspect");
if (!Files.exists(variadicPath)) {
Files.createDirectory(variadicPath);
}
var destFile = variadicPath.resolve("java_info.bzl").toFile();
var sourceFile = aspectDirectory.map(d -> new File(d, "java_info.bzl"));
if(!FileUtils.contentEquals(destFile, sourceFile.get())){
FileUtils.copyFile(sourceFile.get(), destFile);
}
var moduleFile = variadicPath.resolve("MODULE.bazel").toFile().createNewFile(); //#todo workspace
var buildFile = variadicPath.resolve("BUILD.bazel").toFile().createNewFile(); //#todo workspace
System.out.println(destFile);
return Optional.of(variadicPath.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}

}
}


@VisibleForTesting
public AspectStrategyBazel(BlazeVersionData versionData) {
super(/* aspectSupportsDirectDepsTrimming= */ true);
Expand Down
Loading