Skip to content

Instantly share code, notes, and snippets.

@pcj
Last active September 19, 2018 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcj/7bdc4e2713e950e6d4215f03a90c8075 to your computer and use it in GitHub Desktop.
Save pcj/7bdc4e2713e950e6d4215f03a90c8075 to your computer and use it in GitHub Desktop.
Loading io_grpc_grpc_java and rules_closure together without initial conflict.
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "workspace_and_buildfile")
def _http_archive_impl(ctx):
"""Buildozer implementation of the http_archive rule."""
if not ctx.attr.url and not ctx.attr.urls:
fail("At least one of url and urls must be provided")
if ctx.attr.build_file and ctx.attr.build_file_content:
fail("Only one of build_file and build_file_content can be provided.")
all_urls = []
if ctx.attr.urls:
all_urls = ctx.attr.urls
if ctx.attr.url:
all_urls = [ctx.attr.url] + all_urls
ctx.download_and_extract(
all_urls,
"",
ctx.attr.sha256,
ctx.attr.type,
ctx.attr.strip_prefix,
)
ctx.download(
[ctx.attr.buildozer_linux_url],
output = "buildozer",
sha256 = ctx.attr.buildozer_linux_sha256,
executable = True,
)
if ctx.attr.label_list:
args = ["./buildozer", "-root_dir", ctx.path(".")]
args += ["replace deps %s %s" % (k, v) for k, v in ctx.attr.replace_deps.items()]
args += ctx.attr.label_list
result = ctx.execute(args, quiet = False)
if result.return_code:
fail("Buildozer failed: %s" % result.stderr)
if ctx.attr.sed_replacements:
sed = ctx.which("sed")
if not sed:
fail("sed utility not found")
# For each file (dict key) in the target list...
for filename, replacements in ctx.attr.sed_replacements.items():
# And each sed replacement to make (dict value)...
for replacement in replacements:
args = [sed, "--in-place", replacement, filename]
# execute the replace on that file.
result = ctx.execute(args, quiet = False)
if result.return_code:
fail("Buildozer failed: %s" % result.stderr)
workspace_and_buildfile(ctx)
_http_archive_attrs = {
"url": attr.string(),
"urls": attr.string_list(),
"sha256": attr.string(),
"strip_prefix": attr.string(),
"type": attr.string(),
"build_file": attr.label(allow_single_file = True),
"build_file_content": attr.string(),
"replace_deps": attr.string_dict(),
"sed_replacements": attr.string_list_dict(),
"label_list": attr.string_list(),
"workspace_file": attr.label(allow_single_file = True),
"workspace_file_content": attr.string(),
"buildozer_linux_url": attr.string(
default = "https://github.com/bazelbuild/buildtools/releases/download/0.15.0/buildozer",
),
"buildozer_linux_sha256": attr.string(
default = "be07a37307759c68696c989058b3446390dd6e8aa6fdca6f44f04ae3c37212c5",
),
}
buildozer_http_archive = repository_rule(
implementation = _http_archive_impl,
attrs = _http_archive_attrs,
)
"""
http_archive implementation that applies buildozer and sed replacements in the
downloaded archive.
Refer to documentation of the typical the http_archive rule in http.bzl. This
rule lacks the patch functionality of the original.
Following download and extraction of the archive, this rule will:
1. Execute a single buildozer command.
2. Execute a list of sed commands.
The buildozer command is constructed from the `replace_deps` and `label_list`
attributes. For each A -> B mapping in the replace_deps dict, a command like
'replace deps A B' will be appended. The list of labels to match are taken from
the label_list attribute. Refer to buildozer documentation for an explanation
of the replace deps command.
The sed commands are constructed from the `sed_replacements` attribute. These
sed commands might not be necessary if buildozer was capable of replacement
within *.bzl files, but currently it cannot. This attribute is a
string_list_dict, meaning the dict keys are filename to modify (in place), and
each dict value is are list of sed commands to apply onto that file. The value
typically looks something like 's|A|B|g'.
"""
http_archive(
name = "io_grpc_grpc_java",
urls = ["https://github.com/grpc/grpc-java/archive/v1.15.0.tar.gz"],
strip_prefix = "grpc-java-1.15.0",
sha256 = "8a131e773b1c9c0442e606b7fc85d7fc6739659281589d01bd917ceda218a1c7",
)
load("@//:buildozer_http_archive.bzl", "buildozer_http_archive")
RULES_CLOSURE_VERSION = "1e12aa5612d758daf2df339991c8d187223a7ee6"
RULES_CLOSURE_SHA256 = "663424d34fd067a8d066308eb2887fcaba36d73b354669ec1467498726a6b82c"
buildozer_http_archive(
name = "io_bazel_rules_closure",
urls = ["https://github.com/bazelbuild/rules_closure/archive/%s.tar.gz" % RULES_CLOSURE_VERSION],
sha256 = RULES_CLOSURE_SHA256,
strip_prefix = "rules_closure-" + RULES_CLOSURE_VERSION,
label_list = ["//...:%java_binary", "//...:%java_library"],
replace_deps = {
"@com_google_code_findbugs_jsr305": "@com_google_code_findbugs_jsr305_3_0_0",
"@com_google_errorprone_error_prone_annotations": "@com_google_errorprone_error_prone_annotations_2_1_3",
},
sed_replacements = {
"closure/repositories.bzl": [
"s|com_google_code_findbugs_jsr305|com_google_code_findbugs_jsr305_3_0_0|g",
"s|com_google_errorprone_error_prone_annotations|com_google_errorprone_error_prone_annotations_2_1_3|g",
],
},
)
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")
closure_repositories(
omit_com_google_protobuf = True,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment