load("@rules_jvm_external//:defs.bzl", "artifact")
load("//common:defs.bzl", "copy_file")
load(
    "//java:defs.bzl",
    "java_binary",
    "java_dist_zip",
    "java_export",
    "java_library",
    "javadoc",
    "maven_bom",
    "merge_jars",
)
load("//java:version.bzl", "SE_VERSION")
load("//java/src/org/openqa/selenium/devtools:versions.bzl", "CDP_DEPS")

genrule(
    name = "executable-grid",
    srcs = [
        "zip-preamble.sh",
        ":add-build-info",
    ],
    outs = ["selenium"],
    cmd = "cat $(location :zip-preamble.sh) $(location :add-build-info) > $@",
    executable = True,
    visibility = [
        "//:__pkg__",
        "//deploys/docker:__pkg__",
    ],
)

java_binary(
    name = "pinned-grid",
    data = select({
        "//common:linux": [
            "@linux_chrome//:chrome-linux64/chrome",
            "@linux_chromedriver//:chromedriver",
            "@linux_firefox//:firefox/firefox",
            "@linux_geckodriver//:geckodriver",
        ],
        "//common:macos": [
            "@mac_chrome//:Chrome.app",
            "@mac_chromedriver//:chromedriver",
            "@mac_firefox//:Firefox.app",
            "@mac_geckodriver//:geckodriver",
        ],
        "//conditions:default": [],
    }),
    jvm_flags = select({
        "//common:linux": [
            "-Dwebdriver.chrome.driver=$(location @linux_chromedriver//:chromedriver)",
            "-Dwebdriver.chrome.binary=$(location @linux_chrome//:chrome-linux64/chrome)",
            "-Dwebdriver.gecko.driver=$(location @linux_geckodriver//:geckodriver)",
            "-Dwebdriver.firefox.bin=$(location @linux_firefox//:firefox/firefox)",
        ],
        "//common:macos": [
            "-Dwebdriver.chrome.driver=$(location @mac_chromedriver//:chromedriver)",
            "-Dwebdriver.chrome.binary=$(location @mac_chrome//:Chrome.app)/Contents/MacOS/Chrome",
            "-Dwebdriver.gecko.driver=$(location @mac_geckodriver//:geckodriver)",
            "-Dwebdriver.firefox.bin=$(location @mac_firefox//:Firefox.app)/Contents/MacOS/firefox",
        ],
        "//conditions:default": [],
    }),
    main_class = "org.openqa.selenium.grid.Bootstrap",
    visibility = [
        "//:__pkg__",
    ],
    runtime_deps = [":grid"],
)

BASE_COMMAND_SRCS = [
    "TemplateGridCommand.java",
    "TemplateGridServerCommand.java",
]

java_library(
    name = "base-command",
    srcs = BASE_COMMAND_SRCS,
    visibility = [
        "//java/src/org/openqa/selenium/grid:__subpackages__",
    ],
    deps = [
        "//java/src/org/openqa/selenium:core",
        "//java/src/org/openqa/selenium/cli",
        "//java/src/org/openqa/selenium/grid/config",
        "//java/src/org/openqa/selenium/grid/log",
        "//java/src/org/openqa/selenium/grid/server",
        "//java/src/org/openqa/selenium/netty/server",
        "//java/src/org/openqa/selenium/remote/http",
        artifact("com.beust:jcommander"),
        artifact("com.google.guava:guava"),
    ],
)

