summaryrefslogtreecommitdiff
path: root/rabbitmq_home.bzl
blob: 0af142dfd6303745f06faa7b2010f923aa5eef99 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
load("@rules_erlang//:erlang_app_info.bzl", "ErlangAppInfo", "flat_deps")
load("@rules_erlang//:util.bzl", "path_join")
load("@rules_erlang//:ct.bzl", "additional_file_dest_relative_path")

RabbitmqHomeInfo = provider(
    doc = "An assembled RABBITMQ_HOME dir",
    fields = {
        "rabbitmqctl": "rabbitmqctl script from the sbin directory",
    },
)

def _copy_script(ctx, script):
    dest = ctx.actions.declare_file(
        path_join(ctx.label.name, "sbin", script.basename),
    )
    ctx.actions.expand_template(
        template = script,
        output = dest,
        substitutions = {},
        is_executable = True,
    )
    return dest

def copy_escript(ctx, escript):
    e = ctx.attr._rabbitmqctl_escript.files_to_run.executable
    dest = ctx.actions.declare_file(
        path_join(ctx.label.name, "escript", escript.basename),
    )
    ctx.actions.run(
        inputs = [e],
        outputs = [dest],
        executable = "cp",
        arguments = [e.path, dest.path],
    )
    return dest

def _plugins_dir_links(ctx, plugin):
    lib_info = plugin[ErlangAppInfo]
    plugin_path = path_join(
        ctx.label.name,
        "plugins",
        lib_info.app_name,
    )

    links = []
    for f in lib_info.include:
        o = ctx.actions.declare_file(path_join(plugin_path, "include", f.basename))
        ctx.actions.symlink(
            output = o,
            target_file = f,
        )
        links.append(o)

    for f in lib_info.beam:
        if f.is_directory:
            if f.basename != "ebin":
                fail("{} contains a directory in 'beam' that is not an ebin dir".format(lib_info.lib_name))
            o = ctx.actions.declare_directory(path_join(plugin_path, "ebin"))
        else:
            o = ctx.actions.declare_file(path_join(plugin_path, "ebin", f.basename))
        ctx.actions.symlink(
            output = o,
            target_file = f,
        )
        links.append(o)

    for f in lib_info.priv:
        p = additional_file_dest_relative_path(plugin.label, f)
        o = ctx.actions.declare_file(path_join(plugin_path, p))
        ctx.actions.symlink(
            output = o,
            target_file = f,
        )
        links.append(o)

    return links

def flatten(list_of_lists):
    return [item for sublist in list_of_lists for item in sublist]

def _impl(ctx):
    plugins = flat_deps(ctx.attr.plugins)

    if not ctx.attr.is_windows:
        source_scripts = ctx.files._scripts
    else:
        source_scripts = ctx.files._scripts_windows
    scripts = [_copy_script(ctx, script) for script in source_scripts]

    escripts = [copy_escript(ctx, escript) for escript in ctx.files._escripts]

    plugins = flatten([_plugins_dir_links(ctx, plugin) for plugin in plugins])

    rabbitmqctl = None
    for script in scripts:
        if script.basename == ("rabbitmqctl" if not ctx.attr.is_windows else "rabbitmqctl.bat"):
            rabbitmqctl = script
    if rabbitmqctl == None:
        fail("could not find rabbitmqctl among", scripts)

    return [
        RabbitmqHomeInfo(
            rabbitmqctl = rabbitmqctl,
        ),
        DefaultInfo(
            files = depset(scripts + escripts + plugins),
        ),
    ]

RABBITMQ_HOME_ATTRS = {
    "_escripts": attr.label_list(
        default = [
            "//deps/rabbit:scripts/rabbitmq-diagnostics",
            "//deps/rabbit:scripts/rabbitmq-plugins",
            "//deps/rabbit:scripts/rabbitmq-queues",
            "//deps/rabbit:scripts/rabbitmq-streams",
            "//deps/rabbit:scripts/rabbitmq-upgrade",
            "//deps/rabbit:scripts/rabbitmqctl",
            "//deps/rabbit:scripts/vmware-rabbitmq",
        ],
        allow_files = True,
    ),
    "_scripts": attr.label_list(
        default = [
            "//deps/rabbit:scripts/rabbitmq-defaults",
            "//deps/rabbit:scripts/rabbitmq-diagnostics",
            "//deps/rabbit:scripts/rabbitmq-env",
            "//deps/rabbit:scripts/rabbitmq-plugins",
            "//deps/rabbit:scripts/rabbitmq-queues",
            "//deps/rabbit:scripts/rabbitmq-server",
            "//deps/rabbit:scripts/rabbitmq-streams",
            "//deps/rabbit:scripts/rabbitmq-upgrade",
            "//deps/rabbit:scripts/rabbitmqctl",
            "//deps/rabbit:scripts/vmware-rabbitmq",
        ],
        allow_files = True,
    ),
    "_scripts_windows": attr.label_list(
        default = [
            "//deps/rabbit:scripts/rabbitmq-defaults.bat",
            "//deps/rabbit:scripts/rabbitmq-diagnostics.bat",
            "//deps/rabbit:scripts/rabbitmq-env.bat",
            "//deps/rabbit:scripts/rabbitmq-plugins.bat",
            "//deps/rabbit:scripts/rabbitmq-queues.bat",
            "//deps/rabbit:scripts/rabbitmq-server.bat",
            "//deps/rabbit:scripts/rabbitmq-streams.bat",
            "//deps/rabbit:scripts/rabbitmq-upgrade.bat",
            "//deps/rabbit:scripts/rabbitmqctl.bat",
            "//deps/rabbit:scripts/vmware-rabbitmq.bat",
        ],
        allow_files = True,
    ),
    "_rabbitmqctl_escript": attr.label(default = "//deps/rabbitmq_cli:rabbitmqctl"),
    "is_windows": attr.bool(mandatory = True),
    "plugins": attr.label_list(providers = [ErlangAppInfo]),
}

rabbitmq_home_private = rule(
    implementation = _impl,
    attrs = RABBITMQ_HOME_ATTRS,
)

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

def _dirname(p):
    return p.rpartition("/")[0]

def rabbitmq_home_short_path(rabbitmq_home):
    short_path = rabbitmq_home[RabbitmqHomeInfo].rabbitmqctl.short_path
    if rabbitmq_home.label.workspace_root != "":
        short_path = path_join(rabbitmq_home.label.workspace_root, short_path)
    return _dirname(_dirname(short_path))