From 7219c2ce2c77fb6af10690fabef6dbb60e3bc0a5 Mon Sep 17 00:00:00 2001 From: maleo Date: Mon, 11 Sep 2023 13:12:31 +0000 Subject: [PATCH 1/2] Re-use write_metadata_info() --- rules_gathering/gather_metadata.bzl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/rules_gathering/gather_metadata.bzl b/rules_gathering/gather_metadata.bzl index aae5b69..88ac5e1 100644 --- a/rules_gathering/gather_metadata.bzl +++ b/rules_gathering/gather_metadata.bzl @@ -85,14 +85,9 @@ def _write_metadata_info_impl(target, ctx): return [OutputGroupInfo(licenses = depset())] # Write the output file for the target - name = "%s_metadata_info.json" % ctx.label.name - content = "[\n%s\n]\n" % ",\n".join(metadata_info_to_json(info)) - out = ctx.actions.declare_file(name) - ctx.actions.write( - output = out, - content = content, - ) - outs.append(out) + json_out = ctx.actions.declare_file("%s_metadata_info.json" % ctx.label.name) + write_metadata_info(ctx, target, json_out) + outs.append(json_out) if ctx.attr._trace[TraceInfo].trace: trace = ctx.actions.declare_file("%s_trace_info.json" % ctx.label.name) @@ -140,8 +135,8 @@ def write_metadata_info(ctx, deps, json_out): def _foo_impl(ctx): ... - out = ctx.actions.declare_file("%s_licenses.json" % ctx.label.name) - write_metadata_info(ctx, ctx.attr.deps, metadata_file) + json_file = ctx.actions.declare_file("%s_licenses.json" % ctx.label.name) + write_metadata_info(ctx, ctx.attr.deps, json_file) Args: ctx: context of the caller @@ -150,10 +145,12 @@ def write_metadata_info(ctx, deps, json_out): aspect over them json_out: output handle to write the JSON info """ + licenses = [] for dep in deps: if TransitiveMetadataInfo in dep: - licenses.extend(metadata_info_to_json(dep[TransitiveMetadataInfo])) + metadata_info = dep[TransitiveMetadataInfo] + licenses.extend(metadata_info_to_json(metadata_info)) ctx.actions.write( output = json_out, content = "[\n%s\n]\n" % ",\n".join(licenses), From d739509e568a6546cb9086b27c9e260df6200bac Mon Sep 17 00:00:00 2001 From: maleo Date: Mon, 11 Sep 2023 13:32:25 +0000 Subject: [PATCH 2/2] Re-use write_licenses_info() --- rules/gather_licenses_info.bzl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/rules/gather_licenses_info.bzl b/rules/gather_licenses_info.bzl index 518d5ff..9254a6c 100644 --- a/rules/gather_licenses_info.bzl +++ b/rules/gather_licenses_info.bzl @@ -71,15 +71,9 @@ def _write_licenses_info_impl(target, ctx): return [OutputGroupInfo(licenses = depset())] # Write the output file for the target - name = "%s_licenses_info.json" % ctx.label.name - lic_info, _ = licenses_info_to_json(info) - content = "[\n%s\n]\n" % ",\n".join(lic_info) - out = ctx.actions.declare_file(name) - ctx.actions.write( - output = out, - content = content, - ) - outs.append(out) + json_out = ctx.actions.declare_file("%s_licenses_info.json" % ctx.label.name) + write_licenses_info(ctx, target, json_out) + outs.append(json_out) if ctx.attr._trace[TraceInfo].trace: trace = ctx.actions.declare_file("%s_trace_info.json" % ctx.label.name) @@ -146,6 +140,7 @@ def write_licenses_info(ctx, deps, json_out): Returns: A list of License File objects for each of the license paths referenced in the json. """ + licenses_json = [] licenses_files = [] for dep in deps: