Add source code to Xcode target programmatically

Viewed 2378

I'm using protobuf to generate swift classes from protofiles (I've written a script for this purpose). Now I need to add all generated files into target in Xcode project.

How can I do this programmatically? Maybe script in Build Phases or some other kind of actions in macOS (Automator?)?

3 Answers

It is possible to do that with functionality built into Xcode (we have done it with c++, but it should be possible with swift, too):

In your target, add a custom shell script build rule that matches the extension of your protofiles.

Call your script using "${INPUT_FILE_PATH}" and "${SCRIPT_OUTPUT_FILE_0}".

If the output file's extension matches another build rule, that one will be executed. So in you case, set the output file of the build rule to ${DERIVED_FILE_DIR}/${INPUT_FILE_BASE}.swift in order to send it to the swift compiler.

Now go to your target's Compile Sources build phase and add the protofiles.

Related