summaryrefslogtreecommitdiff
path: root/rabbitmq_run.bzl
blob: b2e5debae1e97e08abd017b80fa0faa1e6857c64 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
load(
    "@rules_erlang//:util.bzl",
    "path_join",
    "windows_path",
)
load(
    "@rules_erlang//tools:erlang_toolchain.bzl",
    "erlang_dirs",
)
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 = ctx.configuration.host_path_separator.join([
        path_join(rabbitmq_home_path, "plugins"),
    ])

    (erlang_home, _, runfiles) = erlang_dirs(ctx)

    if not ctx.attr.is_windows:
        output = ctx.actions.declare_file(ctx.label.name)
        ctx.actions.expand_template(
            template = ctx.file._template,
            output = output,
            substitutions = {
                "{RABBITMQ_HOME}": rabbitmq_home_path,
                "{ERL_LIBS}": erl_libs,
                "{ERLANG_HOME}": erlang_home,
            },
            is_executable = True,
        )
    else:
        output = ctx.actions.declare_file(ctx.label.name + ".bat")
        ctx.actions.expand_template(
            template = ctx.file._windows_template,
            output = output,
            substitutions = {
                "{RABBITMQ_HOME}": windows_path(rabbitmq_home_path),
                "{ERL_LIBS}": erl_libs,
                "{ERLANG_HOME}": windows_path(erlang_home),
            },
            is_executable = True,
        )

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

    return [DefaultInfo(
        runfiles = runfiles,
        executable = output,
    )]

rabbitmq_run_private = rule(
    implementation = _impl,
    attrs = {
        "_template": attr.label(
            default = Label("//:scripts/bazel/rabbitmq-run.sh"),
            allow_single_file = True,
        ),
        "_windows_template": attr.label(
            default = Label("//:scripts/bazel/rabbitmq-run.bat"),
            allow_single_file = True,
        ),
        "is_windows": attr.bool(mandatory = True),
        "home": attr.label(providers = [RabbitmqHomeInfo]),
    },
    toolchains = ["@rules_erlang//tools:toolchain_type"],
    executable = True,
)

def rabbitmq_run(**kwargs):
    rabbitmq_run_private(
        is_windows = select({
            "@bazel_tools//src/conditions:host_windows": True,
            "//conditions:default": False,
        }),
        **kwargs
    )

def _run_command_impl(ctx):
    if not ctx.attr.is_windows:
        output = ctx.actions.declare_file(ctx.label.name)
        script = "exec ./{} {} $@".format(
            ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
            ctx.attr.subcommand,
        )
    else:
        output = ctx.actions.declare_file(ctx.label.name + ".bat")
        script = """@echo off
call {} {} %*
if ERRORLEVEL 1 (
    exit /B %ERRORLEVEL%
)
EXIT /B 0
""".format(
            ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path,
            ctx.attr.subcommand,
        )

    ctx.actions.write(
        output = output,
        content = script,
        is_executable = True,
    )

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

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

def rabbitmq_run_command(**kwargs):
    rabbitmq_run_command_private(
        is_windows = select({
            "@bazel_tools//src/conditions:host_windows": True,
            "//conditions:default": False,
        }),
        **kwargs
    )