簡體   English   中英

將 Bazel 規則輸出的目錄展開為另一個規則的平面 output

[英]Expand a Bazel rule output's directory into a flat output of another rule

我正在嘗試將 package 捆綁包上傳到 Google Cloud。 我有一個來自我所做的 angular 構建的pkg_web的 output,如果我傳遞到我正在生成的這個自定義規則中,它是一個File object 的目錄,它是文件的目錄。 我生成的自定義規則采用 app.yaml 等,以及捆綁包和上傳。

但是,捆綁包變成了一個目錄,我需要擴展該目錄的文件以在命令的根目錄中上傳。

例如:

- 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],
                inputs = [f],
                arguments = [f.path, out_file.path],
                progress_message = "Copying %s to output directory.".format(f.basename),
                # 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",
            )
    ....
  
``

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM