-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulators.sh
executable file
·153 lines (124 loc) · 5.48 KB
/
simulators.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
#!/bin/bash
# Script to install specific Xcode simulators
# Author: richard at richard - purves dot com
# Version: 1.0 - 4th February 2017
# xcode-installer curtesy of https://github.com/KrauseFx/xcode-install/
# Without that, terminal notifier and cocoaDialog this script wouldn't be possible.
# Set up variables and functions here
consoleuser="$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')"
brandid="com.application.id"
tn="/path/to/terminal-notifier.app/Contents/MacOS/terminal-notifier"
cd="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog"
# Logging stuff starts here
LOGFOLDER="/private/var/log/"
LOG=$LOGFOLDER"XcodeSimulators.log"
if [ ! -d "$LOGFOLDER" ];
then
mkdir $LOGFOLDER
fi
function logme()
{
# Check to see if function has been called correctly
if [ -z "$1" ]
then
echo $( date )" - logme function call error: no text passed to function! Please recheck code!"
echo $( date )" - logme function call error: no text passed to function! Please recheck code!" >> $LOG
exit 1
fi
# Log the passed details
echo -e $( date )" - $1" >> $LOG
echo -e $( date )" - $1"
}
# Check and start logging - done twice for local log and for JAMF
logme "Xcode Simulator Installation"
# Log the current console user
logme "Current user: $consoleuser"
# Is Xcode actually installed?
if [ ! -d "/Applications/Xcode.app" ];
then
su -l "$consoleuser" -c " "'"'$tn'"'" -sender "'"'$brandid'"'" -title "'"Xcode Install"'" -message "'"Xcode not present. Please install."'" "
logme "Xcode is not present. Stopping."
exit 0
fi
# Set IFS as we mean to go on, so the space line splitting doesn't occur.
OIFS=$IFS
IFS=$'\n'
# Let's start here by caffinating the mac so it stays awake or bad things happen.
caffeinate -d -i -m -u &
caffeinatepid=$!
logme "Caffinating the mac under process id: $caffeinatepid"
# Is xcode-install installed?
if [ ! -f "/usr/local/bin/xcversion" ];
then
# Install xcode-install so we can deal with simulators and documentation
su -l "$consoleuser" -c " "'"'$tn'"'" -sender "'"'$brandid'"'" -title "'"Xcode Install"'" -message "'"Installing missing helper tools"'" "
logme "Installing missing helper tools."
curl -sL -O https://github.com/neonichu/ruby-domain_name/releases/download/v0.5.99999999/domain_name-0.5.99999999.gem 2>&1 | tee -a ${LOG}
sleep 3
gem install domain_name-0.5.99999999.gem | tee -a ${LOG}
sleep 3
gem install --conservative xcode-install | tee -a ${LOG}
sleep 3
rm -f domain_name-0.5.99999999.gem | tee -a ${LOG}
fi
# Clean up anything already cached
logme "Cleaning up any previously cached downloads"
/usr/local/bin/xcversion cleanup | tee -a ${LOG}
# Start the installation here
su -l "$consoleuser" -c " "'"'$tn'"'" -sender "'"'$brandid'"'" -title "'"Xcode Install"'" -message "'"Getting available simulators"'" "
logme "Getting available simulators"
# Declare the not installed array
declare -a not_installed
# Read through the output of xcversion, skipping the first line. Look for "not installed".
# If so, add them to the array.
logme "Processing not installed simulators into an array"
while read line;
do
if [[ ${line} =~ '(not installed)' ]];
then
not_installed+=( "${line/' (not installed)'/}" )
logme "Adding to checkbox list: $line"
fi
done < <( /usr/local/bin/xcversion simulators | awk 'NR>1' )
# Display list of simulators that aren't installed
# Ask for which ones to install
dialog=$($cd checkbox --title "Xcode Simulators" \
--text "Please choose:" \
--button1 "Install" \
--items $(for (( i=0; i<${#not_installed[@]}; i++ )); do echo "${not_installed[$i]}" ; done) )
# Strip the first element from the dialog return, as we don't need it. It refers to the output from clicking the install button.
# Should we test for cancel? Maybe a later version.
dialog=$( echo $dialog | cut -c2- )
# Declare choice array and read dialog choices into it
IFS=' ' read -r -a choices <<< "$dialog"
# Compare the number of not installed to that pulled from the cocoadialog dialog variable
[ ${#not_installed[*]} != ${#choices[*]} ] && { logme "Arrays are different sizes"; exit 1; }
# Do the comparison. If 1, it means it's selected and should be added to the install_me array
# Now we're creating this install_me array so we can be sure it's blank. Otherwise the code gets REALLY messy.
# Declare the install_me array
declare -a install_me
for (( i=0; i<${#choices[@]}; i++ ));
do
logme "Processing ${not_installed[$i]}"
if [ "${choices[$i]}" == "1" ]
then
logme "Adding ${not_installed[$i]} to install list"
install_me+=( ${not_installed[$i]} )
fi
done
# Go through the list, get xcode-install to download and install each one that's missing and specified.
for sim in "${install_me[@]}"
do
logme "Installing $sim"
su -l "$consoleuser" -c " "'"'$tn'"'" -sender "'"'$brandid'"'" -title "'"Xcode Install"'" -message "'"Installing '$sim'"'" "
/usr/local/bin/xcversion simulators --install="$sim" 2>&1 | tee -a ${LOG}
done
# IFS back to normal please.
IFS=$OIFS
# Notify user that all is completed
logme "Installations complete"
su -l "$consoleuser" -c " "'"'$tn'"'" -sender "'"'$brandid'"'" -title "'"Xcode Install"'" -message "'"Installations complete"'" "
# No more caffeine please. I've a headache. Especially after all that array comparison work.
kill "$caffeinatepid"
# All done!
exit 0