|
1 | 1 | load(
|
2 |
| - "@rules_erlang//tools:erlang_toolchain.bzl", |
3 |
| - "erlang_dirs", |
| 2 | + "@rules_erlang//:erlang_app_info.bzl", |
| 3 | + "ErlangAppInfo", |
| 4 | +) |
| 5 | +load( |
| 6 | + "@rules_erlang//:util.bzl", |
| 7 | + "path_join", |
| 8 | +) |
| 9 | +load( |
| 10 | + "@rules_erlang//private:util.bzl", |
| 11 | + "additional_file_dest_relative_path", |
4 | 12 | )
|
5 | 13 |
|
6 |
| -def _impl(ctx): |
7 |
| - out = ctx.actions.declare_file(ctx.label.name) |
8 |
| - |
9 |
| - (erlang_home, _, _) = erlang_dirs(ctx) |
| 14 | +def _erlang_ls_config(ctx): |
| 15 | + runtime_prefix = path_join( |
| 16 | + ctx.bin_dir.path, |
| 17 | + ctx.label.package, |
| 18 | + ctx.label.name + ".runfiles", |
| 19 | + ctx.workspace_name, |
| 20 | + ) |
10 | 21 |
|
11 | 22 | ctx.actions.write(
|
12 |
| - output = out, |
13 |
| - content = """otp_path: {erlang_home} |
| 23 | + output = ctx.outputs.executable, |
| 24 | + content = """#!/usr/bin/env bash |
| 25 | +
|
| 26 | +set -euo pipefail |
| 27 | +
|
| 28 | +BAZEL_OUT_ABSOLUTE_PATH="${{PWD%/{}}}/bazel-out" |
| 29 | +
|
| 30 | +cat << EOF |
14 | 31 | apps_dirs:
|
15 |
| - - deps/* |
16 |
| - - deps/rabbit/apps/* |
| 32 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/apps/* |
17 | 33 | deps_dirs:
|
18 |
| - - bazel-bin/external/* |
| 34 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/deps/* |
19 | 35 | include_dirs:
|
20 |
| - - deps |
21 |
| - - deps/* |
22 |
| - - deps/*/include |
23 |
| - - deps/*/src |
24 |
| - - bazel-bin/external |
25 |
| - - bazel-bin/external/*/include |
26 |
| -plt_path: bazel-bin/deps/rabbit/.base_plt.plt |
27 |
| -""".format( |
28 |
| - erlang_home = erlang_home, |
29 |
| - ), |
| 36 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/apps |
| 37 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/apps/*/include |
| 38 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/deps |
| 39 | +- ${{BAZEL_OUT_ABSOLUTE_PATH}}/*/bin/tools/erlang_ls_files/deps/*/include |
| 40 | +EOF |
| 41 | +""".format(runtime_prefix), |
30 | 42 | )
|
31 | 43 |
|
| 44 | +erlang_ls_config = rule( |
| 45 | + implementation = _erlang_ls_config, |
| 46 | + executable = True, |
| 47 | +) |
| 48 | + |
| 49 | +def _erlang_app_files(ctx, app, directory): |
| 50 | + app_info = app[ErlangAppInfo] |
| 51 | + app_path = path_join(directory, app_info.app_name) |
| 52 | + files = [] |
| 53 | + for f in app_info.srcs + app_info.beam: |
| 54 | + relative_path = additional_file_dest_relative_path(app.label, f) |
| 55 | + dest = ctx.actions.declare_file(path_join(app_path, relative_path)) |
| 56 | + ctx.actions.symlink(output = dest, target_file = f) |
| 57 | + files.append(dest) |
| 58 | + return files |
| 59 | + |
| 60 | +def _erlang_ls_tree(ctx): |
| 61 | + apps = ctx.attr.apps |
| 62 | + deps = [] |
| 63 | + |
| 64 | + for app in apps: |
| 65 | + app_info = app[ErlangAppInfo] |
| 66 | + for dep in app_info.deps: |
| 67 | + # this puts non rabbitmq plugins, like amqp10_client into deps, |
| 68 | + # but maybe those should be in apps? Does it matter? |
| 69 | + if dep not in deps and dep not in apps: |
| 70 | + deps.append(dep) |
| 71 | + |
| 72 | + files = [] |
| 73 | + for app in apps: |
| 74 | + files.extend( |
| 75 | + _erlang_app_files(ctx, app, path_join(ctx.label.name, "apps")), |
| 76 | + ) |
| 77 | + for dep in deps: |
| 78 | + files.extend( |
| 79 | + _erlang_app_files(ctx, dep, path_join(ctx.label.name, "deps")), |
| 80 | + ) |
| 81 | + |
32 | 82 | return [
|
33 |
| - DefaultInfo(files = depset([out])), |
| 83 | + DefaultInfo(files = depset(files)), |
34 | 84 | ]
|
35 | 85 |
|
36 |
| -erlang_ls_config = rule( |
37 |
| - implementation = _impl, |
38 |
| - toolchains = [ |
39 |
| - "@rules_erlang//tools:toolchain_type", |
40 |
| - ], |
| 86 | +erlang_ls_tree = rule( |
| 87 | + implementation = _erlang_ls_tree, |
| 88 | + attrs = { |
| 89 | + "apps": attr.label_list( |
| 90 | + providers = [ErlangAppInfo], |
| 91 | + ), |
| 92 | + }, |
41 | 93 | )
|
0 commit comments