Automate the process of including generated proto files into an Xcode project

723 views Asked by At

Our iOS app currently is using Google protobuffer gRPC as our API layer to communicate between App and backend. And so we have these .proto files in our backend directory which will be converting to .grpc.swift and .pb.swift files by gRPC-Swift-Plugins and then consumed by the App.

This works okay, but the process of converting is very tedious, and we would like to automate the whole process.

Below is how we're doing it:
  1. Delete previously copied directory, and copy all .proto files from backend (.proto files are maintained by backend devs) to App directory named "Protos" via a shell script
  2. We already set up Build rules and include .proto files in Compile Sources. Following the steps from an answer here on SO

Screenshot of the current setup in Xcode Build Rules: enter image description here

  1. Whenever we build the project, .pb.swift and .grpc.swift are generated and putting into a directory named "generated" under the "Protos" folder.

Here are the problems:

  1. If the backend added a new .proto files into the source directory, my script will only copy the files into the Protos directory but not included news files in the Compile Sources list.
  2. Similar to the first problem, we need to manually set up Compile Sources in Xcode and that means if a new dev joins our team, he/she also needs to do the same setup again.
  3. We sometimes need to refer to the .grpc.swift and .ph.swift files while coding. But If we add these files into Xcode and build the project again, Xcode will complain that these generated files are there like (Sorry, we're working on a private repo, so the project name and file names are replaced):

Multiple commands produce '${user_path}/${proto_name}.pb.o':

  1. Target '${my_project_name}' (project '${my_project_name}') has compile command for Swift source files
  2. Target '${my_project_name}' (project '${my_project_name}') has compile command for Swift source files

Any answers or comments and suggestions are greatly appreciated!

1

There are 1 answers

0
xarly On

It's interesting I didn't have those problems with Swift if I use the $DERIVED_FILE_DIR

protoc "$INPUT_FILE_NAME" --swift_out="$DERIVED_FILE_DIR" --proto_path="Your/proto/path"

I don't use the plugin because I've got the plugin installed in my /usr/local/bin

But I have exactly those problems when we use the output for cpp files.