Skip to content

Commit

Permalink
Return providers instead of struct from rule impl functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alex1545 committed Mar 28, 2019
1 parent fb1ec3c commit b222896
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 82 deletions.
28 changes: 12 additions & 16 deletions container/bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"""Rule for bundling Container images into a tarball."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@io_bazel_rules_docker//container:providers.bzl", "BundleInfo")
load(
"//container:layer_tools.bzl",
_assemble_image = "assemble",
_get_layers = "get_from_target",
_incr_load = "incremental_load",
_layer_tools = "tools",
)
load("//container:providers.bzl", "BundleInfo")
load(
"//skylib:label.bzl",
_string_to_label = "string_to_label",
Expand Down Expand Up @@ -71,21 +71,17 @@ def _container_bundle_impl(ctx):

stamp_files = [ctx.info_file, ctx.version_file] if stamp else []

return struct(
container_images = images,
stamp = stamp,
providers = [
BundleInfo(
container_images = images,
stamp = stamp,
),
DefaultInfo(
files = depset(),
executable = ctx.outputs.executable,
runfiles = ctx.runfiles(files = (stamp_files + runfiles)),
),
],
)
return [
BundleInfo(
container_images = images,
stamp = stamp,
),
DefaultInfo(
executable = ctx.outputs.executable,
files = depset(),
runfiles = ctx.runfiles(files = (stamp_files + runfiles)),
),
]

container_bundle_ = rule(
attrs = dicts.add({
Expand Down
2 changes: 1 addition & 1 deletion container/flatten.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"""A rule to flatten container images."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@io_bazel_rules_docker//container:providers.bzl", "FlattenInfo")
load(
"//container:layer_tools.bzl",
_get_layers = "get_from_target",
_layer_tools = "tools",
)
load("//container:providers.bzl", "FlattenInfo")

