# Copyright 2021-2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_go//go:go_library.bzl", "go_library")
load("@rules_go//go:go_test.bzl", "go_test")
load("//private:package_features.bzl", "PACKAGE_FEATURES")

package(
    default_applicable_licenses = ["//:license"],
    default_visibility = ["//visibility:private"],
    features = PACKAGE_FEATURES,
)

licenses(["notice"])

go_library(
    name = "elisp",
    srcs = [
        "builtin.go",
        "extension.go",
        "feature.go",
        "generate.go",
        "imports.go",
        "language.go",
        "loadpath.go",
        "resolve.go",
        "source.go",
    ],
    embedsrcs = ["builtin_features.json"],
    importpath = "github.com/phst/rules_elisp/gazelle/elisp",
    visibility = [
        "//dev:__pkg__",
        "//gazelle:__pkg__",
    ],
    deps = [
        "@gazelle//config",
        "@gazelle//label",
        "@gazelle//language",
        "@gazelle//pathtools",
        "@gazelle//repo",
        "@gazelle//resolve",
        "@gazelle//rule",
    ],
)

TEST_DATA = [
    # keep sorted
    "//gazelle:gazelle",
    "//gazelle/testdata:BUILD",
]

go_test(
    name = "elisp_test",
    size = "medium",
    timeout = "short",
    srcs = [
        "generate_test.go",
        "imports_test.go",
        "integration_test.go",
        "resolve_test.go",
    ],
    args = [
        # See https://github.com/bazelbuild/bazel/issues/12313 why we need to
        # add additional quoting.
        shell.quote("--{t}=$(rlocationpath {t})".format(t = t))
        for t in TEST_DATA
    ],
    data = TEST_DATA + ["//gazelle/testdata"],
    deps = [
        ":elisp",
        "//private/testutil",
        "@com_github_bazelbuild_buildtools//build",
        "@com_github_google_go_cmp//cmp",
        "@gazelle//config",
        "@gazelle//label",
        "@gazelle//language",
        "@gazelle//repo",
        "@gazelle//resolve",
        "@gazelle//rule",
        "@gazelle//testtools",
    ],
)

copy_file(
    name = "copy_builtin_features",
    src = ":builtin_features",
    out = "builtin_features.json",
)

alias(
    name = "builtin_features",
    actual = select({
        "@platforms//os:windows": "@emacs_windows_30//:builtin_features",
        "//conditions:default": "@emacs_30//:builtin_features",
    }),
)
