Add files to an Xcode project from a script?

Solution 1:

I had a similar need as Andrew. I needed to be able to include resources from a script without knowing those resources ahead of time. Here's the solutions I came up with:

Add a new Run Script build phase after “Copy Bundle Resource” that contains the following command:

find -L ${SRCROOT}/SomeDerivedResources \
    -type f -not -name ".*" \
    -not -name "`basename ${INFOPLIST_FILE}`" \
    | xargs -t -I {} \
    cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/

Looks scary, but let’s break it down:

find -L ${SRCROOT}/SomeDerivedResources

This crawls the directory SomeDerivedResources in our source root (-L tells it to follow symbolic links)

-type f

Only include regular files

-not -name ".*"

Ignore files starting with a dot

-not -name "`basename ${INFOPLIST_FILE}`"

In my case, my Info plists live in my SomeDerivedResources directory so we need to exclude that file from being copied to our product

| xargs -t -I {}

Pipe the results of find into xargs with -t (echo resulting commands to stderr so they show up in our build log), -I (run the command once for each input file) and use {} as our argument placeholder

cp {} ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/

Lastly, copy each found file (denoted by {}) to our product’s resource directory.

I realized when typing this that using an rsync setup instead of cp could prevent us from copying resources each time you build. If your resources are very large it might be worth looking in to.

(Also, a folder reference wouldn’t work for my need for a few reasons. One, my icons are in my DerivedResources directory and having them in a subdirectory in the bundle seems not to work. Also, I ideally wanted to be able to use [UIImage imageNamed:@"MyAwesomeHappyImage.png"] and -pathForResource:ofType: (and some of my files are nested further inside my DerivedResources directory). If your needs don’t contain those restraints, I highly suggest you go the folder reference route.)

Solution 2:

This can be done by adding a new build phase to your application.

  1. In your Xcode project browser, find the target for your application, and expand it to show all of the build phases.

  2. Add a new "run script" build phase to your target. The easiest way is to right-click on the target and choose "Add/New Build Phase/New Run Script Build Phase"

  3. Adding the new build phase should bring up an inspector window. In this window, you can enter the entire shell script, or simply a command line to run the script.

  4. Here's the gold: At the bottom of the inspector window you can specify input files and output files. Specifying input files sets up dependencies automatically (the shell script will only be executed if some of the input files have been modified). Specifying output files automatically propagates the dependencies to those files. When your shell script is run, Xcode knows that it needs to deal with those files that the shell script has modified.

  5. Be sure to drag your new build phase up to the top of the list of phases as shown in the screenshot below. The order will be important if you need those resource files to be included in the bundle.

  6. Save, build, commit to the repository, ask for a raise, get some fresh air and have a nice day! :)