pachanga
pachanga

Reputation: 3073

Xcode4 : custom task for copying resources to the bundle

Folks, is it possible to create a custom task for copying resources in Xcode?

I'd like to use rsync instead of Xcode's built-in resource copy tasks. From time to time Xcode for some reason doesn't copy modified resources to the bundle so I'd like to have a better control over this task.

I think I need some magic variable which points to the destination bundle directory, so I could create the following task:

rsync -a ${SRCROOT}/resources ${PATH_TO_THE_BUNDLE}/

...but I have no idea if there is such a variable.

Update: I tried ${BUILT_PRODUCTS_DIR} variable and it points to some intermediate directory while during the runtime [[NSBundle mainBundle] pathForResource: my_path ofType: nil] returns something completely different. Is there any Xcode variable which which returns the same result as NSBundle does?

Upvotes: 1

Views: 931

Answers (2)

Wilbo Baggins
Wilbo Baggins

Reputation: 2751

To do this on a per-directory basis:

  1. Add a 'Run Script Build Phase' (Editor > Add Build Phase > Add Run Script Build Phase)
  2. Copy/paste the following script into the build phase (don't forget to set the actual paths on line 1):

dirsToCopy=("path1/directory1","path2/directory2")

for INPATTERN in "${dirsToCopy[@]}"; do
    INDIR=$PROJECT_DIR/$INPATTERN/*
    OUTDIR=$TARGET_BUILD_DIR/$CONTENTS_FOLDER_PATH/$INPATTERN
    cp -R $INDIR $OUTDIR
done

For those not used to working with shell scripts:

  • paths on line 1 of the script are relative to the project directory (where the .xcodeproj file resides)

  • you can add more (or less) paths to the array dirsToCopy if you want

  • you can change the pattern of files to copy where variable INDIR is defined

  • you can change how the copying is done (currently, recursive due to -R) by changing the flags to cp in the last line of script within the for block.

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

Yes... it sounds like you already know how to add a "Run Script" action to a build scheme, and the magic variables you may want are either:

$SRCROOT -- the source tree's root directory

$BUILT_PRODUCTS_DIR -- the convoluted path to the destination where the built product eventually arrives at (i.e. and yes, this variable's contents changes slightly depending on whether you are building a Mac app, or an iPhone app targeting the simulator or the device)

So... it sounds like what you want is "$BUILT_PRODUCTS_DIR/Contents/Resources" in your build script?

Upvotes: 2

Related Questions