| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  | load("@rules_erlang//:erlang_home.bzl", "ErlangHomeProvider", "ErlangVersionProvider") | 
					
						
							|  |  |  | load("@rules_erlang//:util.bzl", "path_join", "windows_path") | 
					
						
							|  |  |  | load("@rules_erlang//:ct.bzl", "sanitize_sname") | 
					
						
							| 
									
										
										
										
											2021-09-08 18:01:38 +08:00
										 |  |  | load(":rabbitmq_home.bzl", "RabbitmqHomeInfo", "rabbitmq_home_short_path") | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def _impl(ctx): | 
					
						
							| 
									
										
										
										
											2021-09-08 18:01:38 +08:00
										 |  |  |     rabbitmq_home_path = rabbitmq_home_short_path(ctx.attr.home) | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 19:50:22 +08:00
										 |  |  |     # the rabbitmq-run.sh template only allows a single erl_libs currently | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |     erl_libs = ctx.configuration.host_path_separator.join([ | 
					
						
							|  |  |  |         path_join(rabbitmq_home_path, "plugins"), | 
					
						
							|  |  |  |     ]) | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |     sname = sanitize_sname("sbb-" + ctx.attr.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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}": ctx.attr._erlang_home[ErlangHomeProvider].path, | 
					
						
							|  |  |  |                 "{SNAME}": sname, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             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(ctx.attr._erlang_home[ErlangHomeProvider].path), | 
					
						
							|  |  |  |                 "{SNAME}": sname, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             is_executable = True, | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 20:33:25 +08:00
										 |  |  |     runfiles = ctx.runfiles(ctx.attr.home[DefaultInfo].files.to_list()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |     return [DefaultInfo( | 
					
						
							|  |  |  |         runfiles = runfiles, | 
					
						
							|  |  |  |         executable = output, | 
					
						
							|  |  |  |     )] | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  | rabbitmq_run_private = rule( | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |     implementation = _impl, | 
					
						
							|  |  |  |     attrs = { | 
					
						
							|  |  |  |         "_template": attr.label( | 
					
						
							|  |  |  |             default = Label("//:scripts/bazel/rabbitmq-run.sh"), | 
					
						
							|  |  |  |             allow_single_file = True, | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |         "_windows_template": attr.label( | 
					
						
							|  |  |  |             default = Label("//:scripts/bazel/rabbitmq-run.bat"), | 
					
						
							|  |  |  |             allow_single_file = True, | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         "_erlang_home": attr.label(default = Label("@rules_erlang//:erlang_home")), | 
					
						
							|  |  |  |         "is_windows": attr.bool(mandatory = True), | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |         "home": attr.label(providers = [RabbitmqHomeInfo]), | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     executable = True, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  | def rabbitmq_run(**kwargs): | 
					
						
							|  |  |  |     rabbitmq_run_private( | 
					
						
							|  |  |  |         is_windows = select({ | 
					
						
							|  |  |  |             "@bazel_tools//src/conditions:host_windows": True, | 
					
						
							|  |  |  |             "//conditions:default": False, | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         **kwargs | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  | def _run_command_impl(ctx): | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |     if not ctx.attr.is_windows: | 
					
						
							|  |  |  |         output = ctx.actions.declare_file(ctx.label.name) | 
					
						
							|  |  |  |         script = "exec ./{} {} $@".format( | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |             ctx.attr.rabbitmq_run[DefaultInfo].files_to_run.executable.short_path, | 
					
						
							|  |  |  |             ctx.attr.subcommand, | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |     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, | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return [DefaultInfo( | 
					
						
							|  |  |  |         runfiles = ctx.attr.rabbitmq_run[DefaultInfo].default_runfiles, | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |         executable = output, | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |     )] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  | rabbitmq_run_command_private = rule( | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |     implementation = _run_command_impl, | 
					
						
							|  |  |  |     attrs = { | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  |         "is_windows": attr.bool(mandatory = True), | 
					
						
							| 
									
										
										
										
											2021-03-29 17:01:43 +08:00
										 |  |  |         "rabbitmq_run": attr.label( | 
					
						
							|  |  |  |             executable = True, | 
					
						
							|  |  |  |             cfg = "target", | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         "subcommand": attr.string(values = [ | 
					
						
							|  |  |  |             "run-broker", | 
					
						
							|  |  |  |             "start-background-broker", | 
					
						
							|  |  |  |             "stop-node", | 
					
						
							|  |  |  |         ]), | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     executable = True, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-01-06 03:57:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def rabbitmq_run_command(**kwargs): | 
					
						
							|  |  |  |     rabbitmq_run_command_private( | 
					
						
							|  |  |  |         is_windows = select({ | 
					
						
							|  |  |  |             "@bazel_tools//src/conditions:host_windows": True, | 
					
						
							|  |  |  |             "//conditions:default": False, | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         **kwargs | 
					
						
							|  |  |  |     ) |