• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2022 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
15import { exec, ExecException } from 'child_process';
16import fs from 'fs';
17
18const run = function (executable: string, args: string[]) {
19  console.log(args);
20  return new Promise<void>((resolve) => {
21    exec(
22      `${executable} ${args.join(' ')}`,
23      { cwd: process.cwd() },
24      (error: ExecException | null, stdout: string | Buffer) => {
25        if (error) {
26          throw error;
27        }
28
29        console.log(stdout);
30        resolve();
31      },
32    );
33  });
34};
35
36const protos = [
37  'pw_transfer/transfer.proto',
38  'pw_rpc/ts/test.proto',
39  'pw_rpc/echo.proto',
40  'pw_protobuf/pw_protobuf_protos/status.proto',
41  'pw_protobuf/pw_protobuf_protos/common.proto',
42  'pw_tokenizer/pw_tokenizer_proto/options.proto',
43  'pw_log/log.proto',
44  'pw_rpc/ts/test2.proto',
45  'pw_rpc/internal/packet.proto',
46  'pw_protobuf_compiler/pw_protobuf_compiler_protos/nested/more_nesting/test.proto',
47  'pw_protobuf_compiler/pw_protobuf_compiler_protos/test.proto',
48];
49
50// Replace these import statements so they are actual paths to proto files.
51const remapImports = {
52  'pw_protobuf_protos/common.proto':
53    'pw_protobuf/pw_protobuf_protos/common.proto',
54  'pw_tokenizer_proto/options.proto':
55    'pw_tokenizer/pw_tokenizer_proto/options.proto',
56};
57
58// Only modify the .proto files when running this builder and then restore any
59// modified .proto files to their original states after the builder has finished
60// running.
61const restoreProtoList = [];
62protos.forEach((protoPath) => {
63  const protoData = fs.readFileSync(protoPath, 'utf-8');
64  let newProtoData = protoData;
65  Object.keys(remapImports).forEach((remapImportFrom) => {
66    if (protoData.indexOf(`import "${remapImportFrom}"`) !== -1) {
67      newProtoData = newProtoData.replaceAll(
68        remapImportFrom,
69        remapImports[remapImportFrom],
70      );
71    }
72  });
73  if (protoData !== newProtoData) {
74    restoreProtoList.push([protoPath, protoData]);
75    fs.writeFileSync(protoPath, newProtoData);
76  }
77});
78
79run(
80  'ts-node',
81  [`./pw_protobuf_compiler/ts/build.ts`, `--out dist/protos`].concat(
82    protos.map((proto) => `-p ${proto}`),
83  ),
84).then(() => {
85  restoreProtoList.forEach((restoreProtoData) => {
86    fs.writeFileSync(restoreProtoData[0], restoreProtoData[1]);
87  });
88});
89