我正在尝试使用Starlark做一个非常简单的实现:
greeting.bzl
def greet():
print ("Hello World!")
test.bzl
load (":greeting.bzl", "greet")
greet()
并执行以下命令:
java -jar Starlark_deploy.jar test.bzl
上述调用的结果是:
file ':greeting.bzl' was not correctly loaded. Make sure the 'load' statement appears in the global scope in your file
我的最终目标是拥有自己的Starlark引擎,该引擎取决于Starlark_deploy.jar
。然后,我将利用Bazel规则(例如htt_archive
和http_file
)的存在,并定义自己的Starlark超集。
我正在为Starlark described in the official documentation使用Bazel Java实现。它是通过以下方式获得的:
bazel build //src/main/java/com/google/devtools/starlark:Starlark_deploy.jar
Starlark_deploy.jar
答案 0 :(得分:2)
未记录,因为它不是干净或稳定的API。预计将来API会发生变化。要嵌入其他工具,Go implementation要更加成熟。
话虽如此,如果您想尝试,可以:
import com.google.devtools.build.lib.syntax.ParserInputSource;
import com.google.devtools.build.lib.syntax.ParserInputSource;
import com.google.devtools.build.lib.syntax.StringLiteral;
import java.util.HashMap;
import java.util.Map;
public Environment newEnvironment(Map<String, Environment.Extension> imports) {
return Environment.builder(mutability)
.useDefaultSemantics()
.setGlobals(Environment.DEFAULT_GLOBALS)
.setEventHandler(PRINT_HANDLER)
.setImportedExtensions(imports)
.build();
}
public Environment execute(String path)
throws InterruptedException, IOException, EvalException {
String content = new String(Files.readAllBytes(Paths.get(path)), CHARSET);
ParserInputSource input = ParserInputSource.create(content, PathFragment.EMPTY_FRAGMENT);
BuildFileAST ast = BuildFileAST.parseSkylarkFileWithoutImports(input, PRINT_HANDLER);
Map<String, Environment.Extension> imports = new HashMap<>();
for (StringLiteral imp : ast.getRawImports()) {
imports.put(
imp.getValue(),
new Environment.Extension(execute(imp.getValue())));
}
Environment env = newEnvironment(imports);
ast.eval(env);
return env;
}
执行后返回环境,因此您可以检查已定义的变量或函数。
在上面的示例中,模块在for
循环中一个接一个地加载。您可以像Bazel一样并行进行评估。
正如我之前所说,期望API发生重大变化。