Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash shell script to clean, archive, and package xcode project. (Script extension can be renamed to .command to make it executable. Script must be located in the same directory as the .xcodeproj file)
#!/bin/bash
#configurable variables
SCHEME_NAME="SCHEME-NAME-HERE"
DEVELOPER_NAME="iPhone Distribution: YOUR_DEVELOPER_NAME_HERE (ID_HERE)"
PROVISIONING_PROFILE="/ABSOLUTE/PATH/TO/PROVISIONING/PROFILE_HERE.mobileprovision"
USE_DEFAULT_DEVELOPER_NAME=true
USE_DEFAULT_PROVISIONING_PROFILE=true
OPEN_DIR_WHEN_FINISHED=false
DISPLAY_NOTIFICATION_WHEN_FINISHED=false
#dont change below
ARCHIVE_DIR="${HOME}/Library/Developer/Xcode/Archives/$(date +%Y-%m-%d)"
ARCHIVE_DIR_COPY="${ARCHIVE_DIR}_"
ARCHIVE_FILE_PATH="${ARCHIVE_DIR}/"*".xcarchive"
ARCHIVE_APP_PATH=${ARCHIVE_FILE_PATH}"/Products/Applications/"*".app"
pushd "$(dirname "$0")" > /dev/null
PROJECT_DIR="`pwd -P`"
popd > /dev/null
OUTPUT_DIR="${PROJECT_DIR}/release"
TEXT_NORMAL=$(tput sgr0)
TEXT_BOLD=$(tput bold)
TEXT_BOLD_RED=${TEXT_BOLD}$(tput setaf 1)
if [ -f "${ARCHIVE_DIR}" ] ; then
mv "${ARCHIVE_DIR}" "${ARCHIVE_DIR_COPY}"
fi
cd "${PROJECT_DIR}"
xcodebuild -scheme "${SCHEME_NAME}" -configuration Release clean archive
if [ $? != 0 ] ; then
echo ""
echo "${TEXT_BOLD_RED}** ARCHIVE FAILED **${TEXT_NORMAL}"
echo ""
xcodebuild -list
echo ""
if [ -f "${ARCHIVE_DIR_COPY}" ] ; then
mv "${ARCHIVE_DIR_COPY}" "${ARCHIVE_DIR}"
fi
exit 1
fi
mkdir -p "${OUTPUT_DIR}"
ARCHIVE_FILENAME="$(basename $ARCHIVE_FILE_PATH)"
OUTPUT_FILENAME="${ARCHIVE_FILENAME%.*}"
OUTPUT_IPA_PATH="${OUTPUT_DIR}/${OUTPUT_FILENAME}.ipa"
if [ "$USE_DEFAULT_DEVELOPER_NAME" == true -a "$USE_DEFAULT_PROVISIONING_PROFILE" == true ] ; then
/usr/bin/xcrun -sdk iphoneos -log PackageApplication ${ARCHIVE_APP_PATH} -o "${OUTPUT_IPA_PATH}"
elif $USE_DEFAULT_DEVELOPER_NAME ; then
/usr/bin/xcrun -sdk iphoneos -log PackageApplication ${ARCHIVE_APP_PATH} -o "${OUTPUT_IPA_PATH}" --embed ${PROVISIONING_PROFILE}
elif $USE_DEFAULT_PROVISIONING_PROFILE ; then
/usr/bin/xcrun -sdk iphoneos -log PackageApplication ${ARCHIVE_APP_PATH} -o "${OUTPUT_IPA_PATH}" --sign "${DEVELOPER_NAME}"
else
/usr/bin/xcrun -sdk iphoneos -log PackageApplication ${ARCHIVE_APP_PATH} -o "${OUTPUT_IPA_PATH}" --sign "${DEVELOPER_NAME}" --embed ${PROVISIONING_PROFILE}
fi
if [ $? != 0 ] ; then
echo ""
echo "${TEXT_BOLD_RED}** PACKAGE FAILED **${TEXT_NORMAL}"
echo ""
rm -rf "${ARCHIVE_DIR}"
if [ -f "${ARCHIVE_DIR_COPY}" ] ; then
mv "${ARCHIVE_DIR_COPY}" "${ARCHIVE_DIR}"
fi
if $OPEN_DIR_WHEN_FINISHED ; then
open "${OUTPUT_DIR}"
fi
exit 1
else
mv ${ARCHIVE_FILE_PATH} "${OUTPUT_DIR}/$(basename $ARCHIVE_FILE_PATH)"
rm -rf "${ARCHIVE_DIR}"
if [ -f "${ARCHIVE_DIR_COPY}" ] ; then
mv "${ARCHIVE_DIR_COPY}" "${ARCHIVE_DIR}"
fi
if $OPEN_DIR_WHEN_FINISHED ; then
open "${OUTPUT_DIR}"
fi
fi
echo ""
echo "${TEXT_BOLD}** PACKAGE SUCCEEDED **${TEXT_NORMAL}"
echo ""
if $DISPLAY_NOTIFICATION_WHEN_FINISHED ; then
terminal-notifier -message "${OUTPUT_FILENAME}.ipa" -title "Packaged App"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.