Last active
December 7, 2018 21:52
-
-
Save pixelbacon/29c27e5db8a3bef5f55fede3d314597f to your computer and use it in GitHub Desktop.
Switch Version of Xcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]>" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
/Applications
and append_$VERSION
to the file name. I.E.Xcode_8.3.3
_
character in the file name.~/xcode-version.sh
and paste the contents.Usage
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
_
in the filename.