Run a shell script before build in Xcode

后端 未结 2 631
北荒
北荒 2021-01-02 07:24

I need to adjust my build and version number for my project before build/archiving.
I tried multiple things, but so far to no avail.

I added a target with the sc

相关标签:
2条回答
  • 2021-01-02 07:48

    Xcode has command line tools for build/archiving: https://developer.apple.com/library/ios/technotes/tn2339/_index.html

    So, you can write shell script that at first runs your script for adjusting build/version number and then runs xcode build/archive as command line tool.

    0 讨论(0)
  • 2021-01-02 08:05

    agvtools just does not work in an Xcode build. It will always stop the build. What works fine is PlistBuddy, although the setup is not as nice and neat.

    I added a Pre-Action to the build in my main scheme to call a new target in my project:

    xcodebuild -project "${SRCROOT}/MAIN_APP.xcodeproj" -scheme BuildNumberPreProcess
    

    In the target BuildNumberPreProcess I have a Run Script:

    VERSION=$(head -n 1 version.txt)
    BUILD=`git rev-list $(git rev-parse --abbrev-ref HEAD) | wc -l | awk '{ print $1 }'`
    
    echo "${VERSION} (${BUILD})"
    
    SCRIPT="${SRCROOT}/CLIENT/Supporting Files/set-version-in-plist.sh"
    
    "${SCRIPT}" "${SRCROOT}/MAIN_APP/Supporting Files/Info.plist" ${VERSION} ${BUILD}
    "${SCRIPT}" "${SRCROOT}/EXTENSION/Info.plist" ${VERSION} ${BUILD}
    ...
    

    set-version-in-plist.h:

    #!/bin/sh
    
    #  set-version-in-plist.sh
    #
    # usage:
    # set-version-in-plist LIST VERSION BUILD
    # LIST:      Info.plist path & name
    # VERSION:   version number xxx.xxx.xxx
    # BUILD:     build number xxxxx
    #
    
    # Location of PlistBuddy
    PLISTBUDDY="/usr/libexec/PlistBuddy"
    
    echo "$1: $2 ($3)"
    
    ${PLISTBUDDY} -c "Set :CFBundleShortVersionString $2" "$1";
    ${PLISTBUDDY} -c "Set :CFBundleVersion $3" "$1";
    
    0 讨论(0)
提交回复
热议问题