load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
load("@rules_ruby//ruby:defs.bzl", "rb_binary", "rb_library")

package(default_visibility = ["//rb:__subpackages__"])

CDP_VERSIONS = [
    "v85",
    "v131",
    "v132",
    "v130",
]

rb_library(
    name = "devtools",
    data = [":cdp-" + n for n in CDP_VERSIONS],
    deps = [":version"],
)

rb_library(
    name = "version",
    srcs = ["version.rb"],
)

rb_binary(
    name = "cdp-generate",
    srcs = ["support/cdp_client_generator.rb"],
    data = [
        "support/cdp/domain.rb.erb",
        "support/cdp/loader.rb.erb",
    ],
    main = "support/cdp_client_generator.rb",
)

[
    run_binary(
        name = "cdp-" + version,
        srcs = [
            "//common/devtools/chromium/" + version + ":browser_protocol",
            "//common/devtools/chromium/" + version + ":js_protocol",
        ],
        outs = [version + ".rb"],
        args = [
            "$(location //common/devtools/chromium/" + version + ":browser_protocol)",
            "$(location //common/devtools/chromium/" + version + ":js_protocol)",
            "$(location :" + version + ".rb)",
            version,
        ],
        out_dirs = [version],
        tool = ":cdp-generate",
    )
    for version in CDP_VERSIONS
]
