aboutsummaryrefslogtreecommitdiff
path: root/pw_protobuf_compiler/ts/ts_proto_collection.bzl
blob: fe57e28c545d40ecfa65f07cef5f759d17740c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright 2021 The Pigweed Authors
#
# 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.

"""Builds a proto collection."""

load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("@//pw_protobuf_compiler/ts/codegen:template_replacement.bzl", "template_replacement")

def _lib(name, proto_library, js_proto_library):
    js_proto_library_name = Label(js_proto_library).name
    proto_root_dir = js_proto_library_name + "/" + js_proto_library_name + "_pb"
    template_name = name + "_template"
    template_replacement(
        name = template_name,
        descriptor_data = proto_library,
        proto_root_dir = proto_root_dir,
        output_file = "generated/ts_proto_collection.ts",
    )

    ts_library(
        name = name + "_lib",
        package_name = name,
        module_name = name,
        srcs = [template_name],
        deps = [
            js_proto_library,
            "@//pw_protobuf_compiler/ts:pw_protobuf_compiler",
            "@//pw_rpc:packet_proto_tspb",
            "@npm//@types/google-protobuf",
            "@npm//@types/node",
            "@npm//base64-js",
        ],
    )

    native.filegroup(
        name = name + "_esm",
        srcs = [name + "_lib"],
        output_group = "es6_sources",
    )

def ts_proto_collection(name, proto_library, js_proto_library):
    _lib(name, proto_library, js_proto_library)
    js_library(
        name = name,
        package_name = "@pigweed/" + name + "/pw_protobuf_compiler",
        deps = [name + "_lib", name + "_esm"],
    )