Skip to content

Commit

Permalink
refactor: inline some rules_docker 'skylib' helpers
Browse files Browse the repository at this point in the history
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 bazelbuild#723
I'd like to start small and gauge interest from rules_k8s maintainers before spending too much time here.
  • Loading branch information
alexeagle committed Apr 5, 2023
1 parent 75c78d0 commit 9aedb36
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions k8s/object.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,42 @@ 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)

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()

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9aedb36

Please sign in to comment.