Skip to content

Instantly share code, notes, and snippets.

@charlesbedrosian
Created December 19, 2014 05:36
Show Gist options
  • Save charlesbedrosian/7e1e38951cebf46e4c79 to your computer and use it in GitHub Desktop.
Save charlesbedrosian/7e1e38951cebf46e4c79 to your computer and use it in GitHub Desktop.
Takes inforrmation from git repo and updates the settings bundle values for known keys. Also sets the version information based on the number of git commits as a means to increment the bundle version and appends it to the version string.
#/bin/bash
function set_default_value_for_key {
key_to_find=$1
value_to_set=$2
cnt=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:" $plist | grep "Dict"|wc -l | tr -d ' '`
cnt=`expr "$cnt" '-' '1'`
for i in `seq 0 $cnt`; do
key=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${i}:Key" $plist 2>/dev/null`
if [[ $key == $key_to_find ]]; then
/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${i}:DefaultValue $value_to_set" $plist
/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${i}" $plist
fi
done
}
plist=$1
info_plist=$2
GIT_REVISION=$(git log --pretty=format:'%h' -n 1)
commit_sha=`git rev-parse --short HEAD`
commit_ts=`git show -s --format="%ci" $sha`
commit_dt=${commit_ts:0:16}
commit_count=`git log --oneline | wc -l | tr -d ' '`
build_no=$commit_count
version_no=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" $info_plist`
base_pattern="(^[0-9]+\.[0-9]+).*"
if [[ $version_no =~ $base_pattern ]]; then
version_no_stripped="${BASH_REMATCH[1]}"
fi
version_string="${version_no_stripped}.${build_no}"
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $build_no" $info_plist
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $version_string" $info_plist
set_default_value_for_key commit_sha1 $commit_sha
set_default_value_for_key commit_date $commit_dt
set_default_value_for_key version_info_string $version_string
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $info_plist
/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" $info_plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment