簡體   English   中英

有沒有辦法在每次 bazel 調用時使用 local=True 執行 repository_rule ?

[英]Is there a way to execute a repository_rule with local=True on every bazel invocation?

我有一個 repository_rule 正在查詢以查看本地系統是否正在運行數據庫並已完全遷移。

# impl
    result = ctx.execute(["mysql", "-u", "root", "--protocol", "tcp", "-e", "select * from %s.flyway_schema_history" % ctx.attr.dbname])

    ctx.file(
        "local_checksum",
        """
             {RETURN_CODE}
             {STDERR}
             {STDOUT}
             """.format(
            RETURN_CODE = result.return_code,
            STDERR = result.stderr,
            STDOUT = result.stdout,
        ),
    )
...

# Rule Def
local_database = repository_rule(
    implementation = _local_database,
    local = True,
    configure=True,
    attrs = {
        "datasource_configuration": attr.label(providers = [DataSourceConnectionInfo]),
        "dbname": attr.string(doc = """
        If omitted, will be the name of the repository.
        """),
        "migrations": attr.label_list(allow_files = True),
    },
)

每當依賴關系圖發生變化時,都會重新計算 local_checksum 並執行其工作(如文檔https://docs.bazel.build/versions/master/skylark/repository_rules.html#when-is-the-implementation-function中所述-執行)。

但是由於數據庫不是由 bazel 管理的,有沒有辦法在每次調用 bazel 時強制運行此特定規則以確保所有依賴項都可用?

睡了一覺后,我拼湊了一些東西。 仍在尋找更好的答案,我認為有第一個 class 方法來解決這個問題。

我在 tools/bazel 創建了一個 bazel 包裝器

#!/bin/bash

set -e

echo "`date`

Generated by tools/bazel" > .bazelexec.stamp

# from https://github.com/grpc/grpc/blob/master/tools/bazel
exec -a "$0" "${BAZEL_REAL}" "$@"

然后我在規則中添加了一個用於讀取該文件的屬性:

local_database = repository_rule(
    implementation = _local_database,
    local = True,
    configure=True,
    attrs = {
        "datasource_configuration": attr.label(providers = [DataSourceConnectionInfo]),
        "dbname": attr.string(doc = """
        If omitted, will be the name of the repository.
        """),
        "migrations": attr.label_list(allow_files = True),
        "recalculate_when": attr.label_list(allow_files = True, doc = """
        Files to watch which will trigger the repository to run when they change.

        You can add a tools/bazel script to your local repository, and write a file with a date
        every time bazel is executed in order to get the migrator to check each bazel run if
        someone changed the database.
        """),
    },

最后,我在規則中為這些文件創建路徑,以便存儲庫相信它的圖表已經改變。

    # If you don't do something with the file, then the rule does not recalculate.
    [ctx.path(file) for file in ctx.attr.recalculate_when]

    # Total mysql hack for now... need java tool which dumps content of a table for different databases
    result = ctx.execute(
        ["mysql", "-u", "root", "--protocol", "tcp", "-e", "show databases"],
    )

    ctx.file(
        "local_database_checksum",
        """
             {RETURN_CODE}
             {STDERR}
             {STDOUT}
             """.format(
            RETURN_CODE = result.return_code,
            STDERR = result.stderr,
            STDOUT = result.stdout,
        ),
    )

現在每次我運行構建時,如果數據庫發生變化,local_checksum 文件也會發生變化,並且可以觸發其他規則重新構建(在我的情況下,我正在生成 jooq 類,所以我的查詢進入表中),如果數據庫和表是穩定的,那么它不會觸發重建。

暫無
暫無

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

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