Skip to content

Commit ebbf67e

Browse files
hvadehracopybara-github
authored andcommitted
Add a java_utils.bzl to rules_java
For now, this only exposes a method to tokenize javacopts. This will be useful for both internal and OSS projects once we change the type of `JavaInfo.compilation_info.javac_opts` to a `depset`. PiperOrigin-RevId: 582301665 Change-Id: I717725ecde348cac73b6ab3050c7712b2261685b
1 parent 82e7f35 commit ebbf67e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

java/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ bzl_library(
1717
deps = ["//java/private"],
1818
)
1919

20+
bzl_library(
21+
name = "utils",
22+
srcs = ["java_utils.bzl"],
23+
visibility = ["//visibility:public"],
24+
)
25+
2026
bzl_library(
2127
name = "java_single_jar",
2228
srcs = ["java_single_jar.bzl"],

java/java_utils.bzl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Utility methods for interacting with the java rules"""
2+
3+
def _tokenize_javacopts(ctx, opts):
4+
"""Tokenizes a list or depset of options to a list.
5+
6+
Iff opts is a depset, we reverse the flattened list to ensure right-most
7+
duplicates are preserved in their correct position.
8+
9+
Args:
10+
ctx: (RuleContext) the rule context
11+
opts: (depset[str]|[str]) the javac options to tokenize
12+
Returns:
13+
[str] list of tokenized options
14+
"""
15+
if hasattr(opts, "to_list"):
16+
opts = reversed(opts.to_list())
17+
return [
18+
token
19+
for opt in opts
20+
for token in ctx.tokenize(opt)
21+
]
22+
23+
utils = struct(
24+
tokenize_javacopts = _tokenize_javacopts,
25+
)

0 commit comments

Comments
 (0)