# 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"])

ts_library(
    name = "update",
    srcs = glob(
        include = ["**/*.ts"],
        exclude = [
            "**/*_spec.ts",
            "**/*_benchmark.ts",
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ) + [
        "//packages/schematics/update:update/schema.ts",
        "//packages/schematics/update:migrate/schema.ts",
    ],
    deps = [
        "//packages/angular_devkit/core",
        "//packages/angular_devkit/schematics",
        "//packages/angular_devkit/schematics/tasks",
        "@npm//@types/node",
        "@npm//@types/npm-package-arg",
        "@npm//@types/semver",
        "@npm//npm-package-arg",
        "@npm//semver",
    ],
)

ts_json_schema(
    name = "update_schema",
    src = "update/schema.json",
)

ts_json_schema(
    name = "migrate_schema",
    src = "migrate/schema.json",
)

ts_library(
    name = "update_test_lib",
    testonly = True,
    srcs = glob(
        include = ["**/*_spec.ts"],
        exclude = [
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ),
    data = glob(
        include = [
            "**/*.json",
        ],
        exclude = [
            # NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
            "node_modules/**",
        ],
    ),
    # @external_begin
    deps = [
        ":update",
        "//packages/angular_devkit/core",
        "//packages/angular_devkit/schematics",
        "//packages/angular_devkit/schematics/testing",
        "@npm//@types/semver",
        "@npm//@yarnpkg/lockfile",
        "@npm//ini",
        "@npm//pacote",
        "@npm//rxjs",
    ],
    # @external_end
)

jasmine_node_test(
    name = "update_test",
    srcs = [":update_test_lib"],
    # TODO: Audit tests to determine if tests can be run in RBE environments
    local = True,
    deps = [
        "@npm//jasmine",
        "@npm//npm-registry-client",
        "@npm//source-map",
    ],
)

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

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