summaryrefslogtreecommitdiff
path: root/rabbitmq_run.bzl
blob: e690451e132d5ea924bbb41e160748a75f0bef6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
load("@bazel-erlang//:erlang_home.bzl", "ErlangHomeProvider", "ErlangVersionProvider")
load("@bazel-erlang//:bazel_erlang_lib.bzl", "path_join")
load("@bazel-erlang//:ct.bzl", "sanitize_sname")
load(":rabbitmq_home.bzl", "RabbitmqHomeInfo", "rabbitmq_home_short_path")

def _impl(ctx):
    rabbitmq_home_path = rabbitmq_home_short_path(ctx.attr.home)

    # the rabbitmq-run.sh template only allows a single erl_libs currently
    erl_libs = [path_join(rabbitmq_home_path, "plugins")]

    ctx.actions.expand_template(
        template = ctx.file._template,
        output = ctx.outputs.executable,
        substitutions = {
            "{RABBITMQ_HOME}": rabbitmq_home_path,
            "{ERL_LIBS}": ":".join(erl_libs),
            "{ERLANG_HOME}": ctx.attr._erlang_home[ErlangHomeProvider].path,
            "{SNAME}": sanitize_sname("sbb-" + ctx.attr.name),
        },
        is_executable = True,
    )

    runfiles = ctx.runfiles(ctx.attr.home[DefaultInfo].files.to_list())

    return [DefaultInfo(runfiles = runfiles)]

rabbitmq_run = rule(
    implementation = _impl,
    attrs = {
        "_template": attr.label(
            default = Label("//:scripts/bazel/rabbitmq-run.sh"),
            allow_single_file = True,
        ),
        "_erlang_home": attr.label(default = "@bazel-erlang//:erlang_home"),
        "home": attr.label(providers = [RabbitmqHomeInfo]),
    },
    executable = True,
)

def _run_command_impl(ctx):
    ctx.actions.write(
        output = ctx.outputs.executable,
        content = "exec ./{} {} $@".format(
            ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
            ctx.attr.subcommand,
        ),
    )

    return [DefaultInfo(
        runfiles = ctx.attr.rabbitmq_run[DefaultInfo].default_runfiles,
    )]

rabbitmq_run_command = rule(
    implementation = _run_command_impl,
    attrs = {
        "rabbitmq_run": attr.label(
            executable = True,
            cfg = "target",
        ),
        "subcommand": attr.string(values = [
            "run-broker",
            "start-background-broker",
            "stop-node",
        ]),
    },
    executable = True,
)