-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.sh
executable file
·288 lines (231 loc) · 8.15 KB
/
Bootstrap.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
279
280
281
282
283
284
285
286
287
288
#!/bin/sh
readonly arguments=$@
readonly update_gemfile_flag='--update-gemfile'
readonly update_cartfile_flag='--update-cartfile'
readonly project_path="$( pwd )"
readonly required_macos_version='10.14.0'
readonly required_ruby_version=$(cat .ruby-version)
readonly homebrew_url='https://raw.githubusercontent.com/Homebrew/install/master/install'
readonly rbenv_doctor_url='https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor'
readonly rbenv_doctor_temp_path="${project_path}/rbenv_doctor"
readonly profile_rbenv_init_line='eval "$(rbenv init -)"'
readonly profile_rbenv_path_line='export PATH="$HOME/.rbenv/bin:$PATH"'
readonly default_style='\033[0m'
readonly warning_style='\033[33m'
readonly error_style='\033[31m'
readonly project_style='\033[38;5;150m'
readonly macos_style='\033[38;5;99m'
readonly xcode_style='\033[38;5;75m'
readonly homebrew_style='\033[38;5;208m'
readonly rbenv_style='\033[38;5;43m'
readonly ruby_style='\033[38;5;89m'
readonly bundler_style='\033[38;5;45m'
readonly carthage_style='\033[38;5;222m'
readonly congratulations_style='\033[38;5;48m'
cleanup() {
rm -rf $rbenv_doctor_temp_path;
}
failure() {
echo "${error_style}Fatal error:${default_style} '$1' failed with exit code $2"
exit 1
}
warning() {
echo "${warning_style}Warning:${default_style} '$1' failed with exit code $2"
}
assert_failure() {
eval $1 2>&1 | sed -e "s/^/ /"
local exit_code=${PIPESTATUS[0]}
if [ $exit_code -ne 0 ]; then
failure "$1" $exit_code
fi
}
assert_warning() {
eval $1 2>&1 | sed -e "s/^/ /"
local exit_code=${PIPESTATUS[0]}
if [ $exit_code -ne 0 ]; then
warning "$1" $exit_code
fi
}
plain_version() {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d", $1,$2,$3,$4); }'
}
########################################################################################
welcome_message_step() {
echo "${default_style}"
echo "------------------------------------------------------------------"
echo "--- ---"
echo "--- Welcome to ${project_style}AurorKit${default_style}"'!'" ---"
echo "--- ---"
echo "------------------------------------------------------------------"
echo ""
echo "This script will set up your development environment."
echo "This might take a few minutes. Please don't interrupt the script."
}
macos_version_step() {
echo ""
echo "Checking ${macos_style}macOS${default_style} version:"
macos_version=$(/usr/bin/sw_vers -productVersion 2>&1)
if [ "$(plain_version $macos_version)" -lt "$(plain_version $required_macos_version)" ]; then
echo " ${error_style}Your macOS version ($macos_version) is older then required version ($required_macos_version). Exiting...${default_style}"
exit 1
else
echo " Your macOS version: $macos_version"
fi
}
xcode_command_line_tools_step() {
echo ""
echo "Checking ${xcode_style}Xcode Command Line Tools${default_style} installation:"
if xcode-select --version &> /dev/null; then
echo " Xcode Command Line Tools already installed."
else
echo " Xcode Command Line Tools not found. Installing..."
assert_failure 'xcode-select --install'
fi
}
homebrew_step() {
echo ""
echo "Checking ${homebrew_style}Homebrew${default_style} installation:"
if which -s brew; then
echo " Homebrew already installed. Updating..."
assert_failure 'brew update'
else
echo " Homebrew not found. Installing..."
assert_failure 'ruby -e "$(curl -fsSL $homebrew_url)" < /dev/null'
fi
echo ""
echo " Verifying that Homebrew is properly set up..."
assert_warning 'brew doctor'
}
rbenv_step() {
echo ""
echo "Checking ${rbenv_style}rbenv${default_style} installation:"
if brew ls --versions rbenv &> /dev/null; then
echo " rbenv already installed. Updating..."
brew_outdated=$(brew outdated 2> /dev/null)
brew_outdated_exit_code=$?
if [ $brew_outdated_exit_code -ne 0 ]; then
echo " Failed to find outdated formulae."
warning 'brew outdated' $brew_outdated_exit_code
else
if [[ $brew_outdated == *"rbenv"* ]]; then
assert_failure 'brew upgrade rbenv'
else
echo " Already up-to-date."
fi
fi
else
echo " rbenv not found. Installing..."
assert_failure 'brew install rbenv'
fi
assert_warning 'rbenv rehash'
bash_profile_content=$(grep rbenv ~/.bash_profile 2> /dev/null)
if [[ $bash_profile_content != *"$profile_rbenv_init_line"* ]]; then
echo $profile_rbenv_init_line >> ~/.bash_profile
fi
if [ -n "$ZSH_VERSION" ]; then
zsh_profile_content=$(grep rbenv ~/.zshrc 2> /dev/null)
if [[ $zsh_profile_content != *"$profile_rbenv_init_line"* ]]; then
echo $profile_rbenv_init_line >> ~/.zshrc
fi
if [[ $zsh_profile_content != *"$profile_rbenv_path_line"* ]]; then
echo $profile_rbenv_path_line >> ~/.zshrc
fi
fi
eval "$(rbenv init -)"
echo ""
echo " Verifying that rbenv is properly set up..."
curl -fsSL $rbenv_doctor_url > $rbenv_doctor_temp_path 2> /dev/null
rbenv_doctor_exit_code=$?
if [ $rbenv_doctor_exit_code -ne 0 ]; then
echo " Failed to load rbenv-doctor script."
warning 'curl -fsSL $rbenv_doctor_url' $rbenv_doctor_exit_code
else
chmod a+x $rbenv_doctor_temp_path
assert_warning 'bash $rbenv_doctor_temp_path'
fi
}
ruby_step() {
echo ""
echo "Checking ${ruby_style}Ruby${default_style} version:"
ruby_version=$(ruby -v 2>&1 | sed "s/^.*ruby \([0-9.]*\).*/\1/")
if [ "$(plain_version $ruby_version)" -lt "$(plain_version $required_ruby_version)" ]; then
echo " Your Ruby version ($ruby_version) is older then required version ($required_ruby_version). Updating..."
assert_failure 'rbenv install $required_ruby_version'
assert_warning 'rbenv rehash'
else
echo " Required Ruby version ($required_ruby_version) already installed."
fi
}
bundler_step() {
echo ""
echo "Checking ${bundler_style}Bundler${default_style} installation:"
if rbenv which bundler &> /dev/null; then
echo " Bundler already installed. Updating..."
assert_failure 'gem update bundler'
else
echo " Bundler not found. Installing..."
assert_failure 'gem install bundler'
fi
assert_warning 'rbenv rehash'
}
gemfile_step() {
echo ""
if [[ " ${arguments[*]} " == *" $update_gemfile_flag "* ]]; then
echo "Updating ${ruby_style}Ruby gems${default_style} specified in Gemfile..."
assert_failure 'bundle update'
else
echo "Installing ${ruby_style}Ruby gems${default_style} specified in Gemfile..."
assert_failure 'bundle install'
fi
}
carthage_step() {
echo ""
echo "Checking ${carthage_style}Carthage${default_style} installation:"
if brew ls --versions carthage &> /dev/null; then
echo " Carthage already installed. Updating..."
brew_outdated=$(brew outdated 2> /dev/null)
brew_outdated_exit_code=$?
if [ $brew_outdated_exit_code -ne 0 ]; then
echo " Failed to find outdated formulae."
warning 'brew outdated' $brew_outdated_exit_code
else
if [[ $brew_outdated == *"carthage"* ]]; then
assert_failure 'brew upgrade carthage'
else
echo " Already up-to-date."
fi
fi
else
echo " Carthage not found. Installing..."
assert_failure 'brew install carthage'
fi
}
cartfile_step() {
echo ""
if [[ " ${arguments[*]} " == *" $update_cartfile_flag "* ]]; then
echo "Updating ${carthage_style}Carthage${default_style} dependencies specified in Cartfile..."
assert_failure 'carthage update --use-submodules'
else
echo "Building ${carthage_style}Carthage${default_style} dependencies specified in Cartfile..."
assert_failure 'carthage build'
fi
}
congratulations_step() {
echo ""
echo ""
echo "${congratulations_style}Congratulations!${default_style} Setting up the development environment successfully completed 🥳"
echo ""
}
########################################################################################
trap cleanup EXIT
welcome_message_step
macos_version_step
xcode_command_line_tools_step
homebrew_step
rbenv_step
ruby_step
bundler_step
gemfile_step
carthage_step
cartfile_step
congratulations_step