kraftydevil
kraftydevil

Reputation: 5246

Handling spaces & special characters inside an Xcode Build Phase Bash Run Script

I have a custom Bash Run Script in my Xcode project's Build Phases. It writes the official version numbers to the plist.

#proper escape for spaces
TARGET_BUILD_DIR=${TARGET_BUILD_DIR//" "/"\ "}

echo "Setting marketing version, CFBundleShortVersionString, to $VERSION in $TARGET_BUILD_DIR/$INFOPLIST_PATH..."

/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $VERSION" $TARGET_BUILD_DIR/$INFOPLIST_PATH

echo "Setting technical version, CFBundleVersion, to $BUILD in $TARGET_BUILD_DIR/$INFOPLIST_PATH..."

/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD" $TARGET_BUILD_DIR/$INFOPLIST_PATH

This works fine whenever the path has no spaces or special characters.

Here is what happens when attempting to call this code:

Setting marketing version, CFBundleShortVersionString, to 1.7.0.0 in /Users/jenkins/.jenkins/jobs/ClientName/jobs/iOS/jobs/What's Up/workspace/build/WhatsUp.app/Info.plist... File Doesn't Exist, Will Create: /Users/jenkins/.jenkins/jobs/ClientName/jobs/iOS/jobs/What Invalid Arguments

Setting technical version, CFBundleVersion, to 199 in /Users/jenkins/.jenkins/jobs/ClientName/jobs/iOS/jobs/What's Up/workspace/build/WhatsUp.app/Info.plist... File Doesn't Exist, Will Create: /Users/jenkins/.jenkins/jobs/ClientName/jobs/iOS/jobs/What Invalid Arguments

The echo picks up the directory just fine, but PlistBuddy returns an error when trying to use the directory in question.

So far I have tried a number of things like escaping spaces and special characters with '\':

#replace spaces attempt
TARGET_BUILD_DIR=${TARGET_BUILD_DIR//" "/"\ "}
TARGET_BUILD_DIR=${TARGET_BUILD_DIR// /\ }

#replace apostrophes attempt
TARGET_BUILD_DIR=${TARGET_BUILD_DIR//"'"/"\'"}
TARGET_BUILD_DIR=${TARGET_BUILD_DIR//'/\'}

What do I have to do to get PlistBuddy to accept a directory with a space or special character?

Upvotes: 3

Views: 2487

Answers (1)

kraftydevil
kraftydevil

Reputation: 5246

It turns out that replacing spaces and special characters wasn't needed.

@Etan Reisner's comment about quoting variable expansions made me try all sorts of other things having to do with that.

Quoting "$TARGET_BUILD_DIR" and "$INFOPLIST" individually did the trick:

Setting marketing version, CFBundleShortVersionString, to $VERSION in $TARGET_BUILD_DIR/$INFOPLIST_PATH..."
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $VERSION" "$TARGET_BUILD_DIR"/"$INFOPLIST_PATH"

Setting technical version, CFBundleVersion, to $BUILD in $TARGET_BUILD_DIR/$INFOPLIST_PATH..."
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD" "$TARGET_BUILD_DIR"/"$INFOPLIST_PATH"

Upvotes: 3

Related Questions