【bazel】 一文读懂Starlark

发布于:2024-07-08 ⋅ 阅读:(44) ⋅ 点赞:(0)

Starlark 的语言编码

当我们编写工作区和构建文件时,我们使用一种名为 Starlark 的语言进行编码,它是 Python 的一种自定义变体。现在,对于所有 Python 开发人员来说,请保持您的敬意。Starlark 是 Python 的一种变体 - 即该语言的受限版本。【When we write our workspaces and build files, we code in language called Starlark which is a custom variant of Python. Now for all you Python developers out there, hold your high fives. Starlark is a variant of Python - that is, a limited version of the language.】

这是专门为 Bazel 设计的。它旨在具有确定性,并设计为并行运行,例如在完全不同的机器上的核心上运行。因此,您最喜欢的一些语言功能可能已被剔除。

例如,对于 Starlark,类已从语言中提取。您不能使用 import 语句。相反,您使用自定义加载函数。大多数内置函数和大多数方法都不受支持。还有许多其他限制。【This was specifically designed for Bazel. It was meant to be deterministic and designed to run in parallel such as on cores are entirely different machines. Because of this, some of your favorite features of the language may have been culled.

For instance, with Starlark, classes have been pulled from the language. You can’t use the import statement. Instead, you use a custom load function. Most built in functions and most methods aren’t supported. There are a bunch of other limitations as well.】

如果您对 Starlark 的功能感兴趣,那么您应该查看 Starlark 规范。这全面细分了从类型系统到各种内置方法和函数,您可以使用哪些语言功能。

这可能会引出一个问题:你可以用 Starlark 做什么?答案是很多。如果你查看构建 API,你会发现 Bazel 附带了许多开箱即用的函数、对象和类型。通过查看构建 API 文档【https://bazel.build/rules/lib/starlark-overview】,你会看到 Bazel 向我们的脚本公开的所有各种内容。

【If you are interested in what Starlark can do, then you should check out the Starlark specification. This gives a comprehensive breakdown at what language features are available to you from the type system to various built in methods and functions.

This may beg the question as to what you can do with Starlark and the answer is a lot. If you look at the build api, you’ll see that Bazel comes with a lot of functions, objects and types ready to use out of the box. By viewing the build api documentation, you’ll see all the various things that Bazel exposes to our scripts.】
好的,对于那些不懂 Python 的人,我们会写一些代码来让你感到舒适,但你需要

首先,让我们回到我们的 JokeGenerator 项目。这一切都在 monorepo 中,所以我们需要将我们的工作区文件移动到 monorepo 的根目录中。

现在我们将对工作区进行一些修改,以便你在 Starlark 中工作时感到舒适。

首先,让我们创建一个注释。在 Starlark 和 Python 中,我们使用磅号符号。
【Okay, for those of you who don’t know python, we’ll write a little code to comfortable but you’ll need to

To get started, let’s return to our JokeGenerator project. It’s all in a monorepo so we need to move our Workspace file to the root of our monorepo.

Now we’ll make some alterations to the workspace to you comfortable working in Starlark.

创建变量

当然,在编写构建脚本时注释掉代码也很有用。接下来我们将定义一个变量。在这里我们将创建一个简单的名称。【Of course, this is also useful to comment out code while writing your build scripts. Next we’ll define a variable. Here we’ll create a simple name.】

build_name = "JokeGenerator"

现在我们将它打印到控制台。

打印到控制台

print(build_name)

保存文件并切换到命令行。导航到 monorepo 根目录。要启动构建,我们必须调用 JokeGenerator 的构建目标。【Save the file and switch over to the command line. Navigate to the monorepo root. To fire off the build, we have to call the JokeGenerator’s build target.

bazel build //JokeGenerator:knock_knock

您会注意到,我们在构建序列中打印出了一个调试语句。瞧,有一个变量被打印出来了。

我们确实得到了 if 语句,但我们不能在脚本的顶层使用它们。例如,如果我们要检查构建名称,我们需要将其包装到函数中。

让我们添加以下内容来打印出是否允许开玩笑:【We do get if statements, but we can’t use them in the top-level of a script. For example, if we were to check for the build name, we’d need to wrap it into the function.

Let’s add the following to print out whether jokes are allowed:】

if build_name == "JokeGenerator":
	print("Jokes Allowed")
else:
	print("No Jokes Allowed")

现在如果我们保存并构建:

bazel build //JokeGenerator:knock_knock

创建带有 bzl 扩展名的文本文件

我们得到一个错误。欢迎使用 Starlark。在这样的功能中,我们需要定义自己的规则文件。规则文件只是一个带有 bzl 扩展名的文本文件。当我们导入其他语言的规则时,我们会导入 bzl 文件。【And we get an error. Welcome to Starlark. In such a feature, we need to define our own rules file. A rules file is just a text file with a bzl extension. When we import rules for other languages, we import bzl files.】

通过这种方式,我们将逻辑从构建定义中移除。创建一个名为 functions.bzl 的新文件。

现在让我们定义一个方法来获取当前的笑话策略。

def get_joke_policy(name):
	if name == "JokeGenerator":
		return "Jokes Allowed"
	else:
		return "No Jokes Allowed"

保存并返回工作区。我们需要加载新规则。我们使用 load 方法。添加以下内容:

load(“functions.bzl”, “get_joke_policy”)

在这里我们声明我们正在加载函数规则,然后我们使用 get_joke_policy 函数。现在,在运行此构建之前,我们需要提供一个空的构建文件以及我们的自定义规则。

创建一个新的构建文件。现在我们可以调用该函数。

print(get_joke_policy(build_name))

保存文件。现在运行构建。您将看到我们将笑话策略打印到控制台,同时构建运行完成。

我们将在本课程以及其他一些模块中大量使用加载,因此请随意尝试以熟悉情况。


网站公告

今日签到

点亮在社区的每一天
去签到