Search code examples
shprotocgrpc-web

Protobuf Build Protoc select all .proto files in a directory


I have the following .sh file that I run to build my protos.

mkdir -p ./src/gen

protoc -I=../protos/                                       \
       ../protos/common/*.proto                            \
       ../protos/service/protos/*.proto                    \
       ../protos/ui/*.proto                                \
       ../protos/*.proto                                   \
       --js_out=import_style=commonjs,binary:./src/gen     \
       --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen

It works great, however, my team might add other directories to the protos directory that this file doesn't see.

Is there a way for me to update this script to get all .proto files in the protos directory?

I've tried the following but it didn't work:

mkdir -p ./src/gen

protoc -I=../protos/**/*.proto                                             \
       --js_out=import_style=commonjs,binary:./src/gen                     \
       --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen

I'm sure it's just some syntax issue, but I'm not sure how to do it.

EDIT:

I've implemented something to make my generators more flexible, but it's still messy:

mkdir -p ./src/gen

protoc -I=../protos/                                   \
       ../protos/*.proto                               \
       ../protos/**/*.proto                            \
       ../protos/**/**/*.proto                         \
       ../protos/**/**/**/*.proto                      \
       ../protos/**/**/**/**/*.proto                   \
       --js_out=import_style=commonjs,binary:./src/gen \
       --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:./src/gen

Solution

  • Since your script will pass the complete path for every file, it should be enough to add the protos/ folder with -I=../protos. The code below works fine for me, with multiple proto files in different subfolders.

    $ tree ../protos
    ../protos
    ├── a
    │   └── a.proto
    └── b
        ├── b.proto
        └── c
            └── c.proto
    
    $ protoc \
      -I=../protos \
      ../protos/**/*.proto \
      --js_out=import_style=commonjs,binary:./src/gen
    
    $ tree src/gen
    src/gen
    ├── a
    │   └── a_pb.js
    └── b
        ├── b_pb.js
        └── c
            └── c_pb.js
    
    3 directories, 3 files
    

    In your original question, you may just have mixed up ../protos/*/**.proto with ../protos/**/*.proto.