首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >构建依赖于另一条规则的bazel规则

构建依赖于另一条规则的bazel规则
EN

Stack Overflow用户
提问于 2019-12-09 02:30:34
回答 1查看 945关注 0票数 1

我试图使用GraphQL库为编码graphql-codegen类型构建一个bazel规则。

gqlgen规则的实现非常简单:

代码语言:javascript
复制
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

_SCHEMA = """
  {types}:
    schema: {schema}
    plugins:
      - "typescript"
  {meta}:
    schema: {schema}
    plugins:
      - "introspection"
"""

_CONFIG = """
overwrite: true
generates:
  {instructions}
"""

def _gqlgen_impl(ctx):
    config_file = ctx.actions.declare_file(ctx.label.name + ".yml")

    out_files = []
    instructions = []
    for s in ctx.files.srcs:
        name = paths.basename(s.path)[:-len(".graphql")]
        types = ctx.actions.declare_file("types/{}.ts".format(name))
        meta = ctx.actions.declare_file("meta/{}.json".format(name))
        out_files.append(types)
        out_files.append(meta)
        instructions.append(_SCHEMA.format(schema = s.path, types = types.path, meta = meta.path))
    instructions = "\n".join(instructions)

    ctx.actions.write(
        content = _CONFIG.format(instructions = instructions),
        output = config_file,
    )

    ctx.actions.run_shell(
        tools = [ctx.executable._codegen],
        inputs = depset(ctx.files.srcs + [config_file]),
        outputs = out_files,
        command = "{gqlgen} --config {cfg}".format(
            gqlgen = ctx.executable._codegen.path,
            cfg = config_file.path,
        ),
    )

输入是一组*.graphql文件,输出是一组对应的*.json*.ts文件。

我使用的是nodejs规则,因为graphql-codegen是一个节点模块,所以我在gqlgen规则中为它声明了一个规则:

代码语言:javascript
复制
def gqlgen(name, **kwargs):
    nodejs_binary(
        name = "gqlgen",
        data = ["@npm//:node_modules"],
        entry_point = "@npm//:node_modules/@graphql-codegen/cli/bin.js",
        install_source_map_support = False,
        visibility = ["//visibility:public"],
    )

    _gqlgen(
        name = name,
        **kwargs
    )

我的问题在于把这两件事联系起来。我有以下几点:

代码语言:javascript
复制
_gqlgen = rule(
    implementation = _gqlgen_impl,
    attrs = {
        "srcs": attr.label_list(
            allow_files = [".graphql"],
        ),
        "_codegen": attr.label(
            cfg = "host",
            default = "//schemas:gqlgen",
            executable = True,
        ),
    },
)

但是,请注意,对于_codegen属性,我将可执行文件指定为//schemas:gqlgen,这是错误的,因为我应该能够从任何包中使用gqlgen规则。

有没有方法从调用nodejs_binary中引用rule()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-10 01:21:54

为了回答我的问题,我可以简单地做以下几点:

代码语言:javascript
复制
_gqlgen = rule(
    implementation = _gqlgen_impl,
    attrs = {
        "srcs": attr.label_list(
            allow_files = [".graphql"],
        ),
        "_codegen": attr.label(
            cfg = "host",
            executable = True,
        ),
    },
)

def gqlgen(name, **kwargs):
    nodejs_binary(
        name = "gqlgen",
        data = ["@npm//:node_modules"],
        entry_point = "@npm//:node_modules/@graphql-codegen/cli/bin.js",
        install_source_map_support = False,
        visibility = ["//visibility:public"],
    )

    _gqlgen(
        name = name,
        _codegen = "gqlgen",
        **kwargs
    )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59241815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档