Skip to content

Instantly share code, notes, and snippets.

@mitchellporter
Forked from acrookston/README.md
Created May 30, 2019 16:44
Show Gist options
  • Save mitchellporter/f80e7a6d23b35dcc9800a4f7f93d9d33 to your computer and use it in GitHub Desktop.
Save mitchellporter/f80e7a6d23b35dcc9800a4f7f93d9d33 to your computer and use it in GitHub Desktop.

Revisions

  1. @acrookston acrookston revised this gist May 4, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ Installation procedure for pre-build actions to automatically populate Xcode Inf
    Edit Xcode Scheme and add a pre-action script.
    Copy the contents of `preaction.sh` into the pre-action script box.

    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-manage-preaction.png)
    ![](https://gist.github.com/acrookston/55d69a16cd5363426dbf7a3d6a9ee6ce/raw/4cae5919e4227b48c264cc2bcb6d0419cecf4341/xcode-manage-preaction.png)


    ### 2. Copy processing file
    @@ -22,7 +22,7 @@ Copy the contents of `preaction.sh` into the pre-action script box.
    2. For each Target Build Settings set `Preprocess Info.plist file` to YES.
    3. Enter `environment_preprocess.h` (or the output file if you renamed it `build_environment.sh`) into `Info.plist Preprocessor Prefix File`

    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-build-settings.png)
    ![](https://gist.github.com/acrookston/55d69a16cd5363426dbf7a3d6a9ee6ce/raw/4cae5919e4227b48c264cc2bcb6d0419cecf4341/xcode-build-settings.png)


    ### 4. Install plist file
    @@ -31,7 +31,7 @@ Copy `development.plist` file to `config/environments/development.plist`. DO NOT

    If you have several schemes setup in the previous step (`ENVIORMENT`) you need to copy this file to each option if you use multiple build schemes and `ENVIRONMENT` variables.

    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-user-defined-environment.png)
    ![](https://gist.github.com/acrookston/55d69a16cd5363426dbf7a3d6a9ee6ce/raw/4cae5919e4227b48c264cc2bcb6d0419cecf4341/xcode-user-defined-environment.png)

    ### 5. Setup environment variables

    @@ -50,7 +50,7 @@ TIP 2: You can use it to use git commit count as the build version/number.

    6. Use those generated environment variables in your `Info.plist`.

    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-info-plist.png)
    ![](https://gist.github.com/acrookston/55d69a16cd5363426dbf7a3d6a9ee6ce/raw/4cae5919e4227b48c264cc2bcb6d0419cecf4341/xcode-info-plist.png)

    ### 7. Run Build!

  2. @acrookston acrookston revised this gist May 4, 2016. 2 changed files with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ Installation procedure for pre-build actions to automatically populate Xcode Inf
    Edit Xcode Scheme and add a pre-action script.
    Copy the contents of `preaction.sh` into the pre-action script box.

    ![](xcode-manage-preaction.png)
    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-manage-preaction.png)


    ### 2. Copy processing file
    @@ -22,7 +22,7 @@ Copy the contents of `preaction.sh` into the pre-action script box.
    2. For each Target Build Settings set `Preprocess Info.plist file` to YES.
    3. Enter `environment_preprocess.h` (or the output file if you renamed it `build_environment.sh`) into `Info.plist Preprocessor Prefix File`

    ![](xcode-build-settings.png)
    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-build-settings.png)


    ### 4. Install plist file
    @@ -31,7 +31,7 @@ Copy `development.plist` file to `config/environments/development.plist`. DO NOT

    If you have several schemes setup in the previous step (`ENVIORMENT`) you need to copy this file to each option if you use multiple build schemes and `ENVIRONMENT` variables.

    ![](xcode-user-defined-environment.png)
    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-user-defined-environment.png)

    ### 5. Setup environment variables

    @@ -50,9 +50,9 @@ TIP 2: You can use it to use git commit count as the build version/number.

    6. Use those generated environment variables in your `Info.plist`.

    ![](xcode-info-plist.png)
    ![](55d69a16cd5363426dbf7a3d6a9ee6ce/xcode-info-plist.png)

    ### 7. Run Build!

    This will generate the `environment_preprocess.h` file and make it available for inclusion in your `Info.plist`
    It's advisable to exclude the generated file from any version control system.
    It's advisable to exclude the generated file from any version control system.
    Binary file added xcode-user-defined-environment.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  3. @acrookston acrookston revised this gist May 4, 2016. 1 changed file with 54 additions and 7 deletions.
    61 changes: 54 additions & 7 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,57 @@

    Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

    1. Edit Xcode Scheme and add a pre-action script (see xcode-manage-preaction.png). Copy the included script into the pre-build box.
    2. Edit Xcode Build Settings to use Info.plist preprocessing and enter the script header file name in Prefix File.
    3. Copy the `development.plist` file to `config/environments/development.plist` (or wherever but don't forget to change the paths).
    4. Copy `build_environment.sh` to `config/environments/build_environment.sh`
    5. Update the `build_environment.sh` script to output whatever environment variables you need. Note it reads defaults from the `development.plist`.
    6. Use those environment variables in your `Info.plist`. See example image for usage (eg. `ENV_APP_NAME`).
    7. Build!
    ### 1. Xcode Scheme pre-action

    Edit Xcode Scheme and add a pre-action script.
    Copy the contents of `preaction.sh` into the pre-action script box.

    ![](xcode-manage-preaction.png)


    ### 2. Copy processing file

    1. Copy `build_environment.sh` to `config/environments/build_environment.sh`.
    2. Modify it's variables or path to `environment_preprocess.h` as needed.


    ### 3. Edit Xcode Build Settings

    1. For the `Project Build Settings` add a new User-Defined variables called `ENVIRONMENT`, for each scheme set the appropriate name eg `development` or `production`.
    2. For each Target Build Settings set `Preprocess Info.plist file` to YES.
    3. Enter `environment_preprocess.h` (or the output file if you renamed it `build_environment.sh`) into `Info.plist Preprocessor Prefix File`

    ![](xcode-build-settings.png)


    ### 4. Install plist file

    Copy `development.plist` file to `config/environments/development.plist`. DO NOT include this file in any Target Memberships.

    If you have several schemes setup in the previous step (`ENVIORMENT`) you need to copy this file to each option if you use multiple build schemes and `ENVIRONMENT` variables.

    ![](xcode-user-defined-environment.png)

    ### 5. Setup environment variables

    This file `development.plist` (and others) is used to populate data in the `build_environment.sh` script.
    Modify contents as needed and don't forget to also modify the `build_environment.sh` accordingly.

    This is really where the magic happens. The rest is just for running this script and processing the output.

    TIP 1: You can run pretty much anything in the `environment_preprocess.h` script if you want to setup link to a local development server you can use the `hostname` command to get your current build computer's network name.
    TIP 2: You can use it to use git commit count as the build version/number.

    (Both of these are included in the script already.)


    ### 6. Using environment variables

    6. Use those generated environment variables in your `Info.plist`.

    ![](xcode-info-plist.png)

    ### 7. Run Build!

    This will generate the `environment_preprocess.h` file and make it available for inclusion in your `Info.plist`
    It's advisable to exclude the generated file from any version control system.
  4. @acrookston acrookston revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Automatic build versions from git in Xcode (and other goodies)
    ## Automatic build versions from git in Xcode (and other goodies)

    Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

  5. @acrookston acrookston revised this gist Apr 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Automatic build versions in Xcode (and other goodies)
    # Automatic build versions from git in Xcode (and other goodies)

    Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

  6. @acrookston acrookston revised this gist Apr 13, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@ Installation procedure for pre-build actions to automatically populate Xcode Inf

    1. Edit Xcode Scheme and add a pre-action script (see xcode-manage-preaction.png). Copy the included script into the pre-build box.
    2. Edit Xcode Build Settings to use Info.plist preprocessing and enter the script header file name in Prefix File.
    3. Copy the development.plist file to config/environments/development.plist (or wherever but don't forget to change the path).
    4. Copy build_environment.sh to config/environments/build_environment.sh
    5. Update the build_environment.sh script to output whatever environment variables you need.
    6. Use those environment variables in your Info.plist. See example image for usage (eg. ENV_APP_NAME).
    3. Copy the `development.plist` file to `config/environments/development.plist` (or wherever but don't forget to change the paths).
    4. Copy `build_environment.sh` to `config/environments/build_environment.sh`
    5. Update the `build_environment.sh` script to output whatever environment variables you need. Note it reads defaults from the `development.plist`.
    6. Use those environment variables in your `Info.plist`. See example image for usage (eg. `ENV_APP_NAME`).
    7. Build!
  7. @acrookston acrookston revised this gist Apr 13, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ Installation procedure for pre-build actions to automatically populate Xcode Inf
    3. Copy the development.plist file to config/environments/development.plist (or wherever but don't forget to change the path).
    4. Copy build_environment.sh to config/environments/build_environment.sh
    5. Update the build_environment.sh script to output whatever environment variables you need.
    6. Use those environment variables in your Info.plist. See example image for usage (eg. ENV_APP_NAME).
    6. Use those environment variables in your Info.plist. See example image for usage (eg. ENV_APP_NAME).
    7. Build!
  8. @acrookston acrookston revised this gist Apr 13, 2016. 2 changed files with 11 additions and 1 deletion.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # Automatic build versions in Xcode (and other goodies)

    Installation procedure for pre-build actions to automatically populate Xcode Info.plist with dynamic data.

    1. Edit Xcode Scheme and add a pre-action script (see xcode-manage-preaction.png). Copy the included script into the pre-build box.
    2. Edit Xcode Build Settings to use Info.plist preprocessing and enter the script header file name in Prefix File.
    3. Copy the development.plist file to config/environments/development.plist (or wherever but don't forget to change the path).
    4. Copy build_environment.sh to config/environments/build_environment.sh
    5. Update the build_environment.sh script to output whatever environment variables you need.
    6. Use those environment variables in your Info.plist. See example image for usage (eg. ENV_APP_NAME).
    2 changes: 1 addition & 1 deletion preaction.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/sh
    cd $PROJECT_DIR
    exec > $PROJECT_DIR/pre_action.log 2>&1
    source $PROJECT_DIR/build_environment.sh
    source $PROJECT_DIR/config/environments/build_environment.sh
  9. @acrookston acrookston revised this gist Apr 13, 2016. 3 changed files with 0 additions and 0 deletions.
    Binary file added xcode-build-settings.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    Binary file added xcode-info-plist.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    Binary file added xcode-manage-preaction.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  10. @acrookston acrookston revised this gist Apr 13, 2016. No changes.
  11. @acrookston acrookston revised this gist Apr 13, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions development.plist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?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>README</key>
    <string>Copy this file to config/environments/development.plist. DO NOT add it to the targets! It gets copied to environment.plist during build. See build_environment script for more.</string>
    <key>AppGroup</key>
    <string>your.app.group</string>
    <key>HostName</key>
    <string>localhost</string>
    <key>AppName</key>
    <string>Your app name</string>
    <key>CFBundleShortVersionString</key>
    <string>1.1</string>
    <key>RandomKey</key>
    <string>whatever you want</string>
    </dict>
    </plist>
  12. @acrookston acrookston created this gist Apr 13, 2016.
    81 changes: 81 additions & 0 deletions build_environment.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    #!/bin/sh

    # build_environment.sh
    #
    # Created by Andrew C on 7/27/15.
    # Released under MIT LICENSE
    # Copyright (c) 2015 Andrew Crookston.


    cd "${PROJECT_DIR}"

    dir=${PROJECT_DIR}/config/environments
    echo "Starting environment ${ENVIRONMENT} configuration for ${PROJECT_NAME}/${PROJECT_FOLDER} with config in " $dir
    echo "Host: " `hostname`

    # environment variable from value passed in to xcodebuild.
    # if not specified, we default to DEV
    env=${ENVIRONMENT}
    if [ -z "$env" ]
    then
    env="development"
    fi
    echo "Using $env environment"

    # copy the environment-specific file
    cp $dir/$env.plist $dir/environment.plist

    # Date and time that we are running this build
    buildDate=`date "+%F %H:%M:%S"`
    todaysDay=`date "+%d"`

    # app settings
    appName=`/usr/libexec/PlistBuddy -c "Print :AppName" "$dir/environment.plist"`
    appGroup=`/usr/libexec/PlistBuddy -c "Print :AppGroup" "$dir/environment.plist"`
    version=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "$dir/environment.plist"`
    hostname=`/usr/libexec/PlistBuddy -c "Print :HostName" "$dir/environment.plist"`
    hostscheme=`/usr/libexec/PlistBuddy -c "Print :HostScheme" "$dir/environment.plist"`
    bundle=`git rev-list master |wc -l | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`


    if [ "$env" == "development" ]
    then
    hostname=`hostname`
    # need to dynamically append today's day for non-prod
    appName="$appName-$todaysDay"
    # Last hash from the current branch
    #version=`git log --pretty=format:"%h" -1`
    fi

    buildhost=`hostname`

    # Build the preprocess file
    cd "${PROJECT_DIR}"
    preprocessFile="environment_preprocess.h"

    echo "Creating header file ${PROJECT_DIR}/${preprocessFile}"

    echo "//------------------------------------------" > $preprocessFile
    echo "// Auto generated file. Don't edit manually." >> $preprocessFile
    echo "// See build_environment script for details." >> $preprocessFile
    echo "// Created $buildDate" >> $preprocessFile
    echo "//------------------------------------------" >> $preprocessFile
    echo "" >> $preprocessFile
    echo "#define ENV $env" >> $preprocessFile
    echo "#define ENV_APP_NAME $appName" >> $preprocessFile
    echo "#define ENV_APP_GROUP $appGroup" >> $preprocessFile
    echo "#define ENV_APP_VERSION $version" >> $preprocessFile
    echo "#define ENV_HOST_NAME $hostname" >> $preprocessFile
    echo "#define ENV_BUILD_HOST $buildhost" >> $preprocessFile
    echo "#define ENV_BUNDLE_VERSION $bundle" >> $preprocessFile
    echo "#define ENV_HOST_SCHEME $hostscheme" >> $preprocessFile

    # dump out file to build log
    cat $preprocessFile

    # Force the system to process the plist file
    echo "Touching all plists at: ${PROJECT_DIR}/**/Info.plist"
    touch ${PROJECT_DIR}/**/Info.plist

    # done
    echo "Done."
    4 changes: 4 additions & 0 deletions preaction.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #!/bin/sh
    cd $PROJECT_DIR
    exec > $PROJECT_DIR/pre_action.log 2>&1
    source $PROJECT_DIR/build_environment.sh