load("@crates//:defs.bzl", "aliases", "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__",
    ],
)

alias(
    name = "rust",
    actual = ":selenium_manager",
    visibility = ["//visibility:public"],
)

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"],
    aliases = aliases(),
    edition = "2024",
    # clap's `#[clap(about)]` derives the CLI description from
    # CARGO_PKG_DESCRIPTION. Cargo sets this from Cargo.toml automatically, but
    # the Bazel build must supply it explicitly. Keep in sync with rust/Cargo.toml.
    rustc_env = {
        "CARGO_PKG_DESCRIPTION": "Selenium Manager is a CLI tool that automatically manages the browser/driver resources required by Selenium.",
    },
    version = "0.4.46",
    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"],
    ),
    aliases = aliases(),
    compile_data = [
        "src/resources/rules.md",
        "src/resources/skills.md",
    ],
    crate_features = select({
        "//common:stamp": [],
        "//conditions:default": ["avoid_stats"],
    }),
    edition = "2024",
    visibility = ["//rust:__subpackages__"],
    deps = all_crate_deps(normal = True),
)

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

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

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