load("@crates//:defs.bzl", "all_crate_deps")
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rustfmt_config")

rustfmt_config(
    name = "enable-rustfmt",
)

# We want the release versions of Selenium to include the prebuilt
# binaries, but if we're doing day-to-day dev work, then we should
# use a local build, unless on we're on Windows, where for some
# reason we're not able to build locally.
#
# We tag the compiled versions as `manual` so that when we do a
# `bazel build //...` we don't do any additional work

# Start with the variants for each platform
alias(
    name = "selenium-manager-windows",
    actual = select({
        "//common:windows": ":selenium-manager",
        "//conditions:default": "@download_sm_windows//file",
    }),
    tags = [
        "manual",
    ],
    visibility = [
        "//common/manager:__pkg__",
    ],
)

alias(
    name = "selenium-manager-macos",
    actual = select({
        "//common:macos": ":selenium-manager",
        "//conditions:default": "@download_sm_macos//file",
    }),
    tags = [
        "manual",
    ],
    visibility = [
        "//common/manager:__pkg__",
    ],
)

alias(
    name = "selenium-manager-linux",
    actual = select({
        "//common:linux": ":selenium-manager",
        "//conditions:default": "@download_sm_linux//file",
    }),
    tags = [
        "manual",
    ],
    visibility = [
        "//common/manager:__pkg__",
    ],
)

filegroup(
    name = "selenium-manager-dev",
    srcs = [
        ":selenium-manager-linux",
        ":selenium-manager-macos",
        ":selenium-manager-windows",
    ],
    tags = [
        "manual",
    ],
    visibility = [
        "//common/manager:__subpackages__",
    ],
)

rust_binary(
    # Yes, this name is very similar to the library. Note the dash
    # instead of an underscore
    name = "selenium-manager",
    srcs = ["src/main.rs"],
    edition = "2021",
    version = "0.4.34",
    visibility = ["//visibility:public"],
    deps = [
        ":selenium_manager",
    ] + all_crate_deps(normal = True),
)

rust_library(
    # The name here is used as the crate name
    name = "selenium_manager",
    srcs = glob(
        ["src/**/*.rs"],
        exclude = ["main.rs"],
    ),
    edition = "2021",
    visibility = ["//rust:__subpackages__"],
    deps = all_crate_deps(normal = True),
)

filegroup(
    name = "selenium_manager_srcs",
    srcs = [
        "Cargo.lock",
        "Cargo.toml",
        ":selenium_manager_rs_srcs",
    ],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "selenium_manager_rs_srcs",
    srcs = glob(["src/**/*.rs"]),
)

rust_test(
    name = "unit",
    size = "small",
    crate = ":selenium_manager",
    edition = "2021",
    tags = ["no-sandbox"],
)
