Skip to content

Instantly share code, notes, and snippets.

@adaam
Created April 28, 2015 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaam/4d89b0e3cdfec73fc15c to your computer and use it in GitHub Desktop.
Save adaam/4d89b0e3cdfec73fc15c to your computer and use it in GitHub Desktop.
use for ipa file resign
#!/bin/sh
. config.sh
# variables
SourceFile=""
DestFile=""
Version="1.1.0"
IsAppStore=0
function Print_Usage()
{
echo
echo " Usage: $0 [options] [IPA file]"
echo
echo " Options:"
echo " -h, --help Output usage information"
echo " -v, --version Output the version number"
echo " -o, --output [IPA], --output=[IPA] Specify output file name resigned by IPA"
echo " -s, --appstore Generate IPA file which is going to been submitted to App Store"
echo " (Default output is used to been deployed by the way of AdHoc or Enterprise Program)"
echo
echo " Output: Signed_[IPA] or Specified IPA"
echo
}
function Print_Version()
{
echo "Version: $Version"
}
# Get variable
optspec=":hv-:o:s"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
help)
Print_Usage
exit 0
;;
version)
Print_Version
exit 0
;;
output)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
DestFile="${val}"
;;
output=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
DestFile="${val}"
;;
appstore)
IsAppStore=1
;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
echo "Unknown option --${OPTARG}" >&2
fi
;;
esac;;
o)
DestFile="${OPTARG}"
;;
s)
IsAppStore=1
;;
h)
Print_Usage
exit 0
;;
v)
echo "Version: $Version"
exit 0
;;
:)
echo "Non-option argument: '-${OPTARG}'" >&2
exit 1
;;
?)
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'" >&2
fi
;;
esac
done
SourceFile="${!OPTIND}"
# Default checking
if [ -z "$SourceFile" ] || [ ! -f "$SourceFile" ] ; then
echo "Error: Need to indicate an Application" >&2
exit 1
fi
if [ -z "$ProvProfile" ] || [ ! -f "$ProvProfile" ] ; then
echo "Error: Need to indicate a Provisioning Profile" >&2
exit 1
fi
if [ -z "$SigningCertName" ] ; then
echo "Error: Need to specify certification name for sign." >&2
exit 1
fi
if [ -z "$DestFile" ] ; then
DestFile="Signed_$SourceFile"
fi
rm -rf temparea
unzip -q $SourceFile -d temparea
# APP NAME FROM INSIDE PAYLOAD FOLDER.app
AppInternalName=`find temparea/Payload/ -name "*.app" -exec basename {} \;`
if [ -z "$AppInternalName" ] ; then
echo "Error: Need to specify app internal name." >&2
echo "(APP name from inside Payload FOLDER.app)" >&2
rm -rf temparea
exit 1
fi
if [ ! -d "temparea/Payload/$AppInternalName" ] ; then
echo "Error: Format Error inside Payload FOLDER.app" >&2
rm -rf temparea
exit 1
fi
if [ "$IsAppStore" = 1 ]; then
if [ -z "$AppleID" ] ; then
echo "Error: Need to specify Apple ID" >&2
exit 1
fi
cp Entitlements.plist "temparea/Payload/$AppInternalName"
if [ ! -f "temparea/Payload/$AppInternalName/Entitlements.plist" ]; then
echo "Error: Fail to copy Entitlements.plist"
exit 1
fi
fi
echo
echo "## Going to take the app $SourceFile and resign it as $DestFile ##"
echo
if [ "$IsAppStore" = 1 ]; then
echo "# (Apple ID) $AppleID"
fi
echo "# (Bundle ID) $BundleID"
echo "# (APP Name) $AppInternalName"
echo "# (ProvProfile Path) $ProvProfile"
echo "# (SigningCertName) $SigningCertName"
echo
echo "===== Original Sign Information ====="
cd temparea/Payload
codesign -d -vv "./$AppInternalName/"
echo
# Copy the local provisioning profile to this app
cp "$ProvProfile" "./$AppInternalName/embedded.mobileprovision"
export EMBEDDED_PROFILE_NAME=embedded.mobileprovision
# Set CODESIGN_ALLOCATE base on Xcode version
# Xcode < 5.0: /Applications/Xcode.app/Contents/Developer/usr/bin/codesign_allocate
# Xcode = 5.0: /usr/bin/codesign_allocate
# Xcode > 5.0.1: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
XCODE_PATH_CONTENT_DEVELOPER=`xcode-select -print-path`
if [ -f "${XCODE_PATH_CONTENT_DEVELOPER}/usr/bin/codesign_allocate" ]; then
export CODESIGN_ALLOCATE="${XCODE_PATH_CONTENT_DEVELOPER}/usr/bin/codesign_allocate"
elif [ -f "${XCODE_PATH_CONTENT_DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate" ]; then
export CODESIGN_ALLOCATE="${XCODE_PATH_CONTENT_DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate"
else
export CODESIGN_ALLOCATE="/usr/bin/codesign_allocate"
fi
# Update the Info.plist with the new Bundle ID
echo "----- Modifying -----"
if [ ! -z "$BundleID" ] ; then
From=`/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "./$AppInternalName/Info.plist"`
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $BundleID" "./$AppInternalName/Info.plist"
To=`/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "./$AppInternalName/Info.plist"`
echo "BundleID: $From => $To"
else
BundleID=`/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" "./$AppInternalName/Info.plist"`
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $BundleID" "./$AppInternalName/Info.plist"
echo "BundleID: Keep $BundleID"
fi
if [ ! -z "$ProductVersion" ] ; then
From=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "./$AppInternalName/Info.plist"`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $ProductVersion" "./$AppInternalName/Info.plist"
To=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "./$AppInternalName/Info.plist"`
echo "ProductVersion: $From => $To"
fi
if [ ! -z "$ResourcesFolderName" ] ; then
find "../../$ResourcesFolderName" -type f -maxdepth 2 \( -name "*.png" -o -name "*.bmp" -o -name "*.html" -o -name "*.strings" \) -print | while read obj
do
F=`echo $obj | sed -e "s/..\/..\/${ResourcesFolderName}\///"`
if [ -f "./$AppInternalName/$F" ] ; then
cp -f "../../$ResourcesFolderName/$F" "./$AppInternalName/$F"
echo "Copy ../../$ResourcesFolderName/$F => ./$AppInternalName/$F"
else
echo "Skip ../../$ResourcesFolderName/$F"
fi
done
fi
if [ "$IsAppStore" = 1 ]; then
/usr/libexec/PlistBuddy -c "Set :application-identifier $AppleID" "./$AppInternalName/Entitlements.plist"
AID=`/usr/libexec/PlistBuddy -c "Print application-identifier" "./$AppInternalName/Entitlements.plist"`
echo "AppleID: => $AID"
fi
echo
echo "----- Resigning -----"
pwd
echo "CODESIGN_ALLOCATE: $CODESIGN_ALLOCATE"
#echo "IsAppStore: $IsAppStore"
if [ "$IsAppStore" = 1 ]; then
echo "*** Sign for App Store Distribution ***"
codesign -f -vv -s "$SigningCertName" -i "$BundleID" --entitlements "./$AppInternalName/Entitlements.plist" "$AppInternalName/"
else
echo "*** Not sign for App Store Distribution ***"
codesign -f -vv -s "$SigningCertName" -i "$BundleID" "$AppInternalName/"
fi
if [ $? -ne 0 ] ; then
echo "Error: Fail to resign"
exit 2
fi
echo
echo "===== New Sign Information ====="
codesign -d -vv "./$AppInternalName/"
echo
cd ..
zip -r -q "../$DestFile" .
cd ..
rm -rf temparea
echo
echo "# DONE"
echo
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment