-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathXcodeVersionSwitcher.sh
More file actions
executable file
·325 lines (278 loc) · 8.46 KB
/
XcodeVersionSwitcher.sh
File metadata and controls
executable file
·325 lines (278 loc) · 8.46 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/sh
###############################################################################################
#
# Allows for quick switching between Xcode and the Xcode beta (Swift 3.0.1 and Swift 3.1)
#
# Author: Tyler Hostager
# Version: 3/3/17
#
##############################################################################################
NUM_PARAMETERS=$#
USR_PARAM=$1
HAS_PARAM=false
USE_BETA=false
VALID_INPUT=true
EXECUTE_CMD=true
INVALID_CONT_OPTION=true
XCODE_TITLE="Xcode"
XCODE_BETA_TITLE="Xcode Beta"
USR_XCODE_TITLE=""
USR_ERR=""
NUM_RUNS=0
UP_KEY="\027[A"
DOWN_KEY="\027[B"
LEFT_KEY="\027[D"
RIGHT_KEY="\027[C"
DEFAULT_XCODE_APP="/Applications/Xcode.app"
DEFAULT_XCODE_BETA_APP="/Applications/Xcode-beta.app"
HEADER_TPUT=6
DEFAULT_TPUT=2
ERR_TPUT=1
PROMPT_TPUT=3
HAS_XCODE=false
HAS_XCODE_BETA=false
HAS_XCODE_VERSIONS=false
ERR_XCODE_BETA_NOT_FOUND="The required Xcode beta was not found on this computer"
ERR_XCODE_NOT_FOUND="The required standard (non-beta) installation of Xcode was not found on this computer"
init() {
if [ ${NUM_RUNS} -eq 0 ]; then
clear
tput setaf $HEADER_TPUT
echo ""
echo ""
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ">>>"
echo ">>>"
echo ">>>"
echo ">>> ** XCODE VERSION SELECTOR **"
echo ">>>"
echo ">>>"
echo ">>> (c) Tyler Hostager, 2017."
echo ">>> All Rights Reserved."
echo ">>>"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ""
echo ""
tput sgr0
fi
# Check for both original and beta Xcode applications at the default path
scan_drive_for_xcode_versions
# print current versions to console so it is clear which one they are using
disp_enabled_xcode_version
if [ ${NUM_RUNS} -ne 0 ]; then
while [ ${INVALID_CONT_OPTION} == true ]; do
tput setaf $PROMPT_TPUT
read -rp '>>> Continue? (y/n): ' -n 1 key
tput sgr0
echo ""
if [ "$key" == "n" ]; then
EXECUTE_CMD=false
INVALID_CONT_OPTION=false
return
elif [ "$key" == "y" ]; then
EXECUTE_CMD=true
INVALID_CONT_OPTION=false
else
USR_ERR="Invalid input--please select a valid option"
disp_usr_err
INVALID_CONT_OPTION=true
fi
tput setaf $DEFAULT_TPUT
done
fi
while [ ${EXECUTE_CMD} == true ]; do
NUM_RUNS=1
tput setaf $PROMPT_TPUT
read -rsp '>>> Press any key to continue...' -n 1 key
tput setaf $DEFAULT_TPUT
# ensure that any parameters entered are valid
validate_usr_input
if [ ${VALID_INPUT} == true ]; then
assign_xcode_title
enable_xcode_version
else
disp_usr_err
fi
done
}
scan_drive_for_xcode_versions() {
tput setaf $DEFAULT_TPUT
echo ">>> Scanning drive for Xcode installations..."
if [ -d ${DEFAULT_XCODE_APP} ] && [ -d ${DEFAULT_XCODE_BETA_APP} ]; then
HAS_XCODE=true
HAS_XCODE_BETA=true
HAS_XCODE_VERSIONS=true
echo ">>> Both \""${XCODE_TITLE}"\" and \""${XCODE_BETA_TITLE}"\" were found on this computer"
echo ">>> Continuing execution"
return
elif [ -d ${DEFAULT_XCODE_APP} ]; then
HAS_XCODE=true
HAS_XCODE_BETA=false
HAS_XCODE_VERSIONS=false
USR_ERR=ERR_XCODE_BETA_NOT_FOUND
elif [ -d ${DEFAULT_XCODE_BETA_APP} ]; then
HAS_XCODE=false
HAS_XCODE_BETA=true
HAS_XCODE_VERSIONS=false
USR_ERR=ERR_XCODE_NOT_FOUND
fi
EXECUTE_CMD=false
disp_usr_err # shows error and exits execution due to missing required software
}
disp_enabled_xcode_version() {
tput setaf $PROMPT_TPUT
echo ">>> Current enabled Xcode version: "
tput sgr0
xcode-select -p
tput setaf $PROMPT_TPUT
echo ">>> Current Swift version: "
tput sgr0
swift -version
}
validate_usr_input() {
tput setaf $DEFAULT_TPUT
echo ""
echo ">>> validating input parameters..."
if [ $# -ne 0 ]; then
if [ ${NUM_PARAMETERS} -eq 1 ]; then
HAS_PARAM=true
if [ -z ${USR_PARAM} ]; then
HAS_PARAM=false
else
if [ ${USR_PARAM} == "-b" ] || [ ${USR_PARAM} == "--beta" ]; then
VALID_INPUT=true
USE_BETA=true
return
elif [ "${USR_PARAM}" == "-s" ] || [ "${USR_PARAM}" == "--standard" ]; then
VALID_INPUT=true
USE_BETA=false
return
else
if [ "${USR_PARAM}" != "n" ] && [ "${USR_PARM}" != "y" ]; then
HAS_PARAM=false
USR_ERR="Invalid parameter specifications. Please enter a valid option"
VALID_INPUT=false
disp_usr_err
return
else
VALID_INPUT=true
HAS_PARAM=true
if [ "${USR_PARAM}" == "y" ]; then
USE_BETA=true
return
elif [ "${USR_PARAM}" == "n" ]; then
USE_BETA=false
return
fi
fi
fi
fi
else
USR_ERR="Invalid number of parameters"
disp_usr_err
fi
fi
echo ">>> no parameters specified"
tput setaf $PROMPT_TPUT
read -rp '>>> Enable Xcode beta? (y/n): ' -n 1 key
tput sgr0
USR_PARAM="$key"
tput setaf $DEFAULT_TPUT
if [ ${HAS_PARAM} == false ]; then
if [ "${USR_PARAM}" != "y" ] && [ "${USR_PARAM}" != "n" ]; then
VALID_INPUT=false
return
elif [ ${USR_PARAM} == "y" ]; then
VALID_INPUT=true
USE_BETA=true
elif [ ${USR_PARAM} == "n" ]; then
VALID_INPUT=true
USE_BETA=false
else
USR_ERR="An unknown error occurred"
disp_usr_err
fi
fi
if [ ${VALID_INPUT} == true ]; then
echo ""
echo ">>> User input validated successfully"
fi
tput sgr0
}
assign_xcode_title() {
tput setaf $DEFAULT_TPUT
echo ">>> Assigning title..."
if [ ${VALID_INPUT} == true ]; then
if [ ${USE_BETA} == true ]; then
USR_XCODE_TITLE=${XCODE_BETA_TITLE}
else
USR_XCODE_TITLE=${XCODE_TITLE}
fi
echo ">>> Title successfully assigned --> \"${USR_XCODE_TITLE}\""
else
USR_ERR="Invalid input. Please enter a valid option"
disp_usr_err
fi
tput sgr0
}
disp_usr_err() {
if [ "${USR_ERR}" != "" ]; then
tput setaf $ERR_TPUT
echo "ERROR: ${USR_ERR}"
tput setaf $DEFAULT_TPUT
if [ ${EXECUTE_CMD} == true ]; then
echo "Restarting procedure"
else
echo "Terminating operation"
terminate
fi
init
fi
tput srg0
}
enable_xcode_version() {
tput setaf $DEFAULT_TPUT
echo ">>> This command may need root/administrative privelages in order to be executed..."
# check for admin permisisons
tput setaf $DEFAULT_TPUT
echo ">>> Attempting to enable \""${USR_XCODE_TITLE}"\"... "
tput sgr0
if [ ${USE_BETA} == true ]; then
sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
else
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
fi
tput setaf $HEADER_TPUT
echo ">>> Command executed successfully"
tput sgr0
EXECUTE_CMD=false
init
}
trap_ctrlc() {
tput setaf $DEFAULT_TPUT
echo ""
echo ">>> Cleaning..."
clean_stdin
echo ">>> Cleaned successfully"
echo ">>> Exiting application"
tput sgr0
# end script
exit 2
}
clean_stdin() {
TMP=""
#while [ read -r -t 0 ]; do
# read -n 256 -r -s
#done
}
terminate() {
clean_stdin
tput sgr0 # <-- reset tput
exit 1
}
# captures 'ctrl-c' keycode and exits the program appropriately instead of halting
trap trap_ctrlc 2
# initializes the program
init