亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試編寫自定義規則

嘗試編寫自定義規則

Go
胡說叔叔 2023-07-10 17:43:32
我正在嘗試為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)。


查看完整回答
反對 回復 2023-07-10
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號