From f76d0b8cee312c226e2d2a438873b8a19ac3ecf4 Mon Sep 17 00:00:00 2001 From: Marc Redemske Date: Tue, 23 Jul 2024 21:11:06 +0200 Subject: [PATCH] refactor(toolchains): split asset rules --- docs/bzl/toolchains.bzl | 4 +- docs/toolchains.md | 12 +-- lib/BUILD.bazel | 1 + lib/defs.bzl | 9 +- lib/private/toolchains/BUILD.bazel | 13 ++- lib/private/toolchains/assets.bzl | 109 +++++------------------ lib/private/toolchains/assets_bundle.bzl | 76 ++++++++++++++++ 7 files changed, 126 insertions(+), 98 deletions(-) create mode 100644 lib/private/toolchains/assets_bundle.bzl diff --git a/docs/bzl/toolchains.bzl b/docs/bzl/toolchains.bzl index 3b9a04c..ea7d0e8 100644 --- a/docs/bzl/toolchains.bzl +++ b/docs/bzl/toolchains.bzl @@ -8,7 +8,7 @@ Rules and helpers to create platform-specific toolchains and use them in module load( "//lib:defs.bzl", - _assets = "assets", + _assets_bundle = "assets_bundle", _binary_toolchain = "binary_toolchain", _multi_platform_assets = "multi_platform_assets", _platform_asset = "platform_asset", @@ -18,7 +18,7 @@ load( _resolved_toolchain_impl = "resolved_toolchain_impl", ) -assets = _assets +assets_bundle = _assets_bundle multi_platform_assets = _multi_platform_assets platform_assets = _platform_asset diff --git a/docs/toolchains.md b/docs/toolchains.md index 06c0b72..9a66b95 100644 --- a/docs/toolchains.md +++ b/docs/toolchains.md @@ -97,12 +97,12 @@ platform_toolchains(name, repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | - + -## assets +## assets_bundle
-assets(name, out_file, kwargs)
+assets_bundle(name, out_file, kwargs)
 
@@ -112,9 +112,9 @@ assets(name, out_file, | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| name |

-

| "assets" | -| out_file |

-

| None | -| kwargs |

-

| none | +| name |

-

| "assets" | +| out_file |

-

| None | +| kwargs |

-

| none | diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel index 293d8cd..66ffabb 100644 --- a/lib/BUILD.bazel +++ b/lib/BUILD.bazel @@ -14,6 +14,7 @@ bzl_library( "//lib/private:helpers", "//lib/private:platforms", "//lib/private/toolchains:assets", + "//lib/private/toolchains:assets_bundle", "//lib/private/toolchains:repositories", "//lib/private/utils:create_module_bazel", "//lib/private/utils:formatter", diff --git a/lib/defs.bzl b/lib/defs.bzl index 46782d7..ebd186a 100644 --- a/lib/defs.bzl +++ b/lib/defs.bzl @@ -25,10 +25,13 @@ load( ) load( "//lib/private/toolchains:assets.bzl", - _assets = "assets", _multi_platform_assets = "multi_platform_assets", _platform_asset = "platform_asset", ) +load( + "//lib/private/toolchains:assets_bundle.bzl", + _assets_bundle = "assets_bundle", +) load( "//lib/private/toolchains:repositories.bzl", _binary_toolchain = "binary_toolchain", @@ -57,10 +60,12 @@ load( ) # //lib/private/toolchains:assets -assets = _assets multi_platform_assets = _multi_platform_assets platform_asset = _platform_asset +# //lib/private/toolchains:assets_bundle +assets_bundle = _assets_bundle + # //lib/private/toolchains:repositories.bzl binary_toolchain = _binary_toolchain platform_toolchain = _platform_toolchain diff --git a/lib/private/toolchains/BUILD.bazel b/lib/private/toolchains/BUILD.bazel index 619a2fb..c6ddffe 100644 --- a/lib/private/toolchains/BUILD.bazel +++ b/lib/private/toolchains/BUILD.bazel @@ -8,14 +8,25 @@ exports_files( visibility = ["//visibility:public"], ) +bzl_library( + name = "assets_bundle", + srcs = ["assets_bundle.bzl"], + visibility = ["//lib:__pkg__"], + deps = [ + "//lib/private:helpers", + "//toolchains", + "@aspect_bazel_lib//lib:write_source_files", + ], +) + bzl_library( name = "assets", srcs = ["assets.bzl"], visibility = ["//lib:__pkg__"], deps = [ + ":assets_bundle", "//lib/private:helpers", "//toolchains", - "@aspect_bazel_lib//lib:write_source_files", ], ) diff --git a/lib/private/toolchains/assets.bzl b/lib/private/toolchains/assets.bzl index f8cbdc0..d8f7a27 100644 --- a/lib/private/toolchains/assets.bzl +++ b/lib/private/toolchains/assets.bzl @@ -1,21 +1,24 @@ -# buildifier: disable=module-docstring -load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file") -load( - "//lib/private:helpers.bzl", - "get_binary_from_toolchain", - "write_executable_launcher_file", -) +"Platform Asset" + load("//lib/private/utils:sha.bzl", "sha") load( "//toolchains:toolchains.bzl", - "JSON_BASH_TOOLCHAIN_TYPE", "SHA_TOOLCHAIN_TYPE", - "TEMPL_TOOLCHAIN_TYPE", ) +load(":assets_bundle.bzl", "assets_bundle") def _is_windows(platform): return platform.startswith("windows") +def _os(platform): + return platform.split("_")[0] + +def _switch(val, arms, default = None): + for k, v in arms.items(): + if k == val: + return v + return default + def _platform_asset_impl(ctx): source = ctx.actions.declare_file("%s_/%s_info.json" % (ctx.label.name, ctx.label.name)) merger = ctx.actions.declare_file("%s_/%s_merger.sh" % (ctx.label.name, ctx.label.name)) @@ -81,19 +84,14 @@ platform_asset = rule( }, toolchains = [ SHA_TOOLCHAIN_TYPE, - JSON_BASH_TOOLCHAIN_TYPE, "@aspect_bazel_lib//lib:jq_toolchain_type", ], ) -def _switch(val, arms, default = None): - for k, v in arms.items(): - if k == val: - return v - return default - -def _os(platform): - return platform.split("_")[0] +# def cmd_assets(name, integrity_map): +# for target, platform in integrity_map.items(): +# platform_asset( +# ) # buildifier: disable=function-docstring def multi_platform_assets( @@ -109,7 +107,7 @@ def multi_platform_assets( prefix = "", files = [], platforms_map = {}): - binaries = [] + assets = [] for platform in platforms: _name = "%s_%s" % (name, platform) _platform = platforms_map.get(platform, platform) @@ -118,7 +116,6 @@ def multi_platform_assets( "linux": linux_ext, "windows": windows_ext, }, "tar.gz") - binaries.append(_name) sha( name = "%s_sha" % _name, url = url.format( @@ -139,72 +136,10 @@ def multi_platform_assets( ext = ext, ), ) + assets.append(_name) - assets(name = "%s_assets" % name, out_file = assets_file, srcs = binaries) - -def _assets_impl(ctx): - out = ctx.outputs.out - jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin - buildifier = ctx.toolchains["@buildifier_prebuilt//buildifier:toolchain"]._tool - templ = get_binary_from_toolchain(ctx, TEMPL_TOOLCHAIN_TYPE) - script = write_executable_launcher_file( - ctx, - content = """\ -#!/usr/bin/env bash -{jq} -s '.' $@ | -{templ} -template assets | -{buildifier} -mode fix -lint fix > {out} -""".format( - templ = templ.path, - jq = jq.path, - out = out.path, - buildifier = buildifier.path, - ), - ) - - args = ctx.actions.args() - args.add_all(ctx.files.srcs) - - ctx.actions.run( - inputs = ctx.files.srcs, - outputs = [out], - arguments = [args], - tools = [jq, templ, buildifier], - executable = script, - ) - - return [ - DefaultInfo( - files = depset([out]), - ), - ] - -_assets = rule( - _assets_impl, - attrs = { - "srcs": attr.label_list( - allow_empty = False, - allow_files = True, - mandatory = True, - ), - "out": attr.output(mandatory = True), - }, - toolchains = [ - TEMPL_TOOLCHAIN_TYPE, - "@aspect_bazel_lib//lib:jq_toolchain_type", - "@buildifier_prebuilt//buildifier:toolchain", - ], -) - -def assets(name = "assets", out_file = None, **kwargs): - _assets( - name = name, - out = "%s_bzl" % name, - **kwargs - ) - - write_source_file( - name = "update_%s" % name, - in_file = "%s_bzl" % name, - out_file = out_file or ":%s.bzl" % name, + assets_bundle( + name = "%s_assets" % name, + out_file = assets_file, + srcs = assets, ) diff --git a/lib/private/toolchains/assets_bundle.bzl b/lib/private/toolchains/assets_bundle.bzl new file mode 100644 index 0000000..d4d5545 --- /dev/null +++ b/lib/private/toolchains/assets_bundle.bzl @@ -0,0 +1,76 @@ +"Assets Bundle" + +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file") +load( + "//lib/private:helpers.bzl", + "get_binary_from_toolchain", + "write_executable_launcher_file", +) +load("//toolchains:toolchains.bzl", "TEMPL_TOOLCHAIN_TYPE") + +def _assets_bundle_impl(ctx): + out = ctx.outputs.out + jq = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin + buildifier = ctx.toolchains["@buildifier_prebuilt//buildifier:toolchain"]._tool + templ = get_binary_from_toolchain(ctx, TEMPL_TOOLCHAIN_TYPE) + script = write_executable_launcher_file( + ctx, + content = """\ +#!/usr/bin/env bash +{jq} -s '.' $@ | +{templ} -template assets | +{buildifier} -mode fix -lint fix > {out} +""".format( + templ = templ.path, + jq = jq.path, + out = out.path, + buildifier = buildifier.path, + ), + ) + + args = ctx.actions.args() + args.add_all(ctx.files.srcs) + + ctx.actions.run( + inputs = ctx.files.srcs, + outputs = [out], + arguments = [args], + tools = [jq, templ, buildifier], + executable = script, + ) + + return [ + DefaultInfo( + files = depset([out]), + ), + ] + +_assets_bundle = rule( + _assets_bundle_impl, + attrs = { + "srcs": attr.label_list( + allow_empty = False, + allow_files = True, + mandatory = True, + ), + "out": attr.output(mandatory = True), + }, + toolchains = [ + TEMPL_TOOLCHAIN_TYPE, + "@aspect_bazel_lib//lib:jq_toolchain_type", + "@buildifier_prebuilt//buildifier:toolchain", + ], +) + +def assets_bundle(name = "assets", out_file = None, **kwargs): + _assets_bundle( + name = name, + out = "%s_bzl" % name, + **kwargs + ) + + write_source_file( + name = "update_%s" % name, + in_file = "%s_bzl" % name, + out_file = out_file or ":%s.bzl" % name, + )