def _impl(ctx):
"""Core implementation of container_flatten."""
Expand Down
37 changes: 17 additions & 20 deletions container/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ load(
_hash_tools = "tools",
_sha256 = "sha256",
)
load(
"@io_bazel_rules_docker//container:providers.bzl",
"ImageInfo",
"LayerInfo",
)
load(
"//container:layer.bzl",
_layer = "layer",
Expand All @@ -56,11 +61,6 @@ load(
_incr_load = "incremental_load",
_layer_tools = "tools",
)
load(
"//container:providers.bzl",
"ImageInfo",
"LayerInfo",
)
load(
"//skylib:filetype.bzl",
container_filetype = "container",
Expand Down Expand Up @@ -433,21 +433,18 @@ def _impl(
([container_parts["legacy"]] if container_parts["legacy"] else []),
)

return struct(
container_parts = container_parts,
providers = [
ImageInfo(
container_parts = container_parts,
legacy_run_behavior = legacy_run_behavior,
docker_run_flags = docker_run_flags,
),
DefaultInfo(
executable = output_executable,
files = depset([output_layer]),
runfiles = runfiles,
),
],
)
return [
ImageInfo(
container_parts = container_parts,
legacy_run_behavior = legacy_run_behavior,
docker_run_flags = docker_run_flags,
),
DefaultInfo(
executable = output_executable,
files = depset([output_layer]),
runfiles = runfiles,
),
]

_attrs = dicts.add(_layer.attrs, {
"base": attr.label(allow_files = container_filetype),
Expand Down
25 changes: 11 additions & 14 deletions container/import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ load(
_hash_tools = "tools",
_sha256 = "sha256",
)
load("@io_bazel_rules_docker//container:providers.bzl", "ImportInfo")
load(
"//container:layer_tools.bzl",
_assemble_image = "assemble",
_incr_load = "incremental_load",
_layer_tools = "tools",
)
load("//container:providers.bzl", "ImportInfo")
load(
"//skylib:filetype.bzl",
tar_filetype = "tar",
Expand Down Expand Up @@ -141,19 +141,16 @@ def _container_import_impl(ctx):
),
)

return struct(
container_parts = container_parts,
providers = [
ImportInfo(
container_parts = container_parts,
),
DefaultInfo(
executable = ctx.outputs.executable,
files = depset([ctx.outputs.out]),
runfiles = runfiles,
),
],
)
return [
ImportInfo(
container_parts = container_parts,
),
DefaultInfo(
executable = ctx.outputs.executable,
files = depset([ctx.outputs.out]),
runfiles = runfiles,
),
]

container_import = rule(
attrs = dicts.add({
Expand Down
2 changes: 1 addition & 1 deletion container/layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ load(
_hash_tools = "tools",
_sha256 = "sha256",
)
load("@io_bazel_rules_docker//container:providers.bzl", "LayerInfo")
load(
"//container:layer_tools.bzl",
_layer_tools = "tools",
)
load("//container:providers.bzl", "LayerInfo")
load(
"//skylib:filetype.bzl",
deb_filetype = "deb",
Expand Down
11 changes: 9 additions & 2 deletions container/layer_tools.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# limitations under the License.
"""Tools for dealing with Docker Image layers."""

load(
"@io_bazel_rules_docker//container:providers.bzl",
"ImageInfo",
"ImportInfo",
)
load(
"//skylib:path.bzl",
_get_runfile_path = "runfile",
Expand Down Expand Up @@ -58,8 +63,10 @@ def get_from_target(ctx, name, attr_target, file_target = None):
"""
if file_target:
return _extract_layers(ctx, name, file_target)
elif hasattr(attr_target, "container_parts"):
return attr_target.container_parts
elif attr_target and ImageInfo in attr_target:
return attr_target[ImageInfo].container_parts
elif attr_target and ImportInfo in attr_target:
return attr_target[ImportInfo].container_parts
else:
if not hasattr(attr_target, "files"):
return {}
Expand Down
7 changes: 5 additions & 2 deletions container/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Bazel rule for publishing images.
"""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@io_bazel_rules_docker//container:providers.bzl", "PushInfo")
load(
"//container:layer_tools.bzl",
_get_layers = "get_from_target",
_layer_tools = "tools",
)
load("//container:providers.bzl", "PushInfo")
load(
"//skylib:path.bzl",
"runfile",
Expand Down Expand Up @@ -128,7 +128,10 @@ def _impl(ctx):
runfiles = runfiles.merge(ctx.attr._pusher[DefaultInfo].default_runfiles)

return [
DefaultInfo(executable = ctx.outputs.executable, runfiles = runfiles),
DefaultInfo(
executable = ctx.outputs.executable,
runfiles = runfiles,
),
PushInfo(
registry = registry,
repository = repository,
Expand Down
5 changes: 3 additions & 2 deletions contrib/push-all.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This variant of container_push accepts a container_bundle target and publishes
the embedded image references.
"""

load("@io_bazel_rules_docker//container:providers.bzl", "BundleInfo")
load(
"//skylib:path.bzl",
"runfile",
Expand All @@ -27,8 +28,8 @@ def _get_runfile_path(ctx, f):

def _impl(ctx):
"""Core implementation of container_push."""
stamp = ctx.attr.bundle.stamp
images = ctx.attr.bundle.container_images
stamp = ctx.attr.bundle[BundleInfo].stamp
images = ctx.attr.bundle[BundleInfo].container_images

stamp_inputs = []
if stamp:
Expand Down
36 changes: 12 additions & 24 deletions lang/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load(
"@io_bazel_rules_docker//container:providers.bzl",
"FilterAspectInfo",
"FilterLayerInfo",
)
load(
"//container:container.bzl",
_container = "container",
Expand All @@ -23,7 +28,6 @@ load(
"//container:layer_tools.bzl",
_get_layers = "get_from_target",
)
load("//container:providers.bzl", "FilterAspectInfo", "FilterLayerInfo")

def _binary_name(ctx):
# For //foo/bar/baz:blah this would translate to
Expand Down Expand Up @@ -307,29 +311,13 @@ def _filter_layer_rule_impl(ctx):
runfiles = runfiles.merge(dep.target[DefaultInfo].default_runfiles)
filtered_depsets.append(dep.target_deps)

# Forward correct provider, depending on availability, so that the filter_layer() can be
# used as a normal dependency to native targets (e.g. py_library(deps = [<filter_layer>])).
if hasattr(ctx.attr.dep, "py"):
# Forward legacy builtin provider and PyInfo provider
return struct(
providers = [
FilterLayerInfo(
runfiles = runfiles,
filtered_depset = depset(transitive = filtered_depsets),
),
] + ([ctx.attr.dep[PyInfo]] if PyInfo in ctx.attr.dep else []),
py = ctx.attr.dep.py if hasattr(ctx.attr.dep, "py") else None,
)
else:
# Forward the PyInfo provider only
return struct(
providers = [
FilterLayerInfo(
runfiles = runfiles,
filtered_depset = depset(transitive = filtered_depsets),
),
] + ([ctx.attr.dep[PyInfo]] if PyInfo in ctx.attr.dep else []),
)
# Forward legacy builtin provider and PyInfo provider
return [
FilterLayerInfo(
runfiles = runfiles,
filtered_depset = depset(transitive = filtered_depsets),
),
] + ([ctx.attr.dep[PyInfo]] if PyInfo in ctx.attr.dep else [])

# A rule that allows selecting a subset of transitive dependencies, and using
# them as a layer in an image.
Expand Down

0 comments on commit b222896

Please sign in to comment.