我正在嘗試為gqlgen編寫自定義規則。這個想法是運行它來從 GraphQL 模式生成 Go 代碼。我的預期用途是:gqlgen(
????name?=?"gql-gen-foo",
????schemas?=?["schemas/schema.graphql"],
????visibility?=?["http://visibility:public"],
)“name”是規則的名稱,我希望其他規則依賴于此;“模式”是輸入文件的集合。到目前為止我有: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指定輸入文件的正確方法是什么?這是生成執行命令的規則的正確方法嗎?最后,是否可以讓輸出文件不存在于文件系統中,而是作為其他規則可以依賴的標簽?
1 回答

智慧大石
TA貢獻1946條經驗 獲得超3個贊
代替:
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],
# ..
)
最后,是否可以讓輸出文件不存在于文件系統中,而是作為其他規則可以依賴的標簽?
需要創建輸出文件,并且只要它在提供程序中的規則實現結束時返回DefaultInfo,其他規則將能夠依賴于文件標簽(例如//my/package:foo-gqlgen.go)。
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消