微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 Bazel 规则输出的目录扩展为另一个规则的平面输出

如何解决将 Bazel 规则输出的目录扩展为另一个规则的平面输出

我正在尝试打包要上传到 Google Cloud 的包。我有一个来自我所做的角度构建的 pkg_web 输出,如果我传递给我正在生成的这个自定义规则,它是一个 File 对象,它是文件的目录。我生成自定义规则采用 app.yaml 等,以及包和上传

但是,bundle 变成了一个目录,我需要在命令的根目录中扩展该目录的文件才能上传

例如:

- bundle/index.html <-- bundle directory
- bundle/main.js
- app.yaml

我需要:

- index.html
- main.js
- app.yaml

我的规则:

deploy(
  name = "deploy",srcs = [":bundle"] <-- pkg_web rule,yaml = ":app.yaml"
)

规则实施:

def _deploy_pkg(ctx):
    inputs = []
    inputs.append(ctx.file.yaml)
    inputs.extend(ctx.files.srcs)

    script_template = """
       #!/bin/bash
       gcloud app deploy {yaml_path}
    """
    script = ctx.actions.declare_file("%s-deploy" % ctx.label.name)
    ctx.actions.write(script,script_content,is_executable = True)

    runfiles = ctx.runfiles(files = inputs,transitive_files = depset(ctx.files.srcs))
    return [DefaultInfo(executable = script,runfiles = runfiles)]

谢谢你的想法!

解决方法

似乎有点过分,但我最终使用自定义 shell 命令来完成此操作:

def _deploy_pkg(ctx):
    inputs = []

    out = ctx.actions.declare_directory("out")
    yaml_out = ctx.actions.declare_file(ctx.file.yaml.basename)

    inputs.append(out)

    ctx.actions.run_shell(
        outputs = [yaml_out],inputs = [ctx.file.yaml],arguments = [ctx.file.yaml.path,yaml_out.path],progress_message = "Copying yaml to output directory.",command = "cp $1 $2",)

    for f in ctx.files.srcs:
        if f.is_directory:
            ctx.actions.run_shell(
                outputs = [out],inputs = [f],arguments = [f.path,out.path],progress_message = "Copying %s to output directory.".format(f.basename),command = "cp -a -R $1/* $2",)
        else:
            out_file = ctx.actions.declare_file(f.basename)
            inputs.append(out_file)
            ctx.actions.run_shell(
                outputs = [out_file],out_file.path],# This is what we're all about here. Just a simple 'cp' command.
                # Copy the input to CWD/f.basename,where CWD is the package where
                # the copy_filegroups_to_this_package rule is invoked.
                # (To be clear,the files aren't copied right to where your BUILD
                # file sits in source control. They are copied to the 'shadow tree'
                # parallel location under `bazel info bazel-bin`)
                command = "cp -a $1 $2",)
    ....
  
``

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。