-
Notifications
You must be signed in to change notification settings - Fork 33
/
Setup-Xcode.sh
278 lines (206 loc) · 9.47 KB
/
Setup-Xcode.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# set -x
###################################################################################################
# Script Name: Setup-Xcode.sh
# By: Zack Thompson / Created: 3/29/2021
# Version: 1.1.0 / Updated: 12/17/2022 / By: ZT
#
# Description: This script customizes and sets up the Xcode environment for immediate use.
#
###################################################################################################
echo "***** Setup Xcode Process: START *****"
##################################################
# Define Variables
# Specify whether or not to rename the Xcode.app bundle; options are:
# true: "Xcode 12.app"
# false: "Xcode.app"
include_version_in_app_name="${4}"
# Specify setting developer permissions; options are:
# everyone = adds the "everyone" group as a member of "_developer" group
# launchagent = use a LaunchAgent to add "$USER" as a member of the "_developer" group
# null (no value) = do nothing
set_developer_perms="${5}"
launch_agent_label="${6}"
if [[ -z "${launch_agent_label}" ]]; then
launch_agent_label="com.github.mlbz521.XcodeEnableDeveloper"
fi
launch_agent_location="/Library/LaunchAgents/${launch_agent_label}.plist"
# Specify wether or not to edit the authorizationdb to allow any member of "_developer" group
# to be able to install Apple-provided software; options are:
# true: edits the authorizationdb to allow any member of _developer to install Apple-provided software
# false: does nothing
allow_devs_auth="${7}"
# Default path
xcode_path_default="/Applications/Xcode.app"
# Get OS Version Details
os_version=$( /usr/bin/sw_vers -productVersion )
os_major_version=$( echo "${os_version}" | /usr/bin/awk -F '.' '{print $1}' )
os_minor_patch_version=$( echo "${os_version}" | /usr/bin/awk -F '.' '{print $2"."$3}' )
# Get the Console User
console_user=$( /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' )
# Get the Console Users' UniqueID
console_uid=$( /usr/bin/id -u "${console_user}" )
##################################################
# Functions
get_app_bundle_version() {
# Get version of an app bundle
local path="${1}"
local app_version
app_version=$( /usr/bin/defaults read "${path}/Contents/Info.plist" CFBundleShortVersionString )
echo "${app_version}"
}
get_app_bundle_major_version() {
# Get the major version of an app bundle
local path="${1}"
local app_version
local app_major_version
app_version=$( get_app_bundle_version "${path}" )
app_major_version=$( echo "${app_version}" | /usr/bin/awk -F '.' '{print $1}' )
echo "${app_major_version}"
}
determine_newest_version() {
# Get the newest (largest) version of a passed set of app bundle locations
local array=("$@")
local path
local version_array
local newest
declare -a version_array
for path in "${array[@]}"; do
version_array+=( $( get_app_bundle_version "${path}" ) )
done
old_IFS=$IFS
IFS=$'\n'
newest=($( sort --version-sort --reverse <<< "${version_array[*]}" ))
IFS=$old_IFS
for i in "${!version_array[@]}"; do
[[ "${version_array[$i]}" == "${newest[0]}" ]] && break
done
echo "${array[${i}]}"
}
##################################################
# Bits staged...
if [[ -d "${xcode_path_default}" ]]; then
# Acting as if this is a "new" install...
if [[ $include_version_in_app_name =~ [Tt][Rr][Uu][Ee] ]]; then
xcode_major_version=$( get_app_bundle_major_version "${xcode_path_default}" )
new_xcode_path="/Applications/Xcode ${xcode_major_version}.app"
if [[ -e "${new_xcode_path}" ]]; then
echo "Deleting previous major version..."
/bin/rm -Rf "${new_xcode_path}"
fi
# Update the Xcode app name
echo "Renaming Xcode app bundle..."
/bin/mv "${xcode_path_default}" "${new_xcode_path}"
fi
xcode_path="${xcode_path_default}"
fi
# Determine the Xcode app bundle to work on...
if [[ -z "${xcode_path}" ]]; then
# Find Xcode app bundles
app_paths=$( /usr/bin/find -E /Applications -iregex ".*/Xcode(.*)?[.]app" -type d -prune -maxdepth 1 )
# Verify that at least one app bundle version was found.
if [[ -z "${app_paths}" ]]; then
echo "ERROR: Xcode is not in the expected location!"
echo "***** Setup Xcode Process: FAILED *****"
exit 1
else
# If the machine has multiple app bundles Applications, loop through them...
declare -a app_path_array
while IFS=$'\n' read -r app_path; do
app_path_array+=("${app_path}")
done < <(echo "${app_paths}")
if [[ ${#app_path_array[@]} -gt 1 ]]; then
xcode_path=$( determine_newest_version "${app_path_array[@]}" )
else
xcode_path="${app_path_array[0]}"
fi
fi
fi
# Ensure an app bundle was identified
if [[ -z "${xcode_path}" ]]; then
echo "ERROR: Xcode could not be found!"
echo "***** Setup Xcode Process: FAILED *****"
exit 2
fi
# Remove quarantine bit just in case
/usr/bin/xattr -dr com.apple.quarantine "${xcode_path}"
# Specify the version of Xcode to use
echo "Selecting ${xcode_path} as the default for Xcode CMD Line Tools..."
/usr/bin/xcode-select --switch "${xcode_path}"
# Change the authorization policies to allow members of the admin and _developer groups to be
# able to authenticate to use the Apple-code-signed debugger or performance analysis tools
echo "Allowing the admin and developer groups to use Xcode tools..."
/usr/sbin/DevToolsSecurity -enable
# Accept the Xcode license
echo "Accepting the Xcode license..."
/usr/bin/xcodebuild -license accept
# Install all additional components
echo "Running Xcode first launch..."
/usr/bin/xcodebuild -runFirstLaunch
if [[ "${allow_devs_auth}" =~ [Tt][Rr][Uu][Ee] ]]; then
echo "Editing the authorizationdb to allow members of \`_developer\` group to install Apple-provided software"
# Allow any member of _developer to install Apple-provided software
/usr/bin/security authorizationdb write system.install.apple-software authenticate-developer
fi
if [[ -z "${set_developer_perms}" ]]; then
echo "Not configuring the developer permissions..."
elif [[ "${set_developer_perms}" =~ [Ee][Vv][Ee][Rr][Yy][Oo][Nn][Ee] ]]; then
echo "Adding the \`everyone\` group as a member of the \`_developer\` group."
# Add the "everyone" group as a member of "_developer" group
/usr/sbin/dseditgroup -o edit -a everyone -t group _developer
elif [[ "${set_developer_perms}" =~ [Ll][Aa][Uu][Nn][Cc][Hh][Aa][Gg][Ee][Nn][Tt] ]]; then
echo "Creating LaunchAgent to manage the \`_developer\` group: ${launch_agent_location}"
# Create a LaunchAgent
echo "LaunchAgent is: ${launch_agent_location}"
/bin/cat > "${launch_agent_location}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$launch_agent_label</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>/usr/sbin/dseditgroup -o edit -a "${USER}" -t user _developer</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
/usr/sbin/chown root:wheel "${launch_agent_location}"
/bin/chmod 644 "${launch_agent_location}"
if [[ -z "${console_uid}" || "${console_user}" == "loginwindow" ]]; then
echo "Notice: A console user is not currently logged in, will not attempt to bootstrap the LaunchAgent"
else
if [[ -e "${launch_agent_location}" ]]; then
# Check if the LaunchDaemon is running before loading it again.
# Determine proper launchctl syntax based on OS Version.
# macOS 11+ or macOS 10.11+
if [[ $( /usr/bin/bc <<< "${os_major_version} >= 11" ) -eq 1 || ( "${os_major_version}" == 10 && $( /usr/bin/bc <<< "${os_minor_patch_version} >= 11" ) -eq 1 ) ]]; then
exit_code=$( /bin/launchctl print gui/"${console_uid}"/"${launch_agent_label}" > /dev/null 2>&1; echo $? )
if [[ $exit_code == 0 ]]; then
echo "Stopping agent: ${launch_agent_location}"
/bin/launchctl bootout gui/"${console_uid}"/"${launch_agent_label}"
fi
echo "Starting agent: ${launch_agent_location}"
/bin/launchctl bootstrap gui/"${console_uid}" "${launch_agent_location}"
/bin/launchctl enable gui/"${console_uid}"/"${launch_agent_label}"
# macOS 10.x - macOS 10.10
elif [[ "${os_major_version}" == 10 && $( /usr/bin/bc <<< "${os_minor_patch_version} <= 10" ) -eq 1 ]]; then
exit_code=$( /bin/launchctl asuser "${console_uid}"/bin/launchctl list "${launch_agent_label}" > /dev/null 2>&1; echo $? )
if [[ $exit_code == 0 ]]; then
echo "Stopping agent: ${launch_agent_location}"
/bin/launchctl asuser "${console_uid}"/bin/launchctl unload "${launch_agent_location}"
fi
echo "Starting agent: ${launch_agent_location}"
/bin/launchctl asuser "${console_uid}"/bin/launchctl load "${launch_agent_location}"
/bin/launchctl asuser "${console_uid}"/bin/launchctl start "${launch_agent_location}"
fi
fi
fi
fi
echo "***** Setup Xcode Process: COMPLETE *****"
exit 0