9

In order to put in place a continuous integration system, Hudson, I wrote a bash script to build Xcode project automatically. Moreover, in Debug configuration, It was asked to me, to insert the svn revision number of the project in the CFBundleRevision field of the PROJECT-Info.plist file as ${BUNDLE_VERSION}.r${SVN_REVISION}.

You'll find the source code of PROJECT-Info.plist file below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
[...]
    <key>CFBundleVersion</key>
    <string>1.0</string>
[...]
</dict>
</plist>

I tried this bash script below :

sed 'N;s_^.*<key>CFBundleVersion</key>.*<string>[0-9][0-9]*\.[0-9][0-9]*</string>$_<key>CFBundleVersion</key>\
<string>'"$BUNDLE_VERSION"'\.r'"$SVN_REVISION"'</string>_' $PROJECT-Info.plist

This script should replace the "1.0" string with ${BUNDLE_VERSION}.r${SVN_REVISION} (just in standard output currently). However, the replacement works without the 'N' option which includes the next line in the sed process and for one line at a time. But there is many line with "<string>[...]</string>" string in the PROJECT-Info.plist file...

I think it's my way of processing the unknown characters between the two lines ('N' option and ".*" for any characters) is wrong.

Any idea ?

Thanks in advance and sorry for my bad level in English.

Doc_1faux
  • 141
  • 5
  • 15
  • You need to use a proper XML tool such as a Python or Perl module that's designed for the purpose of manipulating the keys and values in an XML file. There are also shell utilities available such as xmlstarlet. [Regexes are not up to the challenge](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). By the way, your English is very nearly perfect. – Dennis Williamson Mar 14 '11 at 15:31
  • Thank you for the [xmlstarlet](http://xmlstar.sourceforge.net/) shell utility solution. I downloaded and installed it. I'll test it tomorrow and I'll give you my feedback. – Doc_1faux Mar 14 '11 at 17:58

3 Answers3

29

Use PlistBuddy:

# cf. http://davedelong.com/blog/2009/04/15/incrementing-build-numbers-xcode
/usr/libexec/PlistBuddy -h
/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" test.plist
myversion=1.0.5
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion '${myversion}'" test.plist
tognum
  • 291
  • 2
  • 2
  • Thank you. This is the proper way to do it. The other answer is a very unnecessary hack. – chown Mar 11 '12 at 03:29
7

In this specific case you can also use Xcode's agvtool. You do not even need to provide the path to the PROJECT-Info.plist file. Inside your project dir run:

agvtool new-version -all "$BUILD_NUMBER" # sets CFBundleVersion
agvtool new-marketing-version "$BUNDLE_VERSION" # sets CFBundleShortVersionString
Eel Lee
  • 3,513
  • 2
  • 31
  • 49
tfischbach
  • 3,003
  • 3
  • 22
  • 16
0
$ myversion=1.0.3
$ perl -O777 -i.bak -pe 's|<key>CFBundleVersion</key>\\s*<string>[\d.]*</string>|<key>CFBundleVersion</key></key>'"$myversion"'<string>|' bundle

Moves the file bundle to bundle.bak, and replaces 1.0.6.9 with 1.0.3 in the new bundle file.

bobbogo
  • 14,989
  • 3
  • 48
  • 57
  • I'll try this solution first when I'll go back to work tomorrow. Thanks :) – Doc_1faux Mar 14 '11 at 19:49
  • I'm so curious I tried immediately and it works well ! I just modify this command line as below because I make a backup of the .plist file earlier in my script and I restore it at the end. `perl -0777 -pe "s|CFBundleVersion\s*[\d.]*|CFBundleVersion$BUNDLE_VERSION.r$REVISION|" PROJECT-Info.plist.bak > PROJECT-Info.plist` Thank to both of you. – Doc_1faux Mar 14 '11 at 20:51