1#!/bin/bash
2
3# Accept EULA so there is no prompt
4if [[ -e "/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild" ]]; then
5/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -license accept
6fi
7
8# Just in case the xcodebuild command above fails to accept the EULA, set the license acceptance info
9# in /Library/Preferences/com.apple.dt.Xcode.plist. For more details on this, see Tim Sutton's post:
10# http://macops.ca/deploying-xcode-the-trick-with-accepting-license-agreements/
11
12if [[ -e "/Applications/Xcode.app/Contents/Resources/LicenseInfo.plist" ]]; then
13
14 xcode_version_number=`/usr/bin/defaults read "/Applications/Xcode.app/Contents/"Info CFBundleShortVersionString`
15 xcode_build_number=`/usr/bin/defaults read "/Applications/Xcode.app/Contents/Resources/"LicenseInfo licenseID`
16 xcode_license_type=`/usr/bin/defaults read "/Applications/Xcode.app/Contents/Resources/"LicenseInfo licenseType`
17
18 if [[ "${xcode_license_type}" == "GM" ]]; then
19 /usr/bin/defaults write "/Library/Preferences/"com.apple.dt.Xcode IDEXcodeVersionForAgreedToGMLicense "$xcode_version_number"
20 /usr/bin/defaults write "/Library/Preferences/"com.apple.dt.Xcode IDELastGMLicenseAgreedTo "$xcode_build_number"
21 else
22 /usr/bin/defaults write "/Library/Preferences/"com.apple.dt.Xcode IDEXcodeVersionForAgreedToBetaLicense "$xcode_version_number"
23 /usr/bin/defaults write "/Library/Preferences/"com.apple.dt.Xcode IDELastBetaLicenseAgreedTo "$xcode_build_number"
24 fi
25
26fi
27
28# Installing the Xcode command line tools on 10.7.x or higher
29
30osx_vers=$(sw_vers -productVersion | awk -F "." '{print $2}')
31cmd_line_tools_temp_file="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
32
33# Accept EULA so there is no prompt
34if [[ -e "/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild" ]]; then
35/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -license accept
36fi
37# Install Mobile Device Package so there is no prompt
38if [[ -e "/Applications/Xcode.app/Contents/Resources/Packages/MobileDevice.pkg" ]]; then
39/usr/sbin/installer -pkg "/Applications/Xcode.app/Contents/Resources/Packages/MobileDevice.pkg" -target /
40fi
41if [[ -e "/Applications/Xcode.app/Contents/Resources/Packages/MobileDeviceDevelopment.pkg" ]]; then
42/usr/sbin/installer -pkg "/Applications/Xcode.app/Contents/Resources/Packages/MobileDeviceDevelopment.pkg" -target /
43fi
44# Install Xcode System Resources Package, available in Xcode 8 and later
45if [[ -e "/Applications/Xcode.app/Contents/Resources/Packages/XcodeSystemResources.pkg" ]]; then
46 /usr/sbin/installer -pkg "/Applications/Xcode.app/Contents/Resources/Packages/XcodeSystemResources.pkg" -target /
47fi
48
49# Installing the latest Xcode command line tools on 10.9.x or higher
50
51if [[ "$osx_vers" -ge 9 ]]; then
52
53 # Create the placeholder file which is checked by the softwareupdate tool
54 # before allowing the installation of the Xcode command line tools.
55
56 touch "$cmd_line_tools_temp_file"
57
58 # Identify the correct update in the Software Update feed with "Command Line Tools" in the name for the OS version in question.
59
60 if [[ "$osx_vers" -gt 9 ]]; then
61 cmd_line_tools=$(softwareupdate -l | awk '/* Command Line Tools/ { $1=$1;print }' | grep "$osx_vers" | sed 's/^[[ ]]*//;s/[[ ]]*$//;s/*//' | cut -c 2-)
62 elif [[ "$osx_vers" -eq 9 ]]; then
63 cmd_line_tools=$(softwareupdate -l | awk '/* Command Line Tools/ { $1=$1;print }' | grep "Mavericks" | sed 's/^[[ ]]*//;s/[[ ]]*$//;s/*//' | cut -c 2-)
64 fi
65
66 #Install the command line tools
67
68 softwareupdate -i "$cmd_line_tools" --verbose
69
70 # Remove the temp file
71
72 if [[ -f "$cmd_line_tools_temp_file" ]]; then
73 rm "$cmd_line_tools_temp_file"
74 fi
75fi
76
77# Installing the latest Xcode command line tools on 10.7.x and 10.8.x
78
79# on 10.7/10.8, instead of using the software update feed, the command line tools are downloaded
80# instead from public download URLs, which can be found in the dvtdownloadableindex:
81# https://devimages.apple.com.edgekey.net/downloads/xcode/simulators/index-3905972D-B609-49CE-8D06-51ADC78E07BC.dvtdownloadableindex
82
83if [[ "$osx_vers" -eq 7 ]] || [[ "$osx_vers" -eq 8 ]]; then
84
85 if [[ "$osx_vers" -eq 7 ]]; then
86 DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_xcode_os_x_lion_april_2013.dmg
87 fi
88
89 if [[ "$osx_vers" -eq 8 ]]; then
90 DMGURL=http://devimages.apple.com/downloads/xcode/command_line_tools_for_osx_mountain_lion_april_2014.dmg
91 fi
92
93 TOOLS=cltools.dmg
94 curl "$DMGURL" -o "$TOOLS"
95 TMPMOUNT=`/usr/bin/mktemp -d /tmp/clitools.XXXX`
96 hdiutil attach "$TOOLS" -mountpoint "$TMPMOUNT" -nobrowse
97 # The "-allowUntrusted" flag has been added to the installer
98 # command to accomodate for now-expired certificates used
99 # to sign the downloaded command line tools.
100 installer -allowUntrusted -pkg "$(find $TMPMOUNT -name '*.mpkg')" -target /
101 hdiutil detach "$TMPMOUNT"
102 rm -rf "$TMPMOUNT"
103 rm "$TOOLS"
104fi
105
106sleep 5
107/usr/sbin/DevToolsSecurity -enable
108/usr/sbin/dseditgroup -o edit -a everyone -t group _developer
109
110exit 0 ## Success
111exit 1 ## Failure