简体   繁体   English

Bazel - 取决于生成的输出

[英]Bazel - depend on generated outputs

I have a yaml file in a Bazel monorepo that has constants I'd like to use in several languages.我在 Bazel monorepo 中有一个 yaml 文件,其中包含我想在多种语言中使用的常量。 This is kind of like how protobuffs are created and used.这有点像 protobuff 的创建和使用方式。

How can I parse this yaml file at build time and depend on the outputs?如何在构建时解析这个 yaml 文件并依赖于输出?

For instance:例如:

item1: "hello"
item2: "world"
nested:
  nested1: "I'm nested"
  nested2: "I'm also nested"

I then need to parse this yaml file so it can be used in many different languages (eg, Rust, TypeScript, Python, etc.).然后我需要解析这个 yaml 文件,以便它可以用于许多不同的语言(例如,Rust、TypeScript、Python 等)。 For instance, here's the desired output for TypeScript:例如,这是 TypeScript 所需的输出:

export default {
  item1: "hello",
  item2: "world",
  nested: {
    nested1: "I'm nested",
    nested2: "I'm also nested",
  }
}

Notice, I don't want TypeScript code that reads the yaml file and converts it into an object.请注意,我不希望 TypeScript 代码读取 yaml 文件并将其转换为对象。 That conversion should be done in the build process.这种转换应该在构建过程中完成。

For the actual conversion, I'm thinking of writing that in Python, but it doesn't need to be.对于实际的转换,我正在考虑用 Python 编写它,但并不需要如此。 This would then mean the python also needs to run at build time.这意味着 python 也需要在构建时运行。


PS I care mostly about the functionality, so I'm flexible with the exactly how it's done. PS 我最关心的是功能,所以我对它的具体实现方式很灵活。 I'm even fine using another file format aside from yaml.除了 yaml 之外,我什至可以使用另一种文件格式。

Thanks to help from @oakad, I was able to figure it out.感谢@oakad 的帮助,我得以弄清楚。 Essentially, you can use genrule to create outputs.本质上,您可以使用 genrule 来创建输出。

So, assuming you have some target like python setup to generate the output named parse_config , you can just do this:所以,假设你有一些像 python setup 这样的目标来生成名为parse_config的输出,你可以这样做:

genrule(
    name = "generated_output",
    srcs = [],
    outs = ["output.txt"],
    cmd = "./$(locations :parse_config) > $@" % name,
    tools = [":parse_config"],
    visibility = ["//visibility:public"],
)

The generated file is output.txt .生成的文件是output.txt And you can access it via //lib/config:generated_output .您可以通过//lib/config:generated_output访问它。

Note, essentially the cmd is piping the stdout into the file contents.请注意,本质上cmd正在将stdout通过管道传输到文件内容中。 In Python that means anything printed will appear in the generated file.在 Python 中,这意味着打印的任何内容都会出现在生成的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM