From 41a13e44e8ddf47cd69c740b96d0cb932d9da020 Mon Sep 17 00:00:00 2001 From: Rin Kuryloski Date: Fri, 30 Sep 2022 12:32:52 +0200 Subject: [PATCH] Add //tools:erlang_ls_files that generates a tree for Erlang LS in the layout Erlang LS expects --- deps/rabbitmq_amqp1_0/BUILD.bazel | 6 +-- tools/BUILD.bazel | 10 ++++- tools/erlang_ls.bzl | 62 ++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/deps/rabbitmq_amqp1_0/BUILD.bazel b/deps/rabbitmq_amqp1_0/BUILD.bazel index 71d90ac52ec1..3ddac7482b0f 100644 --- a/deps/rabbitmq_amqp1_0/BUILD.bazel +++ b/deps/rabbitmq_amqp1_0/BUILD.bazel @@ -80,12 +80,12 @@ suites = [ PACKAGE, name = "command_SUITE", size = "medium", - deps = [ - "//deps/amqp10_common:erlang_app", - ], runtime_deps = [ "//deps/amqp10_client:erlang_app", ], + deps = [ + "//deps/amqp10_common:erlang_app", + ], ), rabbitmq_integration_suite( PACKAGE, diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index e90ac62a9b3a..ad3f59297499 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -1,4 +1,12 @@ -load(":erlang_ls.bzl", "erlang_ls_config") +load("//:rabbitmq.bzl", "all_plugins") +load(":erlang_ls.bzl", "erlang_ls_config", "erlang_ls_tree") + +erlang_ls_tree( + name = "erlang_ls_files", + apps = all_plugins( + rabbitmq_workspace = "", + ), +) erlang_ls_config( name = "erlang_ls.config", diff --git a/tools/erlang_ls.bzl b/tools/erlang_ls.bzl index 2dbdea821bcf..6e532c8a7149 100644 --- a/tools/erlang_ls.bzl +++ b/tools/erlang_ls.bzl @@ -1,9 +1,21 @@ +load( + "@rules_erlang//:erlang_app_info.bzl", + "ErlangAppInfo", +) load( "@rules_erlang//tools:erlang_toolchain.bzl", "erlang_dirs", ) +load( + "@rules_erlang//:util.bzl", + "path_join", +) +load( + "@rules_erlang//private:util.bzl", + "additional_file_dest_relative_path", +) -def _impl(ctx): +def _erlang_ls_config(ctx): out = ctx.actions.declare_file(ctx.label.name) (erlang_home, _, _) = erlang_dirs(ctx) @@ -34,8 +46,54 @@ plt_path: bazel-bin/deps/rabbit/.base_plt.plt ] erlang_ls_config = rule( - implementation = _impl, + implementation = _erlang_ls_config, toolchains = [ "@rules_erlang//tools:toolchain_type", ], ) + +def _erlang_app_files(ctx, app, directory): + app_info = app[ErlangAppInfo] + app_path = path_join(directory, app_info.app_name) + files = [] + for hdr in app_info.srcs + app_info.beam: + rp = additional_file_dest_relative_path(app.label, hdr) + dest = ctx.actions.declare_file(path_join(app_path, rp)) + ctx.actions.symlink(output = dest, target_file = hdr) + files.append(dest) + return files + +def _erlang_ls_tree(ctx): + apps = ctx.attr.apps + deps = [] + + for app in apps: + app_info = app[ErlangAppInfo] + for dep in app_info.deps: + # this puts non rabbitmq plugins, like amqp10_client into deps, + # but maybe those should be in apps? Does it matter? + if dep not in deps and dep not in apps: + deps.append(dep) + + files = [] + for app in apps: + files.extend( + _erlang_app_files(ctx, app, path_join(ctx.label.name, "apps")), + ) + for dep in deps: + files.extend( + _erlang_app_files(ctx, dep, path_join(ctx.label.name, "deps")), + ) + + return [ + DefaultInfo(files = depset(files)), + ] + +erlang_ls_tree = rule( + implementation = _erlang_ls_tree, + attrs = { + "apps": attr.label_list( + providers = [ErlangAppInfo], + ), + }, +)