github.com/bazelbuild/rules_webtesting@v0.2.0/web/internal/java_import_external.bzl (about) 1 # Copyright 2016 The Closure Rules Authors. All rights reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 _PASS_PROPS = ( 16 "neverlink", 17 "testonly_", 18 "visibility", 19 "exports", 20 "runtime_deps", 21 "deps", 22 ) 23 24 def _java_import_external(repository_ctx): 25 """Downloads jar and creates a java_import rule.""" 26 if (repository_ctx.attr.generated_linkable_rule_name and 27 not repository_ctx.attr.neverlink): 28 fail("Only use generated_linkable_rule_name if neverlink is set") 29 name = repository_ctx.attr.generated_rule_name or repository_ctx.name 30 urls = repository_ctx.attr.jar_urls 31 sha = repository_ctx.attr.jar_sha256 32 path = repository_ctx.name + ".jar" 33 for url in urls: 34 if url.endswith(".jar"): 35 path = url[url.rindex("/") + 1:] 36 break 37 srcurls = repository_ctx.attr.srcjar_urls 38 srcsha = repository_ctx.attr.srcjar_sha256 39 srcpath = repository_ctx.name + "-src.jar" if srcurls else "" 40 for url in srcurls: 41 if url.endswith(".jar"): 42 srcpath = url[url.rindex("/") + 1:].replace("-sources.jar", "-src.jar") 43 break 44 lines = ["# DO NOT EDIT: generated by java_import_external()", ""] 45 if repository_ctx.attr.default_visibility: 46 lines.append("package(default_visibility = %s)" % 47 (repository_ctx.attr.default_visibility)) 48 lines.append("") 49 lines.append("licenses(%s)" % repr(repository_ctx.attr.licenses)) 50 lines.append("") 51 lines.extend( 52 _make_java_import(name, path, srcpath, repository_ctx.attr, _PASS_PROPS), 53 ) 54 if (repository_ctx.attr.neverlink and 55 repository_ctx.attr.generated_linkable_rule_name): 56 lines.extend( 57 _make_java_import( 58 repository_ctx.attr.generated_linkable_rule_name, 59 path, 60 srcpath, 61 repository_ctx.attr, 62 [p for p in _PASS_PROPS if p != "neverlink"], 63 ), 64 ) 65 extra = repository_ctx.attr.extra_build_file_content 66 if extra: 67 lines.append(extra) 68 if not extra.endswith("\n"): 69 lines.append("") 70 repository_ctx.download(urls, path, sha) 71 if srcurls: 72 repository_ctx.download(srcurls, srcpath, srcsha) 73 repository_ctx.file("BUILD", "\n".join(lines)) 74 75 def _make_java_import(name, path, srcpath, attrs, props): 76 lines = [ 77 "java_import(", 78 " name = %s," % repr(name), 79 " jars = [%s]," % repr(path), 80 ] 81 if srcpath: 82 lines.append(" srcjar = %s," % repr(srcpath)) 83 for prop in props: 84 value = getattr(attrs, prop, None) 85 if value: 86 if prop.endswith("_"): 87 prop = prop[:-1] 88 lines.append(" %s = %s," % (prop, repr(value))) 89 lines.append(")") 90 lines.append("") 91 return lines 92 93 java_import_external = repository_rule( 94 attrs = { 95 "licenses": attr.string_list( 96 mandatory = True, 97 allow_empty = False, 98 ), 99 "jar_urls": attr.string_list( 100 mandatory = True, 101 allow_empty = False, 102 ), 103 "jar_sha256": attr.string(mandatory = True), 104 "srcjar_urls": attr.string_list(), 105 "srcjar_sha256": attr.string(), 106 "deps": attr.string_list(), 107 "runtime_deps": attr.string_list(), 108 "testonly_": attr.bool(), 109 "exports": attr.string_list(), 110 "neverlink": attr.bool(), 111 "generated_rule_name": attr.string(), 112 "generated_linkable_rule_name": attr.string(), 113 "default_visibility": attr.string_list(default = ["//visibility:public"]), 114 "extra_build_file_content": attr.string(), 115 }, 116 implementation = _java_import_external, 117 )