From 9aedb36061a078089dbc88275ddc00b78f4983c8 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 5 Apr 2023 09:58:54 -0700 Subject: [PATCH] refactor: inline some rules_docker 'skylib' helpers Note: I think it might be better to introduce a dep on aspect_bazel_lib instead, which is a very-well maintained utility library that's a superset of bazel_skylib, and evidently rules_docker wished for such a thing as well since it had this folder. Baby steps towards #723 I'd like to start small and gauge interest from rules_k8s maintainers before spending too much time here. --- k8s/object.bzl | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/k8s/object.bzl b/k8s/object.bzl index 92fa580c..5ff08ef8 100644 --- a/k8s/object.bzl +++ b/k8s/object.bzl @@ -18,14 +18,34 @@ load( _get_layers = "get_from_target", _layer_tools = "tools", ) -load( - "@io_bazel_rules_docker//skylib:label.bzl", - _string_to_label = "string_to_label", -) -load( - "@io_bazel_rules_docker//skylib:path.bzl", - _get_runfile_path = "runfile", -) + +# Note: could use https://docs.aspect.build/rules/aspect_bazel_lib/docs/paths#to_rlocation_path +# if we had a dependency on aspect_bazel_lib +def _get_runfile_path(ctx, f): + """Return the runfiles relative path of f.""" + if ctx.workspace_name: + return ctx.workspace_name + "/" + f.short_path + else: + return f.short_path + +# Note: could use https://docs.aspect.build/rules/aspect_bazel_lib/docs/utils#to_label +# if we had a dependency on aspect_bazel_lib +def _string_to_label(label_list, string_list): + """Return a mapping from label strings to the resolved label. + + Args: + label_list: The list of labels + string_list: The list of strings + + Returns: + A mapping from label strings to the resolved label. + """ + label_string_dict = dict() + for i in range(len(label_list)): + string = string_list[i] + label = label_list[i] + label_string_dict[string] = label + return label_string_dict def _runfiles(ctx, f): return "${RUNFILES}/%s" % _get_runfile_path(ctx, f) @@ -33,7 +53,7 @@ def _runfiles(ctx, f): def _deduplicate(iterable): """Performs a deduplication (similar to `list(set(...))`) - This is necessary because `set` is not available in Skylark. + This is necessary because `set` is not available in Starlark. """ return {k: None for k in iterable}.keys() @@ -124,7 +144,7 @@ def _impl(ctx): }, ).to_json(), ) - all_inputs += [substitutions_file] + all_inputs.append(substitutions_file) ctx.actions.expand_template( template = ctx.file._template,