Skip to content

Commit

Permalink
[maintenance] copy java_info.bzl to project side to prepare for templ…
Browse files Browse the repository at this point in the history
…ating it.
  • Loading branch information
xuansontrinh committed Oct 4, 2024
1 parent cd8784c commit 0907230
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 97 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
build --java_language_version=17 --java_runtime_version=17

build --override_repository=intellij_aspect_template=aspect_template

# Delete test data packages, needed for bazel integration tests. Update by running the following command:
# bazel run @rules_bazel_integration_test//tools:update_deleted_packages
build --deleted_packages=aspect/testing/tests/src/com/google/idea/blaze/aspect/integration/testdata,clwb/tests/projects/simple/main,clwb/tests/projects/virtual_includes/lib/strip_absolut,clwb/tests/projects/virtual_includes/lib/strip_relative,clwb/tests/projects/virtual_includes/main
Expand Down
96 changes: 11 additions & 85 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

scalatest_toolchain()


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 @@ -37,7 +37,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
2 changes: 1 addition & 1 deletion aspect/intellij_info_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ load(
"struct_omit_none",
"to_artifact_location",
)
load(":java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
load("@intellij_aspect_template//:java_info.bzl", "get_java_info", "java_info_in_target", "java_info_reference")
load(
":make_variables.bzl",
"expand_make_variables",
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
5 changes: 5 additions & 0 deletions aspect_template/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "aspect_files",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
Empty file added aspect_template/MODULE.bazel
Empty file.
2 changes: 2 additions & 0 deletions aspect/java_info.bzl → aspect_template/java_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ def java_info_in_target(target):
def get_java_info(target):
if JavaInfo in target:
return target[JavaInfo]
else:
None

def java_info_reference():
return [JavaInfo]
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.getOverrideFlag(project)).filter(Optional::isPresent)
.map(Optional::get)
.forEach(this::addBlazeFlags);
}

private ImmutableList<String> getArguments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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;
Expand All @@ -61,6 +62,7 @@
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.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -209,6 +211,7 @@ boolean shouldForceFullSync(
private static void executeTask(Project project, BlazeSyncParams params, BlazeContext context) {
Future<?> querySyncPromoFuture = new QuerySyncPromo(project).getPromoShowFuture();
try {
AspectRepositoryProvider.copyAspectsIfNotExists(project);
SyncPhaseCoordinator.getInstance(project).syncProject(params, context).get();
} catch (InterruptedException e) {
context.output(new PrintOutput("Sync interrupted: " + e.getMessage()));
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,99 @@
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();
}

private 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>[] getOverrideFlag(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 copyAspectsIfNotExists(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 {
copyAspectsFromResources(destinationAspectsPath);
} catch (IOException e) {
throw new ExecutionException(e);
}

}
}

private static void copyAspectsFromResources(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 {
String sourceRelativePath = sourcePrefix.relativize(source).toString();
Path destinationAbsolutePath = Paths.get(destination.toString(), sourceRelativePath);
Files.copy(source, destinationAbsolutePath);
}
}
Loading

0 comments on commit 0907230

Please sign in to comment.