• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15"""Utility for generating TS code with string replacement."""
16
17load("@build_bazel_rules_nodejs//:providers.bzl", "run_node")
18
19def _template_replacement_impl(ctx):
20    output_file = ctx.actions.declare_file(ctx.attr.output_file)
21    descriptor_data = ctx.files.descriptor_data[0]
22    template_file = ctx.files.template_file[0]
23
24    run_node(
25        ctx,
26        executable = "_template_replacement_bin",
27        inputs = [descriptor_data, template_file],
28        outputs = [output_file],
29        arguments = [
30            "--template",
31            template_file.path,
32            "--descriptor_data",
33            descriptor_data.path,
34            "--output",
35            output_file.path,
36            "--proto_root_dir",
37            ctx.attr.proto_root_dir,
38        ],
39    )
40
41    return [DefaultInfo(files = depset([output_file]))]
42
43template_replacement = rule(
44    implementation = _template_replacement_impl,
45    attrs = {
46        "_template_replacement_bin": attr.label(
47            executable = True,
48            cfg = "exec",
49            default = Label("@//pw_protobuf_compiler/ts/codegen:template_replacement_bin"),
50        ),
51        "descriptor_data": attr.label(
52            allow_files = [".proto.bin"],
53        ),
54        "proto_root_dir": attr.string(mandatory = True),
55        "output_file": attr.string(mandatory = True),
56        "template_file": attr.label(
57            allow_files = [".ts"],
58            default = Label("@//pw_protobuf_compiler/ts:ts_proto_collection_template"),
59        ),
60    },
61)
62