创建多个类似的基因并使用其输出

发布于 2025-01-18 12:48:29 字数 774 浏览 1 评论 0原文

我有一个如下所示的正则。它基本上运行一个简单的 go 模板工具,获取资源名称、json 文件和模板并输出渲染的文件。我有一堆资源,所有这些资源都需要位于单独的文件中,并最终打包到一个容器中。

resources = ["foo", "bar"]
[genrule(
    name = "generate_" + resource + "_config",
    srcs = [
        "//some:tool:config.tmpl",
        "//some:json",
    ],
    outs = [resource + ".hcl"],
    cmd = "$(location //some/tool:template) -resource " + resource + " -json_path=$(location //some:json) -template=$(location //some/tool:config.tmpl) -out=$@",
    tools = [
        "/some/tool:template",
    ],
) for resource in resources]

上面将生成一些名为 generate_foo_configgenerate_bar_config 的规则,并且文件输出正确。然而,如果不直接在文件组或 pkg_tar 规则中指定而不枚举每一项,我无法弄清楚如何使用每一项。我希望能够向 resources 变量添加新内容,并将其自动包含在文件组或 tar 中,以便稍后在构建规则中使用。这可能吗?

I have a genrule that looks like the below. It basically runs a simple go template tool that gets a resource name, a json file and template and outputs the rendered file. I have a bunch of resources that all need to be in separate files and ultimately packaged into a container.

resources = ["foo", "bar"]
[genrule(
    name = "generate_" + resource + "_config",
    srcs = [
        "//some:tool:config.tmpl",
        "//some:json",
    ],
    outs = [resource + ".hcl"],
    cmd = "$(location //some/tool:template) -resource " + resource + " -json_path=$(location //some:json) -template=$(location //some/tool:config.tmpl) -out=$@",
    tools = [
        "/some/tool:template",
    ],
) for resource in resources]

The above will generate a few rules named generate_foo_config and generate_bar_config and the files output correctly. I however cannot figure out how to use each one without specifying them directly in a filegroup or pkg_tar rule without enumerating each one. I would like to be able to add a new thing to the resources variable, and have it automatically included in the filegroup or tar for use in a build rule later. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-25 12:48:29

使用 列表理解,就像您已经创建了属规则。像这样:

pkg_tar(
    name = "all_resources",
    srcs = [":generate_" + resource + "_config" for resource in resources],
)

您还可以将列表放入 BUILD 文件中的变量中以多次使用它:

all_resources = [":generate_" + resource + "_config" for resource in resources]
pkg_tar(
    name = "all_resources",
    srcs = all_resources,
)
filegroup(
    name = "resources_filegroup",
    srcs = all_resources,
)

Use a list comprehension, like you've got creating the genrules. Something like this:

pkg_tar(
    name = "all_resources",
    srcs = [":generate_" + resource + "_config" for resource in resources],
)

You can also put the list in a variable in the BUILD file to use it multiple times:

all_resources = [":generate_" + resource + "_config" for resource in resources]
pkg_tar(
    name = "all_resources",
    srcs = all_resources,
)
filegroup(
    name = "resources_filegroup",
    srcs = all_resources,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文