Skip to content

Instantly share code, notes, and snippets.

@pixelbacon
Last active December 7, 2018 21:52
Show Gist options
  • Save pixelbacon/29c27e5db8a3bef5f55fede3d314597f to your computer and use it in GitHub Desktop.
Save pixelbacon/29c27e5db8a3bef5f55fede3d314597f to your computer and use it in GitHub Desktop.
Switch Version of Xcode
#!/bin/sh
# `sudo sh xcode-version -8.2.1`
# Set variables
xcodeAppsPattern="Xcode_*.app"
DidSwitchedVersions=false
CurrentV=$( defaults read /Applications/Xcode.app/Contents/Info CFBundleShortVersionString )
DesiredV="$1"
# Move into the right directory
cd /Applications
# Declare functions
show_intro(){
echo ""
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Xcode Version Manager Script"
echo "- Originally created by Michael Minor <me@pixelbacon.com>"
echo "---------------------------------------------------------"
}
exit_gracefully(){
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo ""
exit 1
}
show_help(){
echo "Usage:"
echo ""
echo "# lists versions installed"
echo "sh ./xcode-version -list"
echo ""
echo "# Would switch to Xcode v8.3.2 if in Applications folder"
echo "sudo sh ./xcode-version 8.3.2"
exit_gracefully
}
list_currentInstalled(){
echo "Current version: $CurrentV";
echo "---------------------------------------------------------"
echo "Other versions:"
for App in $xcodeAppsPattern; do
AppVersion=$( defaults read /Applications/$App/Contents/Info CFBundleShortVersionString )
echo "- $AppVersion ($App)";
done
exit_gracefully
}
switch_version(){
echo "Current version: $CurrentV"
echo "Desired version: $DesiredV"
# If current version is the same as desired, exit
if [ "$CurrentV" = "$DesiredV" ]; then
echo "Desired version is already active. Exiting!"
exit_gracefully
fi
echo "---------------------------------------------------------"
# Loop through all versions of Xcode
for App in $xcodeAppsPattern; do
AppVersion=$( defaults read /Applications/$App/Contents/Info CFBundleShortVersionString )
echo "Found version: $AppVersion";
echo "---------------------------------------------------------"
if [ "$AppVersion" = "$DesiredV" ]; then
# Move current, rename based on version
mv Xcode.app Xcode_$CurrentV.app
# Rename desired to current
mv $App Xcode.app
DidSwitchedVersions=true
fi;
done
# If target was found
if $DidSwitchedVersions; then
# Tell system to use the desired version
# @see https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/xcode-select.1.html
xcode-select --switch Xcode.app
echo "SUCCESS: Switched to version $DesiredV"
else
echo "FAIL: Did not find desired version :("
fi
exit_gracefully
}
# Start
show_intro
# If no arguments, show 'help'.
if [ -z "$1" ]; then
show_help
fi
# List
if [ "$1" = '-list' ]; then
list_currentInstalled
fi
# Change version
switch_version
@pixelbacon
Copy link
Author

pixelbacon commented Jun 12, 2017

Switch Between Installed Versions of Xcode

Switching xcode versions is a pain in the @ss... This automates the process, and keeps the specific version number in tact in the file name by reading the actual CFBundleShortVersionString.

Setup

  1. Expand various versions of Xcode to /Applications and append _$VERSION to the file name. I.E. Xcode_8.3.3
  2. Required: Versions other than the current have to have a _ character in the file name.
  3. Create a new file ~/xcode-version.sh and paste the contents.
  4. Run script as desired.

Usage

# Help/Usage
sh xcode-version
# list installed
sh xcode-version -list
# switch version
sudo sh xcode-version 8.2.1

Caveats/Boo/Hiss

xcode-select --switch does NOT change the file association of xcode project files. You will manually have to Open With and choose the version you switched to. I could have put in a way to do this automatically... But it's a bit daunting for OSX. Also, I did not want the script to be that opinionated. If you're switching between versions of Xcode, this is the only non-automated step you need to do.

Why Apple did not put this in as part of the xcode-select --switch, I don't know. That makes no sense to me.

Notes

  • You do not have to worry about naming a new version. You could literally name it anything and the script will find it based on the version number, as long as it has an _ in the filename.
  • I created this solution after seeing every blog post or stackoverflow suggestion be manual. Bundled version has been a standard in OSX for kind of a long time now. This solution takes advantage of that standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment