Xcode
iOS
mobileprovision
プロビジョニングプロファイル
更新

Xcodeのプロビジョニングプロファイル(.mobileprovision)をcliで更新する

More than 1 year has passed since last update.

fastlaneなどの最新のCIツールやXcodeのGUIを使えばいろいろ勝手に処理してくれるので最近ではあまる使わないのですが、その昔Xcodeのプロビジョニングプロファイル(.mobileprovision)をcliから更新する必要にせまられて作ったスクリプトです。

このスクリプトをプロビジョニングプロファイル(.mobileprovision)が保存してあるディレクトリで実行するとその内容を解析してXcodeが保存しているプロビジョニングプロファイル(/Users/[ユーザー]/Library/MobileDevice/Provisioning Profiles/に保存あsれている)の古いプロビジョニングプロファイルと入れ替えます。


update_mobileprovision.sh

#!/bin/bash

function on_exit()
{
if [ -f ${MOBILEPROVISION_FILE}.plain ]; then
rm -f ${MOBILEPROVISION_FILE}.plain
fi
if
[ -f ${OLD_MOBILEPROVISION_FILE}.plain ]; then
rm -f ${OLD_MOBILEPROVISION_FILE}.plain
fi
}

trap on_exit EXIT

USER=`whoami`

MOBILEPROVISION_FILES=`ls *.mobileprovision`
for MOBILEPROVISION_FILE in ${MOBILEPROVISION_FILES}; do
#echo ${MOBILEPROVISION_FILE}
/usr/bin/openssl smime -in ${MOBILEPROVISION_FILE} -inform der -verify > ${MOBILEPROVISION_FILE}.plain 2>/dev/null
MOBILEPROVISION_UUID=`/usr/libexec/PlistBuddy -c "Print UUID" "${MOBILEPROVISION_FILE}.plain"`

BETA_ACTIVE=`/usr/libexec/PlistBuddy -c "Print Entitlements:beta-reports-active" "${MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${BETA_ACTIVE}
APP_IDENTIFIER=`/usr/libexec/PlistBuddy -c "Print Entitlements:application-identifier" "${MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
if [ "${BETA_ACTIVE}" = "true" ]; then
# Maybe Production
TARGET_ENV='AppStore'
else
TASK_ALLOW=`/usr/libexec/PlistBuddy -c "Print Entitlements:get-task-allow" "${MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${TASK_ALLOW}
if [ "${TASK_ALLOW}" = "true" ]; then
# Maybe Development
TARGET_ENV='Development'
else
# Maybe AdHoc
TARGET_ENV='AdHoc'
fi
fi

#echo ${TARGET_ENV}
TERM_ID=`/usr/libexec/PlistBuddy -c "Print Entitlements:com.apple.developer.team-identifier" "${MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${TERM_ID}
APP_ID=`echo ${APP_IDENTIFIER} | sed -e "s/^${TERM_ID}\.//g"`
#echo ${APP_ID}

# Remove old mobileprovision
OLD_MOBILEPROVISION_FILES=`(cd "/Users/${USER}/Library/MobileDevice/Provisioning Profiles/" ;ls *.mobileprovision)`
for OLD_MOBILEPROVISION_FILE in ${OLD_MOBILEPROVISION_FILES}; do
#echo ${OLD_MOBILEPROVISION_FILE}

/usr/bin/openssl smime -in "/Users/${USER}/Library/MobileDevice/Provisioning Profiles/"${OLD_MOBILEPROVISION_FILE} -inform der -verify > ${OLD_MOBILEPROVISION_FILE}.plain 2>/dev/null

BETA_ACTIVE=`/usr/libexec/PlistBuddy -c "Print Entitlements:beta-reports-active" "${OLD_MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${BETA_ACTIVE}
OLD_APP_IDENTIFIER=`/usr/libexec/PlistBuddy -c "Print Entitlements:application-identifier" "${OLD_MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
if [ "${BETA_ACTIVE}" = "true" ]; then
# Maybe Production
OLD_TARGET_ENV='AppStore'
else
TASK_ALLOW=`/usr/libexec/PlistBuddy -c "Print Entitlements:get-task-allow" "${OLD_MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${TASK_ALLOW}
if [ "${TASK_ALLOW}" = "true" ]; then
# Maybe Development
OLD_TARGET_ENV='Development'
else
# Maybe AdHoc
OLD_TARGET_ENV='AdHoc'
fi
fi

#echo ${OLD_TARGET_ENV}
OLD_TERM_ID=`/usr/libexec/PlistBuddy -c "Print Entitlements:com.apple.developer.team-identifier" "${OLD_MOBILEPROVISION_FILE}.plain" 2>/dev/null || true`
#echo ${TERM_ID}
OLD_APP_ID=`echo ${OLD_APP_IDENTIFIER} | sed -e "s/^${OLD_TERM_ID}\.//g"`
#echo ${OLD_APP_ID}

if [ "${OLD_APP_IDENTIFIER}" = "${APP_IDENTIFIER}" ] && [ "${OLD_TARGET_ENV}" = "${TARGET_ENV}" ]; then
rm -f "/Users/${USER}/Library/MobileDevice/Provisioning Profiles/"${OLD_MOBILEPROVISION_FILE}
fi
rm -f ${OLD_MOBILEPROVISION_FILE}.plain
done

if [ ! -f "/Users/${USER}/Library/MobileDevice/Provisioning Profiles/${MOBILEPROVISION_UUID}.mobileprovision" ]; then
cp "${MOBILEPROVISION_FILE}" "/Users/${USER}/Library/MobileDevice/Provisioning Profiles/${MOBILEPROVISION_UUID}.mobileprovision"
fi
rm -f ${MOBILEPROVISION_FILE}.plain
done