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
diff --git a/java/BUILD b/java/BUILD index a42b374..bf9da91 100644 --- a/java/BUILD +++ b/java/BUILD
@@ -18,6 +18,12 @@ ) bzl_library( + name = "utils", + srcs = ["java_utils.bzl"], + visibility = ["//visibility:public"], +) + +bzl_library( name = "java_single_jar", srcs = ["java_single_jar.bzl"], visibility = ["//visibility:public"],
diff --git a/java/java_utils.bzl b/java/java_utils.bzl new file mode 100644 index 0000000..a90a001 --- /dev/null +++ b/java/java_utils.bzl
@@ -0,0 +1,25 @@ +"""Utility methods for interacting with the java rules""" + +def _tokenize_javacopts(ctx, opts): + """Tokenizes a list or depset of options to a list. + + Iff opts is a depset, we reverse the flattened list to ensure right-most + duplicates are preserved in their correct position. + + Args: + ctx: (RuleContext) the rule context + opts: (depset[str]|[str]) the javac options to tokenize + Returns: + [str] list of tokenized options + """ + if hasattr(opts, "to_list"): + opts = reversed(opts.to_list()) + return [ + token + for opt in opts + for token in ctx.tokenize(opt) + ] + +utils = struct( + tokenize_javacopts = _tokenize_javacopts, +)