# Generated from: bazel query 'kind(maven_publish, set(//java/... //third_party/...))' | sort
# with the selenium rc, bom dependencies (naturally), and CDP implementations removed.
maven_bom(
    name = "bom",
    bom_pom_template = "bom-pom-template.xml",
    dependencies_maven_coordinates = "org.seleniumhq.selenium:selenium-dependencies-bom:%s" % SE_VERSION,
    dependencies_pom_template = "bom-pom-template.xml",
    java_exports = [
        "//java/src/org/openqa/selenium/chrome:chrome",
        "//java/src/org/openqa/selenium/chromium:chromium",
        "//java/src/org/openqa/selenium/edge:edge",
        "//java/src/org/openqa/selenium/firefox:firefox",
        "//java/src/org/openqa/selenium/grid/sessionmap/jdbc:jdbc",
        "//java/src/org/openqa/selenium/grid/sessionmap/redis:redis",
        "//java/src/org/openqa/selenium/grid:grid",
        "//java/src/org/openqa/selenium/ie:ie",
        "//java/src/org/openqa/selenium/json:json",
        "//java/src/org/openqa/selenium/manager:manager",
        "//java/src/org/openqa/selenium/remote/http:http",
        "//java/src/org/openqa/selenium/remote:remote",
        "//java/src/org/openqa/selenium/safari:safari",
        "//java/src/org/openqa/selenium/support:support",
        "//java/src/org/openqa/selenium:client-combined",
        "//java/src/org/openqa/selenium:core",
    ] + CDP_DEPS,
    maven_coordinates = "org.seleniumhq.selenium:selenium-bom:%s" % SE_VERSION,
    tags = [
        "release-artifact",
    ],
)

java_export(
    name = "grid",
    srcs = glob(
        ["*.java"],
        exclude = BASE_COMMAND_SRCS,
    ),
    maven_coordinates = "org.seleniumhq.selenium:selenium-grid:%s" % SE_VERSION,
    opens_to = [
        "org.seleniumhq.selenium.json",
    ],
    pom_template = "//java/src/org/openqa/selenium:template-pom",
    tags = [
        "release-artifact",
    ],
    uses = [
        "org.openqa.selenium.WebDriverInfo",
        "org.openqa.selenium.cli.CliCommand",
        "org.openqa.selenium.grid.config.HasRoles",
        "org.openqa.selenium.remote.locators.CustomLocator",
        "org.openqa.selenium.remote.service.DriverService$Builder",
    ],
    visibility = [
        "//visibility:public",
    ],
    runtime_deps = [
        "//java/src/org/openqa/selenium/chrome",
        "//java/src/org/openqa/selenium/edge",
        "//java/src/org/openqa/selenium/events/local",
        "//java/src/org/openqa/selenium/events/zeromq",
        "//java/src/org/openqa/selenium/firefox",
        "//java/src/org/openqa/selenium/grid/commands",
        "//java/src/org/openqa/selenium/grid/distributor/httpd",
        "//java/src/org/openqa/selenium/grid/node/httpd",
        "//java/src/org/openqa/selenium/grid/router/httpd",
        "//java/src/org/openqa/selenium/grid/session",
        "//java/src/org/openqa/selenium/grid/sessionmap/httpd",
        "//java/src/org/openqa/selenium/grid/sessionqueue/httpd",
        "//java/src/org/openqa/selenium/ie",
        "//java/src/org/openqa/selenium/remote",
        "//java/src/org/openqa/selenium/safari",
        "//javascript/grid-ui:react_jar",
    ],
    deps = [
        ":base-command",
        "//java/src/org/openqa/selenium/cli",
        "//java/src/org/openqa/selenium/grid/config",
    ],
)

merge_jars(
    name = "add-build-info",
    inputs = [
        ":selenium_server_deploy.jar",
        "//java/src/org/openqa/selenium:manifest",
    ],
)

java_binary(
    name = "selenium_server",
    main_class = "org.openqa.selenium.grid.Bootstrap",
    visibility = [
        "//:__pkg__",
        "//deploys/docker:__pkg__",
        "//java/test:__subpackages__",
        "//javascript:__subpackages__",
        "//py:__pkg__",
        "//rb:__subpackages__",
    ],
    runtime_deps = [
        ":grid",
        artifact("org.slf4j:slf4j-jdk14"),
    ],
)

copy_file(
    name = "server-binary",
    src = ":selenium_server_deploy.jar",
    out = "selenium-server-%s.jar" % SE_VERSION,
)

java_dist_zip(
    name = "server-zip",
    files = [
        ":server-binary",
        "//:license",
        "//java:CHANGELOG",
    ],
    third_party_prefixes = [
        "@maven//",
        "//third_party",
    ],
    deps = [
        ":grid",
    ],
)

javadoc(
    name = "all-javadocs",
    third_party_prefixes = [
        "@",
        "//third_party",
    ] + CDP_DEPS,
    transitive = True,
    deps = [
        ":grid",
        "//java/src/org/openqa/selenium:client-combined",
    ],
)
