-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathjarjar.bzl
91 lines (83 loc) · 3 KB
/
jarjar.bzl
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
# Copyright (C) 2018 The Bazel Common Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Skylark rules for jarjar. See https://github.com/pantsbuild/jarjar
"""
load("@rules_java//java:defs.bzl", "java_common")
def _jarjar_library(ctx):
ctx.actions.write(
output = ctx.outputs._rules_file,
content = "\n".join(ctx.attr.rules),
)
jar_files = depset(transitive = [jar.files for jar in ctx.attr.jars]).to_list()
command = """
export JAVA_HOME="{java_home}"
export MERGE_META_INF_FILES="{merge_meta_inf_files}"
export JARJAR="{jarjar}"
export RULES_FILE="{rules_file}"
export OUTFILE="{outfile}"
"{jarjar_runner}" {jars}
""".format(
java_home = str(ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home),
merge_meta_inf_files = " ".join(ctx.attr.merge_meta_inf_files),
jarjar = ctx.executable._jarjar.path,
rules_file = ctx.outputs._rules_file.path,
outfile = ctx.outputs.jar.path,
jarjar_runner = ctx.executable._jarjar_runner.path,
jars = " ".join([jar.path for jar in jar_files]),
)
ctx.actions.run_shell(
command = command,
inputs = [ctx.outputs._rules_file] + jar_files + ctx.files._jdk,
outputs = [ctx.outputs.jar],
tools = [ctx.executable._jarjar, ctx.executable._jarjar_runner],
)
_jarjar_library_attrs = {
"rules": attr.string_list(),
"jars": attr.label_list(
allow_files = [".jar"],
),
"merge_meta_inf_files": attr.string_list(
allow_empty = True,
default = [],
mandatory = False,
doc = """A list of regular expressions that match files relative to the
META-INF directory that will be merged into the output jar, in addition
to files in META-INF/services. To add all files in META-INF/foo, for
example, use "foo/.*".""",
),
}
_jarjar_library_attrs.update({
"_jarjar": attr.label(
default = Label("//tools/jarjar"),
executable = True,
cfg = "exec",
),
"_jarjar_runner": attr.label(
default = Label("//tools/jarjar:jarjar_runner"),
executable = True,
cfg = "exec",
),
"_jdk": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
providers = [java_common.JavaRuntimeInfo],
),
})
jarjar_library = rule(
attrs = _jarjar_library_attrs,
outputs = {
"jar": "%{name}.jar",
"_rules_file": "%{name}.jarjar_rules",
},
implementation = _jarjar_library,
)