Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add source code to Xcode target programmatically

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?)?

like image 534
romanilchyshyn Avatar asked Aug 03 '17 15:08

romanilchyshyn


2 Answers

CocoaPods uses this project to modify project files: https://github.com/CocoaPods/Xcodeproj

like image 85
Lou Franco Avatar answered Oct 24 '22 01:10

Lou Franco


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.

like image 25
Tobi Avatar answered Oct 24 '22 00:10

Tobi