def _switched_rules_impl(ctx):
disabled_rule_script = """
def {rule_name}(**kwargs):
pass
"""
enabled_native_rule_script = """
{rule_name} = {native_rule_name}
"""
enabled_rule_script = """
load("{file_label}", _{rule_name} = "{loaded_rule_name}")
"""
elabled_rule_scrip_alias = """
{rule_name} = _{rule_name}
"""
load_rules = [] rules = []
for rule_name, value_and_name in ctx.attr.rules.items():
value = value_and_name[0]
loaded_rule_name = value_and_name[1] if value_and_name[1] else rule_name
if not value:
rules.append(disabled_rule_script.format(rule_name = rule_name))
elif value.startswith("@"):
load_rules.append(enabled_rule_script.format(
file_label = value,
rule_name = rule_name,
loaded_rule_name = loaded_rule_name,
))
rules.append(elabled_rule_scrip_alias.format(rule_name = rule_name))
elif value.startswith("native."):
rules.append(
enabled_native_rule_script.format(rule_name = rule_name, native_rule_name = value),
)
else:
rules.append(value)
ctx.file("BUILD.bazel", "")
ctx.file("imports.bzl", "".join(load_rules + rules))
switched_rules = repository_rule(
implementation = _switched_rules_impl,
attrs = {
"rules": attr.string_list_dict(
allow_empty = True,
mandatory = False,
default = {},
),
},
)
def switched_rules_by_language(
name,
gapic = False,
grpc = False,
java = False,
go = False,
cc = False,
php = False,
nodejs = False,
python = False,
ruby = False,
csharp = False,
rules_override = {}):
rules = {}
rules["proto_library_with_info"] = _switch(
gapic,
"@com_google_api_codegen//rules_gapic:gapic.bzl",
)
rules["moved_proto_library"] = _switch(
gapic,
"@com_google_api_codegen//rules_gapic:gapic.bzl",
)
rules["java_proto_library"] = _switch(
java,
"native.java_proto_library",
)
rules["java_grpc_library"] = _switch(
java and grpc,
"@io_grpc_grpc_java//:java_grpc_library.bzl",
)
rules["java_gapic_library"] = _switch(
java and grpc and gapic,
"@com_google_api_codegen//rules_gapic/java:java_gapic.bzl",
)
rules["java_resource_name_proto_library"] = _switch(
java and grpc and gapic,
"@com_google_api_codegen//rules_gapic/java:java_gapic.bzl",
)
rules["java_gapic_test"] = _switch(
java and grpc and gapic,
"@com_google_api_codegen//rules_gapic/java:java_gapic.bzl",
)
rules["java_gapic_assembly_gradle_pkg"] = _switch(
java and grpc and gapic,
"@com_google_api_codegen//rules_gapic/java:java_gapic_pkg.bzl",
)
rules["py_proto_library"] = _switch(
python,
"@com_github_grpc_grpc//bazel:python_rules.bzl",
)
rules["py_grpc_library"] = _switch(
python and grpc,
"@com_github_grpc_grpc//bazel:python_rules.bzl",
)
rules["py_gapic_library"] = _switch(
python and grpc and gapic,
"@com_google_api_codegen//rules_gapic/python:py_gapic.bzl",
)
rules["py_gapic_assembly_pkg"] = _switch(
python and grpc and gapic,
"@com_google_api_codegen//rules_gapic/python:py_gapic_pkg.bzl",
)
rules["py_gapic_library2"] = _switch(
python and grpc and gapic,
"@gapic_generator_python//rules_python_gapic:py_gapic.bzl",
"py_gapic_library",
)
rules["py_gapic_assembly_pkg2"] = _switch(
python and grpc and gapic,
"@gapic_generator_python//rules_python_gapic:py_gapic_pkg.bzl",
"py_gapic_assembly_pkg",
)
rules["go_proto_library"] = _switch(
go,
"@io_bazel_rules_go//proto:def.bzl",
)
rules["go_library"] = _switch(
go,
"@io_bazel_rules_go//go:def.bzl",
)
rules["go_test"] = _switch(
go and grpc and gapic,
"@io_bazel_rules_go//go:def.bzl",
)
rules["go_gapic_library"] = _switch(
go and grpc and gapic,
"@com_googleapis_gapic_generator_go//rules_go_gapic:go_gapic.bzl",
)
rules["go_gapic_assembly_pkg"] = _switch(
go and grpc and gapic,
"@com_googleapis_gapic_generator_go//rules_go_gapic:go_gapic_pkg.bzl",
)
rules["cc_proto_library"] = _switch(
cc,
"native.cc_proto_library",
)
rules["cc_grpc_library"] = _switch(
cc and grpc,
"@com_github_grpc_grpc//bazel:cc_grpc_library.bzl",
)
rules["cc_gapic_library"] = _switch(False)
rules["php_proto_library"] = _switch(
php,
"@com_google_api_codegen//rules_gapic/php:php_gapic.bzl",
)
rules["php_grpc_library"] = _switch(
php and grpc,
"@com_google_api_codegen//rules_gapic/php:php_gapic.bzl",
)
rules["php_gapic_library"] = _switch(
php and grpc and gapic,
"@com_google_api_codegen//rules_gapic/php:php_gapic.bzl",
)
rules["php_gapic_assembly_pkg"] = _switch(
php and grpc and gapic,
"@com_google_api_codegen//rules_gapic/php:php_gapic_pkg.bzl",
)
rules["nodejs_gapic_library"] = _switch(
nodejs and grpc and gapic,
"@com_google_api_codegen//rules_gapic/nodejs:nodejs_gapic.bzl",
)
rules["nodejs_gapic_assembly_pkg"] = _switch(
nodejs and grpc and gapic,
"@com_google_api_codegen//rules_gapic/nodejs:nodejs_gapic_pkg.bzl",
)
rules["ruby_proto_library"] = _switch(
ruby,
"@com_google_api_codegen//rules_gapic/ruby:ruby_gapic.bzl",
)
rules["ruby_grpc_library"] = _switch(
ruby and grpc,
"@com_google_api_codegen//rules_gapic/ruby:ruby_gapic.bzl",
)
rules["ruby_gapic_library"] = _switch(
ruby and grpc and gapic,
"@com_google_api_codegen//rules_gapic/ruby:ruby_gapic.bzl",
)
rules["ruby_gapic_assembly_pkg"] = _switch(
ruby and grpc and gapic,
"@com_google_api_codegen//rules_gapic/ruby:ruby_gapic_pkg.bzl",
)
rules["csharp_proto_library"] = _switch(
csharp,
"@com_google_api_codegen//rules_gapic/csharp:csharp_gapic.bzl",
)
rules["csharp_grpc_library"] = _switch(
csharp and grpc,
"@com_google_api_codegen//rules_gapic/csharp:csharp_gapic.bzl",
)
rules["csharp_gapic_library"] = _switch(
csharp and grpc and gapic,
"@com_google_api_codegen//rules_gapic/csharp:csharp_gapic.bzl",
)
rules["csharp_gapic_assembly_pkg"] = _switch(
csharp and grpc and gapic,
"@com_google_api_codegen//rules_gapic/csharp:csharp_gapic_pkg.bzl",
)
rules.update(rules_override)
switched_rules(
name = name,
rules = rules,
)
def _switch(enabled, enabled_value = "", actual_name = ""):
if enabled:
return [enabled_value, actual_name]
else:
return ["", actual_name]