Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add files to Xcode project programmatically / from a script?

Tags:

xcode

build

Right now I'm downloading all resources and adding the references via Xcode GUI. There must be a way to automate this last step, so that a script can copying the resources to the project-directory automatically and generate the references.

In some similar questions it means to build a "new 'run script' build phase to your project." But how is the code, that adds it in a subdirectory of my project.

like image 731
ChrKahl Avatar asked Oct 31 '22 08:10

ChrKahl


2 Answers

I don't know of a tool that exists currently. I also had the need for adding files and folders programmatically to an xcode project file. It was rather involved, so I will describe the method and post a link to my project.

If you add just one file or one folder to the project file and look at the diff, you will discover that each file/folder has multiple entries in the project file that share a UUID. You have to generate a UUID and create entries in the respective subsections of the project file.

My use case was to measure if there are any differences in compilation time between projects that use swift and/or objective c files. This section of code manipulates the project file. It is quickly thrown together and won't win any awards for beauty.

like image 165
DudeOnRock Avatar answered Nov 15 '22 08:11

DudeOnRock


To finish this question: I found a solution like this:

find -L "${SRCROOT}/Resources/InitData" -type f -not -name “.*” -not -name “`basename ${INFOPLIST_FILE}`” | xargs -t -I {} cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/;
find -L "${SRCROOT}/Resources/InitData/Images" -type f -not -name “.*” -not -name “`basename ${INFOPLIST_FILE}`” | xargs -t -I {} cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/;

Added this in the build section as an initial run script worked for me.

like image 39
ChrKahl Avatar answered Nov 15 '22 09:11

ChrKahl