Add source code to Xcode target programmatically
Asked Answered
L

3

9

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

Lowgrade answered 3/8, 2017 at 15:3 Comment(0)
H
4

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

Herpetology answered 3/8, 2017 at 15:15 Comment(1)
Probably that's a good solution but too much for me. I mean bringing ruby gem dependency in iOS project which even doesn't use CocoaPods (because of Carthage). Simpler way is to improve project's README, where to clarify how to add files manually.Lowgrade
M
2

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.

Megalocardia answered 30/10, 2018 at 12:12 Comment(0)
N
0

You can do this via Xcode "Build Rules"

Check this project which leverage "Build Rules" to generate source code files and add them to the project from protobuf

https://github.com/microsoft/plcrashreporter

Naseby answered 19/4, 2021 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.