Skip to content

Commit

Permalink
refactor(toolchains): split asset rules
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed Jul 23, 2024
1 parent b950e97 commit f76d0b8
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 98 deletions.
4 changes: 2 additions & 2 deletions docs/bzl/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions docs/toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ platform_toolchains(<a href="#platform_toolchains-name">name</a>, <a href="#plat
| <a id="platform_toolchains-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required | |


<a id="assets"></a>
<a id="assets_bundle"></a>

## assets
## assets_bundle

<pre>
assets(<a href="#assets-name">name</a>, <a href="#assets-out_file">out_file</a>, <a href="#assets-kwargs">kwargs</a>)
assets_bundle(<a href="#assets_bundle-name">name</a>, <a href="#assets_bundle-out_file">out_file</a>, <a href="#assets_bundle-kwargs">kwargs</a>)
</pre>


Expand All @@ -112,9 +112,9 @@ assets(<a href="#assets-name">name</a>, <a href="#assets-out_file">out_file</a>,

| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="assets-name"></a>name | <p align="center"> - </p> | <code>"assets"</code> |
| <a id="assets-out_file"></a>out_file | <p align="center"> - </p> | <code>None</code> |
| <a id="assets-kwargs"></a>kwargs | <p align="center"> - </p> | none |
| <a id="assets_bundle-name"></a>name | <p align="center"> - </p> | <code>"assets"</code> |
| <a id="assets_bundle-out_file"></a>out_file | <p align="center"> - </p> | <code>None</code> |
| <a id="assets_bundle-kwargs"></a>kwargs | <p align="center"> - </p> | none |


<a id="multi_platform_assets"></a>
Expand Down
1 change: 1 addition & 0 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions lib/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/private/toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
109 changes: 22 additions & 87 deletions lib/private/toolchains/assets.bzl
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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,
)
76 changes: 76 additions & 0 deletions lib/private/toolchains/assets_bundle.bzl
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit f76d0b8

Please sign in to comment.