# Copyright Google Inc. All Rights Reserved.
#
# Use of this source code is governed by an MIT-style license that can be
# found in the LICENSE file at https://angular.io/license

load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("//tools:defaults.bzl", "ts_library")
load("//tools:ts_json_schema.bzl", "ts_json_schema")

# @external_begin
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
# @external_end

licenses(["notice"])  # MIT

package(default_visibility = ["//visibility:public"])

# Create a list of Tuple("path/file.json", "path_file") to be used as rules
ALL_SCHEMA_TARGETS = [
    (
        x,
        x.replace("/", "_").replace("-", "_").replace(".json", ""),
    )
    for x in glob(
        include = ["*/schema.json"],
        exclude = [
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    )
]

# Create all the targets.
[
    ts_json_schema(
        name = name,
        src = src,
    )
    for (src, name) in ALL_SCHEMA_TARGETS
]

ts_library(
    name = "angular",
    srcs = glob(
        include = ["**/*.ts"],
        exclude = [
            "**/*_spec.ts",
            "**/*_benchmark.ts",
            # Also exclude templated files.
            "*/files/**/*.ts",
            "*/other-files/**/*.ts",
            # Exclude test helpers.
            "utility/test/**/*.ts",
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ) + [
        "//packages/schematics/angular:" + src.replace(".json", ".ts")
        for (src, _) in ALL_SCHEMA_TARGETS
    ],
    # strict_checks = False,
    data = glob(
        include = [
            "collection.json",
            "package.json",
            "migrations/migration-collection.json",
            "*/schema.json",
            "*/files/**/*",
            "*/other-files/**/*",
        ],
        exclude = [
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ),
    module_name = "@schematics/angular",
    deps = [
        "//packages/angular_devkit/core",
        "//packages/angular_devkit/schematics",
        "//packages/angular_devkit/schematics/tasks",
        "//packages/schematics/angular/third_party/github.com/Microsoft/TypeScript",
        "@npm//@types/browserslist",
        "@npm//@types/caniuse-lite",
        "@npm//@types/node",
        "@npm//jsonc-parser",
        "@npm//rxjs",
        "@npm//tslint",
    ],
)

jasmine_node_test(
    name = "no_typescript_runtime_dep_test",
    srcs = ["no_typescript_runtime_dep_spec.js"],
    deps = [
        ":angular",
        "@npm//jasmine",
    ],
)

ts_library(
    name = "angular_test_lib",
    testonly = True,
    srcs = glob(
        include = [
            "**/*_spec.ts",
            "utility/test/**/*.ts",
        ],
        exclude = [
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ),
    # @external_begin
    deps = [
        ":angular",
        "//packages/angular_devkit/core",
        "//packages/angular_devkit/core/node/testing",
        "//packages/angular_devkit/schematics",
        "//packages/angular_devkit/schematics/testing",
        "//packages/schematics/angular/third_party/github.com/Microsoft/TypeScript",
        "@npm//@types/browserslist",
        "@npm//@types/caniuse-lite",
        "@npm//jsonc-parser",
        "@npm//rxjs",
        "@npm//tslint",
    ],
    # @external_end
)

jasmine_node_test(
    name = "angular_test",
    srcs = [":angular_test_lib"],
    deps = [
        "//packages/schematics/angular/third_party/github.com/Microsoft/TypeScript",
        "@npm//jasmine",
        "@npm//source-map",
    ],
)

# @external_begin
pkg_npm(
    name = "npm_package",
    deps = [
        ":angular",
    ],
)

pkg_tar(
    name = "npm_package_archive",
    srcs = [":npm_package"],
    extension = "tar.gz",
    strip_prefix = "./npm_package",
    tags = ["manual"],
)
# @external_end
