1// Copyright 2024 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 fs from 'fs'; 16import path from 'path'; 17import * as argModule from 'arg'; 18const arg = argModule.default; 19import { buildProtos } from './build'; 20 21const args = arg({ 22 '--outputCollectionName': String, 23}); 24 25const outputCollectionName = args['--outputCollectionName'] || 'collection.ts'; 26 27const OUT_DIR = path.join(process.cwd(), 'dist', 'protos'); 28if (!fs.existsSync(OUT_DIR)) { 29 fs.mkdirSync(OUT_DIR, { recursive: true }); 30} 31 32const protos = [ 33 'pw_transfer/transfer.proto', 34 'pw_rpc/ts/test.proto', 35 'pw_rpc/echo.proto', 36 'pw_protobuf/pw_protobuf_protos/status.proto', 37 'pw_protobuf/pw_protobuf_protos/common.proto', 38 'pw_tokenizer/pw_tokenizer_proto/options.proto', 39 'pw_log/log.proto', 40 'pw_rpc/ts/test2.proto', 41 'pw_rpc/internal/packet.proto', 42 'pw_protobuf_compiler/pw_protobuf_compiler_protos/nested/more_nesting/test.proto', 43 'pw_protobuf_compiler/pw_protobuf_compiler_protos/test.proto', 44]; 45 46const remapProtos = { 47 'pw_protobuf/pw_protobuf_protos/common.proto': 48 'pw_protobuf_protos/common.proto', 49 'pw_tokenizer/pw_tokenizer_proto/options.proto': 50 'pw_tokenizer_proto/options.proto', 51}; 52 53// Only modify the .proto files when running this builder and then restore any 54// modified .proto files to their original states after the builder has finished 55// running. 56const newProtoPaths = []; 57protos.forEach((protoPath) => { 58 const protoFullPath = path.join(process.cwd(), protoPath); 59 const protoData = fs.readFileSync(protoFullPath, 'utf-8'); 60 // Remap file path 61 if (remapProtos[protoPath]) { 62 protoPath = remapProtos[protoPath]; 63 } 64 newProtoPaths.push(protoPath); 65 // Also write a copy to output directory. 66 const protoDistPath = path.join(OUT_DIR, protoPath); 67 if (!fs.existsSync(path.dirname(protoDistPath))) { 68 fs.mkdirSync(path.dirname(protoDistPath), { recursive: true }); 69 } 70 71 fs.writeFileSync(protoDistPath, protoData); 72}); 73 74buildProtos(newProtoPaths, OUT_DIR, outputCollectionName, OUT_DIR); 75