diff --git a/.bazelrc b/.bazelrc index d8f1bc891d8..54824f1298b 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index c21e9d741de..ccca37d8ee0 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -54,8 +54,8 @@ "https://bcr.bazel.build/modules/gazelle/0.33.0/MODULE.bazel": "a13a0f279b462b784fb8dd52a4074526c4a2afe70e114c7d09066097a46b3350", "https://bcr.bazel.build/modules/gazelle/0.34.0/MODULE.bazel": "abdd8ce4d70978933209db92e436deb3a8b737859e9354fb5fd11fb5c2004c8a", "https://bcr.bazel.build/modules/gazelle/0.36.0/MODULE.bazel": "e375d5d6e9a6ca59b0cb38b0540bc9a05b6aa926d322f2de268ad267a2ee74c0", - "https://bcr.bazel.build/modules/gazelle/0.39.0/MODULE.bazel": "973d4c7bffa3518b76a7b8a89db5468fd1098e3bac9f80d60a9f5a5fdc4a544b", - "https://bcr.bazel.build/modules/gazelle/0.39.0/source.json": "96a9b8c58e2ed71f7b73d4f22770592e1c7eabc20d73818900ef3d2961e16e72", + "https://bcr.bazel.build/modules/gazelle/0.39.1/MODULE.bazel": "1fa3fefad240e535066fd0e6950dfccd627d36dc699ee0034645e51dbde3980f", + "https://bcr.bazel.build/modules/gazelle/0.39.1/source.json": "f2facfa8c8c9a4d2ebf613754023054c2eb793b88675082216c6be0419eb20a1", "https://bcr.bazel.build/modules/google_benchmark/1.8.2/MODULE.bazel": "a70cf1bba851000ba93b58ae2f6d76490a9feb74192e57ab8e8ff13c34ec50cb", "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", @@ -91,11 +91,12 @@ "https://bcr.bazel.build/modules/rules_buf/0.1.1/MODULE.bazel": "6189aec18a4f7caff599ad41b851ab7645d4f1e114aa6431acf9b0666eb92162", "https://bcr.bazel.build/modules/rules_buf/0.1.1/source.json": "021363d254f7438f3f10725355969c974bb2c67e0c28667782ade31a9cdb747f", "https://bcr.bazel.build/modules/rules_cc/0.0.1/MODULE.bazel": "cb2aa0747f84c6c3a78dad4e2049c154f08ab9d166b1273835a8174940365647", + "https://bcr.bazel.build/modules/rules_cc/0.0.10/MODULE.bazel": "ec1705118f7eaedd6e118508d3d26deba2a4e76476ada7e0e3965211be012002", + "https://bcr.bazel.build/modules/rules_cc/0.0.10/source.json": "90310b16e0e7df0cf40f8d1dccd7d373360f42419a6bfbbf5bb013182dd70e84", "https://bcr.bazel.build/modules/rules_cc/0.0.2/MODULE.bazel": "6915987c90970493ab97393024c156ea8fb9f3bea953b2f3ec05c34f19b5695c", "https://bcr.bazel.build/modules/rules_cc/0.0.6/MODULE.bazel": "abf360251023dfe3efcef65ab9d56beefa8394d4176dd29529750e1c57eaa33f", "https://bcr.bazel.build/modules/rules_cc/0.0.8/MODULE.bazel": "964c85c82cfeb6f3855e6a07054fdb159aced38e99a5eecf7bce9d53990afa3e", "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", - "https://bcr.bazel.build/modules/rules_cc/0.0.9/source.json": "1f1ba6fea244b616de4a554a0f4983c91a9301640c8fe0dd1d410254115c8430", "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/source.json": "c8b1e2c717646f1702290959a3302a178fb639d987ab61d548105019f11e527e", @@ -105,7 +106,6 @@ "https://bcr.bazel.build/modules/rules_go/0.41.0/MODULE.bazel": "55861d8e8bb0e62cbd2896f60ff303f62ffcb0eddb74ecb0e5c0cbe36fc292c8", "https://bcr.bazel.build/modules/rules_go/0.42.0/MODULE.bazel": "8cfa875b9aa8c6fce2b2e5925e73c1388173ea3c32a0db4d2b4804b453c14270", "https://bcr.bazel.build/modules/rules_go/0.46.0/MODULE.bazel": "3477df8bdcc49e698b9d25f734c4f3a9f5931ff34ee48a2c662be168f5f2d3fd", - "https://bcr.bazel.build/modules/rules_go/0.47.0/MODULE.bazel": "e425890d2a4d668abc0f59d8388b70bf63ad025edec76a385c35d85882519417", "https://bcr.bazel.build/modules/rules_go/0.50.1/MODULE.bazel": "b91a308dc5782bb0a8021ad4330c81fea5bda77f96b9e4c117b9b9c8f6665ee0", "https://bcr.bazel.build/modules/rules_go/0.50.1/source.json": "205765fd30216c70321f84c9a967267684bdc74350af3f3c46c857d9f80a4fa2", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", @@ -114,11 +114,14 @@ "https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31", "https://bcr.bazel.build/modules/rules_java/7.1.0/MODULE.bazel": "30d9135a2b6561c761bd67bd4990da591e6bdc128790ce3e7afd6a3558b2fb64", "https://bcr.bazel.build/modules/rules_java/7.11.1/MODULE.bazel": "b4782e019dd0b0151bd49fd8929136fd4441f527eb208fbd991b77e480b7236e", - "https://bcr.bazel.build/modules/rules_java/7.11.1/source.json": "94b8c8bc691357f1f0bf630f09010d734d081caea8c82d5457e56ee4659101a1", + "https://bcr.bazel.build/modules/rules_java/7.12.1/MODULE.bazel": "0a2ebb53b48a6eb092aef24b36db23294d4d3ebf96bff02b0ccc962bdc70717d", + "https://bcr.bazel.build/modules/rules_java/7.12.1/source.json": "2ab5ceabe9d87a773fa44e4cce42c950e34ff6d2f5164e7413088542fa4f1f3e", "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", + "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", "https://bcr.bazel.build/modules/rules_java/7.6.5/MODULE.bazel": "481164be5e02e4cab6e77a36927683263be56b7e36fef918b458d7a8a1ebadb1", "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", "https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909", + "https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036", "https://bcr.bazel.build/modules/rules_jvm_external/6.4/MODULE.bazel": "e859f460f0fcebbee0af2ac65a38c952eedb60612956395ba414fdf78babae2f", "https://bcr.bazel.build/modules/rules_jvm_external/6.4/source.json": "752c79441c87f8945b1aeddbde2864661e2ca594c82700b8c2d4550d080070d9", "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/MODULE.bazel": "d269a01a18ee74d0335450b10f62c9ed81f2321d7958a2934e44272fe82dcef3", @@ -157,7 +160,8 @@ "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", "https://bcr.bazel.build/modules/stardoc/0.5.4/MODULE.bazel": "6569966df04610b8520957cb8e97cf2e9faac2c0309657c537ab51c16c18a2a4", "https://bcr.bazel.build/modules/stardoc/0.5.6/MODULE.bazel": "c43dabc564990eeab55e25ed61c07a1aadafe9ece96a4efabb3f8bf9063b71ef", - "https://bcr.bazel.build/modules/stardoc/0.5.6/source.json": "956954c9c45ef492ea4001ce579dc40431fbd75090151e8f9eadf9ed6377a108", + "https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c", + "https://bcr.bazel.build/modules/stardoc/0.7.0/source.json": "e3c524bf2ef20992539ce2bc4a2243f4853130209ee831689983e28d05769099", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", "https://bcr.bazel.build/modules/upb/0.0.0-20230516-61a97ef/MODULE.bazel": "c0df5e35ad55e264160417fd0875932ee3c9dda63d9fccace35ac62f45e1b6f9", "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", @@ -697,84 +701,6 @@ "recordedRepoMappingEntries": [] } }, - "@@rules_bazel_integration_test+//:extensions.bzl%bazel_binaries": { - "general": { - "bzlTransitiveDigest": "ruMfvWwZzwPPkk+exdpYAe94RD+4erzc4ePrZ08MuKc=", - "usagesDigest": "NOmZNyUfHK4TOR2tGFP4OfRYOHmgVVZiVibKj0w/wws=", - "recordedFileInputs": {}, - "recordedDirentsInputs": {}, - "envVariables": {}, - "generatedRepoSpecs": { - "build_bazel_bazel_7_2_1": { - "bzlFile": "@@rules_bazel_integration_test+//bazel_integration_test/private:bazel_binaries.bzl", - "ruleClassName": "bazel_binary", - "attributes": { - "version": "7.2.1", - "bazelisk": "@bazel_binaries_bazelisk//:bazelisk" - } - }, - "build_bazel_bazel_5_4_1": { - "bzlFile": "@@rules_bazel_integration_test+//bazel_integration_test/private:bazel_binaries.bzl", - "ruleClassName": "bazel_binary", - "attributes": { - "version": "5.4.1", - "bazelisk": "@bazel_binaries_bazelisk//:bazelisk" - } - }, - "build_bazel_bazel_6_5_0": { - "bzlFile": "@@rules_bazel_integration_test+//bazel_integration_test/private:bazel_binaries.bzl", - "ruleClassName": "bazel_binary", - "attributes": { - "version": "6.5.0", - "bazelisk": "@bazel_binaries_bazelisk//:bazelisk" - } - }, - "bazel_binaries_bazelisk": { - "bzlFile": "@@rules_bazel_integration_test+//bazel_integration_test/private:bazel_binaries.bzl", - "ruleClassName": "bazelisk_binary", - "attributes": { - "version": "1.18.0" - } - }, - "bazel_binaries": { - "bzlFile": "@@rules_bazel_integration_test+//bazel_integration_test/bzlmod:bazel_binaries.bzl", - "ruleClassName": "_bazel_binaries_helper", - "attributes": { - "version_to_repo": { - "5.4.1": "build_bazel_bazel_5_4_1", - "6.5.0": "build_bazel_bazel_6_5_0", - "7.2.1": "build_bazel_bazel_7_2_1" - }, - "current_version": "7.2.1" - } - } - }, - "moduleExtensionMetadata": { - "explicitRootModuleDirectDeps": [], - "explicitRootModuleDirectDevDeps": [ - "bazel_binaries_bazelisk", - "build_bazel_bazel_5_4_1", - "build_bazel_bazel_6_5_0", - "build_bazel_bazel_7_2_1", - "bazel_binaries" - ], - "useAllRepos": "NO", - "reproducible": false - }, - "recordedRepoMappingEntries": [ - [ - "rules_bazel_integration_test+", - "bazel_skylib", - "bazel_skylib+" - ], - [ - "rules_bazel_integration_test+", - "cgrindel_bazel_starlib", - "cgrindel_bazel_starlib+" - ] - ] - } - }, "@@rules_buf+//buf:extensions.bzl%ext": { "general": { "bzlTransitiveDigest": "gmPmM7QT5Jez2VVFcwbbMf/QWSRag+nJ1elFJFFTcn0=", @@ -990,7 +916,7 @@ }, "@@rules_rust+//rust:extensions.bzl%rust": { "general": { - "bzlTransitiveDigest": "3VwM3CNCtyMEBDp4Ys8NbSCPA5VhaEmhJWOf7nQF9Jg=", + "bzlTransitiveDigest": "hZ5xYerIbbeKW9kuWlCaJhzSpXT6mVaOCL7qFyNtU8Y=", "usagesDigest": "NdRlK3VAWziNngvGI/L5ZlYt0dQKHyo20r++0i/MOwM=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod index db913ff14ae..4b111c310d1 100644 --- a/WORKSPACE.bzlmod +++ b/WORKSPACE.bzlmod @@ -31,4 +31,7 @@ scalatest_repositories() scalatest_toolchain() - +local_repository( + name = "intellij_aspect_template", + path = "aspect-template", +) diff --git a/aspect/BUILD b/aspect/BUILD index 5b75ae6852d..00ffa85b458 100644 --- a/aspect/BUILD +++ b/aspect/BUILD @@ -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", diff --git a/aspect/fast_build_info.bzl b/aspect/fast_build_info.bzl index 9600f0baae1..4e40ac3df39 100644 --- a/aspect/fast_build_info.bzl +++ b/aspect/fast_build_info.bzl @@ -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"] diff --git a/aspect/intellij_info_impl.bzl b/aspect/intellij_info_impl.bzl index 0d421daf2d5..7f75b9a3162 100644 --- a/aspect/intellij_info_impl.bzl +++ b/aspect/intellij_info_impl.bzl @@ -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", diff --git a/aspect/java_classpath.bzl b/aspect/java_classpath.bzl index 7ad254f7517..789b622514e 100644 --- a/aspect/java_classpath.bzl +++ b/aspect/java_classpath.bzl @@ -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. diff --git a/aspect_template/BUILD b/aspect_template/BUILD new file mode 100644 index 00000000000..94c750d4bf8 --- /dev/null +++ b/aspect_template/BUILD @@ -0,0 +1,5 @@ +filegroup( + name = "aspect_files", + srcs = glob(["**"]), + visibility = ["//visibility:public"], +) \ No newline at end of file diff --git a/aspect_template/MODULE.bazel b/aspect_template/MODULE.bazel new file mode 100644 index 00000000000..e69de29bb2d diff --git a/aspect/java_info.bzl b/aspect_template/java_info.bzl similarity index 89% rename from aspect/java_info.bzl rename to aspect_template/java_info.bzl index c8bd2b78140..5ebe892f142 100644 --- a/aspect/java_info.bzl +++ b/aspect_template/java_info.bzl @@ -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] diff --git a/base/src/com/google/idea/blaze/base/command/BlazeCommand.java b/base/src/com/google/idea/blaze/base/command/BlazeCommand.java index d2c0b68c9e8..6217f8aae50 100644 --- a/base/src/com/google/idea/blaze/base/command/BlazeCommand.java +++ b/base/src/com/google/idea/blaze/base/command/BlazeCommand.java @@ -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 getArguments() { diff --git a/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java b/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java index 2c0dfed2e7f..dcca4b1e928 100644 --- a/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java +++ b/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java @@ -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; @@ -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; @@ -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())); diff --git a/base/src/com/google/idea/blaze/base/sync/ProjectUpdateSyncTask.java b/base/src/com/google/idea/blaze/base/sync/ProjectUpdateSyncTask.java index 174d9679903..f8521a2d8c2 100644 --- a/base/src/com/google/idea/blaze/base/sync/ProjectUpdateSyncTask.java +++ b/base/src/com/google/idea/blaze/base/sync/ProjectUpdateSyncTask.java @@ -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; @@ -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; diff --git a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectRepositoryProvider.java b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectRepositoryProvider.java index 43a10ab3ec2..3900ea8d151 100644 --- a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectRepositoryProvider.java +++ b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectRepositoryProvider.java @@ -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 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 aspectDirectory(Project project); + Optional aspectDirectory(); - static Optional findAspectDirectory(Project project) { + default Optional aspectTemplateDirectory() { + return Optional.empty(); + } + + private static Optional getProjectAspectDirectory(Project project) { + String basePath = project.getBasePath(); + return basePath == null ? Optional.empty() : Optional.of(Paths.get(basePath).resolve("aspect").toFile()); + } + + private static Optional findAspectDirectory() { return EP_NAME.getExtensionsIfPointIsRegistered().stream() - .map(aspectRepositoryProvider -> aspectRepositoryProvider.aspectDirectory(project)) + .map(AspectRepositoryProvider::aspectDirectory) .filter(Optional::isPresent) .findFirst() .orElse(Optional.empty()); } - static Optional getOverrideFlag(Project project) { - return findAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath()); + private static Optional findAspectTemplateDirectory() { + return EP_NAME.getExtensionsIfPointIsRegistered().stream() + .map(AspectRepositoryProvider::aspectTemplateDirectory) + .filter(Optional::isPresent) + .findFirst() + .orElse(Optional.empty()); + } + + static Optional[] getOverrideFlag(Project project) { + return new Optional[] { + getOverrideFlagForAspectDirectory(), + getOverrideFlagForProjectAspectDirectory(project), + }; + } + + private static Optional getOverrideFlagForAspectDirectory() { + return findAspectDirectory().map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath()); + } + + private static Optional 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 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); } } diff --git a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectStrategyBazel.java b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectStrategyBazel.java index 6fa0e8db356..38f7305e85b 100644 --- a/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectStrategyBazel.java +++ b/base/src/com/google/idea/blaze/base/sync/aspects/strategy/AspectStrategyBazel.java @@ -43,10 +43,16 @@ public AspectStrategy getStrategy(BlazeVersionData versionData) { static final class RepositoryProvider implements AspectRepositoryProvider { @Override - public Optional aspectDirectory(Project project) { + public Optional aspectDirectory() { return Optional.ofNullable(PluginManager.getPluginByClass(AspectStrategy.class)) .map((it) -> new File(it.getPath(), "aspect")); } + + @Override + public Optional aspectTemplateDirectory() { + return Optional.ofNullable(PluginManager.getPluginByClass(AspectStrategy.class)) + .map((it) -> new File(it.getPath(), "aspect_template")); + } } @VisibleForTesting diff --git a/base/src/com/google/idea/blaze/base/util/FileUtil.java b/base/src/com/google/idea/blaze/base/util/FileUtil.java new file mode 100644 index 00000000000..534913a0cc0 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/util/FileUtil.java @@ -0,0 +1,16 @@ +package com.google.idea.blaze.base.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class FileUtil { + public static void writeIfDifferent(Path path, String fileContent) throws IOException { + if (!Files.exists(path) || !getFileContent(path).equals(fileContent)) { + Files.writeString(path, fileContent); + } + } + private static String getFileContent(Path path) throws IOException { + return Files.readString(path); + } +} diff --git a/base/src/com/google/idea/blaze/base/util/TemplateUtil.java b/base/src/com/google/idea/blaze/base/util/TemplateUtil.java new file mode 100644 index 00000000000..2e3269bed76 --- /dev/null +++ b/base/src/com/google/idea/blaze/base/util/TemplateUtil.java @@ -0,0 +1,47 @@ +package com.google.idea.blaze.base.util; + +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; + +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Properties; + +public class TemplateUtil { + + private final Path resourcePath; + private final VelocityEngine velocityEngine; + + public TemplateUtil(Path resourcePath) { + this.resourcePath = resourcePath; + this.velocityEngine = new VelocityEngine(); + this.velocityEngine.init(getProperties()); + } + + private Properties getProperties() { + Properties props = new Properties(); + props.put("resource.loader.file.path", resourcePath.toAbsolutePath().toString()); + props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); + return props; + } + + public void writeToFile(String templateFilePath, Path outputFile, Map variableMap) { + try { + org.apache.velocity.Template template = velocityEngine.getTemplate(templateFilePath); + VelocityContext context = new VelocityContext(); + for (Map.Entry entry : variableMap.entrySet()) { + context.put(entry.getKey(), entry.getValue()); + } + StringWriter writer = new StringWriter(); + template.merge(context, writer); + FileUtil.writeIfDifferent(outputFile, writer.toString()); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error writing template to file", e); + } + } +} diff --git a/ijwb/BUILD b/ijwb/BUILD index 45c0eb10a9e..b819038dca2 100644 --- a/ijwb/BUILD +++ b/ijwb/BUILD @@ -113,6 +113,15 @@ repackaged_files( java_copts = ["-source", "8", "-target", "8"], ) +repackaged_files( + name = "aspect_template_directory", + srcs = ["//aspect_template:aspect_files"], + prefix = "ijwb/aspect_template", + strip_prefix = "/aspect_template", + visibility = IJWB_PACKAGES_VISIBILIY, + java_copts = ["-source", "8", "-target", "8"], +) + repackaged_files( name = "fast_build_javac", srcs = ["//java:fast_build_javac"], @@ -125,6 +134,7 @@ intellij_plugin_debug_target( name = "ijwb_bazel_dev", deps = [ ":aspect_directory", + ":aspect_template_directory", ":fast_build_javac", ":plugin_jar", ], @@ -134,6 +144,7 @@ plugin_deploy_zip( name = "ijwb_bazel_zip", srcs = [ ":aspect_directory", + ":aspect_template_directory", ":fast_build_javac", ":plugin_jar", ],