我正在尝试为格尔根编写一个自定义规则。其想法是运行它以从GraphQL模式生成Go代码。
我的预期用途是:
gqlgen(
name = "gql-gen-foo",
schemas = ["schemas/schema.graphql"],
visibility = ["//visibility:public"],
)" name“是规则的名称,我希望其他规则依赖它;”schema“是输入文件的集合。
到目前为止,我已经:
load(
"@io_bazel_rules_go//go:def.bzl",
_go_context = "go_context",
_go_rule = "go_rule",
)
def _gqlgen_impl(ctx):
go = _go_context(ctx)
args = ["run github.com/99designs/gqlgen --config"] + [ctx.attr.config]
ctx.actions.run(
inputs = ctx.attr.schemas,
outputs = [ctx.actions.declare_file(ctx.attr.name)],
arguments = args,
progress_message = "Generating GraphQL models and runtime from %s" % ctx.attr.config,
executable = go.go,
)
_gqlgen = _go_rule(
implementation = _gqlgen_impl,
attrs = {
"config": attr.string(
default = "gqlgen.yml",
doc = "The gqlgen filename",
),
"schemas": attr.label_list(
allow_files = [".graphql"],
doc = "The schema file location",
),
},
executable = True,
)
def gqlgen(**kwargs):
tags = kwargs.get("tags", [])
if "manual" not in tags:
tags.append("manual")
kwargs["tags"] = tags
_gqlgen(**kwargs)我当前的问题是Bazel抱怨模式不是Files。
expected type 'File' for 'inputs' element but got type 'Target' instead指定输入文件的正确方法是什么?
这是生成执行命令的规则的正确方法吗?
最后,可以让输出文件不存在于文件系统中,而是成为其他规则可以依赖的标签吗?
发布于 2019-08-14 03:06:39
而不是:
ctx.actions.run(
inputs = ctx.attr.schemas,使用:
ctx.actions.run(
inputs = ctx.files.schemas,这是生成执行命令的规则的正确方法吗?
只要gqlgen使用正确的输出名称(outputs = [ctx.actions.declare_file(ctx.attr.name)])创建文件,这看起来是正确的。
generated_go_file = ctx.actions.declare_file(ctx.attr.name + ".go")
# ..
ctx.actions.run(
outputs = [generated_go_file],
args = ["run", "...", "--output", generated_go_file.short_path],
# ..
)最后,可以让输出文件不存在于文件系统中,而是成为其他规则可以依赖的标签吗?
需要创建输出文件,只要在提供程序中的规则实现结束时返回它,其他规则就可以依赖于文件标签(例如//my/package:foo-gqlgen.go)。
https://stackoverflow.com/questions/57482616
复制相似问题