forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.bzl
127 lines (109 loc) · 4.79 KB
/
common.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
load("//java/private:module.bzl", "JavaModuleInfo")
MavenInfo = provider(
fields = {
"coordinates": "Maven coordinates of the library we're building (optional)",
"artifact_jars": "Jars to include within the artifact when we build it",
"source_jars": "Source jars to include within the maven source jar",
"maven_deps": "Maven coordinates that this module depends on",
"transitive_maven_deps": "All maven coordinates that are transitive dependencies",
"transitive_runtime_jars": "Transitive set of jars that this library depends on (including artifact_jars)",
"transitive_source_jars": "Transitive set of source jars that this library depends on",
},
)
MAVEN_PREFIX = "maven_coordinates="
def _has_maven_deps_impl(target, ctx):
java_info = target[JavaInfo]
tags = getattr(ctx.rule.attr, "tags", [])
deps = getattr(ctx.rule.attr, "deps", [])
exports = getattr(ctx.rule.attr, "exports", [])
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])
all_deps = deps + exports + rt_deps
if "maven:compile_only" in tags:
return MavenInfo(
coordinates = None,
maven_deps = depset(),
artifact_jars = depset(),
source_jars = depset(),
transitive_maven_deps = depset(),
transitive_runtime_jars = depset(),
)
# Find all the deps that have coordinates
all_infos = [dep[MavenInfo] for dep in all_deps if MavenInfo in dep]
maven_deps = depset([], transitive = [info.transitive_maven_deps for info in all_infos])
# Because of the way that maven works, if a rule has maven coordinates,
# it's enough to set set the transitive deps to just be the rule for
# anything that depends upon it. Otherwise, gather them up, and carry on
# as if nothing really mattered.
coordinate = read_coordinates(tags)
if coordinate:
transitive_maven_deps = depset([coordinate])
else:
transitive_maven_deps = depset(transitive = [info.transitive_maven_deps for info in all_infos])
artifact_jars = depset(java_info.runtime_output_jars, transitive = [info.artifact_jars for info in all_infos if not info.coordinates])
source_jars = depset(java_info.source_jars, transitive = [info.source_jars for info in all_infos if not info.coordinates])
infos = []
if JavaModuleInfo in target:
info = MavenInfo(
coordinates = coordinate,
maven_deps = maven_deps,
artifact_jars = depset(target[JavaInfo].runtime_output_jars),
source_jars = depset(target[JavaInfo].source_jars),
transitive_maven_deps = transitive_maven_deps,
transitive_runtime_jars = depset(target[JavaInfo].transitive_runtime_jars.to_list(), transitive = [info.transitive_runtime_jars for info in all_infos]),
)
else:
info = MavenInfo(
coordinates = coordinate,
maven_deps = maven_deps,
artifact_jars = artifact_jars,
source_jars = source_jars,
transitive_maven_deps = transitive_maven_deps,
transitive_runtime_jars = depset(target[JavaInfo].transitive_runtime_jars.to_list(), transitive = [info.transitive_runtime_jars for info in all_infos]),
)
infos.append(info)
return infos
has_maven_deps = aspect(
implementation = _has_maven_deps_impl,
attr_aspects = [
"deps",
"exports",
"runtime_deps",
],
)
def combine_jars(ctx, singlejar, inputs, output):
args = ctx.actions.args()
args.add("--output", output)
args.add_all(inputs, before_each = "--sources")
ctx.actions.run(
mnemonic = "BuildMavenJar",
inputs = inputs,
outputs = [output],
executable = singlejar,
arguments = [args],
)
def explode_coordinates(coords):
"""Takes a maven coordinate and explodes it into a tuple of
(groupId, artifactId, version, type, classifier)
"""
if not coords:
return None
parts = coords.split(":")
if len(parts) == 3:
return (parts[0], parts[1], parts[2], "jar", "jar")
if len(parts) == 4:
# Assume a buildr coordinate: groupId:artifactId:type:version
return (parts[0], parts[1], parts[3], parts[2], "jar")
if len(parts) == 5:
# Assume groupId:artifactId:type:classifier:version
return (parts[0], parts[1], parts[4], parts[2], parts[3])
fail("Unparsed: %s -> %s" % (coords, parts))
def read_coordinates(tags):
coordinates = []
for tag in tags:
if tag == "maven:compile_only":
return []
if tag.startswith(MAVEN_PREFIX):
coordinates.append(tag[len(MAVEN_PREFIX):])
if len(coordinates) > 1:
fail("Zero or one set of coordinates should be defined: %s" % coordinates)
return coordinates[0] if len(coordinates) else None