-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.sh
executable file
·276 lines (239 loc) · 7.27 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
#!/usr/bin/env bash
DOTFILE_REPO_GITHUB_LINK=https://raw.githubusercontent.com/mansour-ahmed/dotfiles/master
DOTFILES_REPO=~/dotfiles
main() {
# Required to install packages
ask_for_sudo
install_xcode_command_line_tools
# Gets dotfiles latest repo from master branch
clone_dotfiles_repo
# To manage packages
install_homebrew
install_packages_with_brewfile
setup_zsh
setup_symlinks
update_hosts_file
setup_macOS_defaults
configure_vscode
}
function ask_for_sudo() {
info "Prompting for sudo password"
if sudo --validate; then
# Keep-alive
while true; do sudo --non-interactive true; \
sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
success "Sudo credentials updated"
else
error "Obtaining sudo credentials failed"
exit 1
fi
}
function install_xcode_command_line_tools() {
info "Installing Xcode command line tools"
if softwareupdate --history | grep --silent "Command Line Tools"; then
success "Xcode command line tools already exists"
else
xcode-select --install
while true; do
if softwareupdate --history | grep --silent "Command Line Tools"; then
success "Xcode command line tools installation succeeded"
break
else
substep "Xcode command line tools still installing..."
sleep 20
fi
done
fi
}
function clone_dotfiles_repo() {
info "Cloning dotfiles repository into ${DOTFILES_REPO}"
if test -e $DOTFILES_REPO; then
substep "${DOTFILES_REPO} already exists"
pull_latest $DOTFILES_REPO
success "Pull successful in ${DOTFILES_REPO} repository"
else
url=https://github.com/mansour-ahmed/dotfiles.git
if git clone "$url" $DOTFILES_REPO; then
success "Cloned into ${DOTFILES_REPO}"
else
error "Cloning into ${DOTFILES_REPO} failed"
exit 1
fi
fi
}
function pull_latest() {
substep "Pulling latest changes in ${1} repository"
if git -C $1 pull origin master &> /dev/null; then
return
else
error "Please pull the latest changes in ${1} repository manually"
fi
}
function install_homebrew() {
info "Installing Homebrew"
if hash brew 2>/dev/null; then
success "Homebrew already exists"
else
url=https://raw.githubusercontent.com/Homebrew/install/master/install
if yes | /usr/bin/ruby -e "$(curl -fsSL ${url})"; then
success "Homebrew installation succeeded"
brew analytics off
else
error "Homebrew installation failed"
exit 1
fi
fi
}
function install_packages_with_brewfile() {
info "Installing Brewfile packages"
TAP=${DOTFILES_REPO}/brew/Brewfile_tap
BREW=${DOTFILES_REPO}/brew/Brewfile_brew
CASK=${DOTFILES_REPO}/brew/Brewfile_cask
if hash parallel 2>/dev/null; then
substep "parallel already exists"
else
if brew install parallel &> /dev/null; then
printf 'will cite' | parallel --citation &> /dev/null
substep "parallel installation succeeded"
else
error "parallel installation failed"
exit 1
fi
fi
if (echo $TAP; echo $BREW; echo $CASK) | parallel --verbose --linebuffer -j 4 brew bundle check --file={} &> /dev/null; then
success "Brewfile packages are already installed"
else
if brew bundle --file="$TAP"; then
substep "Brewfile_tap installation succeeded"
export HOMEBREW_CASK_OPTS="--no-quarantine"
if (echo $BREW; echo $CASK) | parallel --verbose --linebuffer -j 3 brew bundle --file={}; then
success "Brewfile packages installation succeeded"
else
error "Brewfile packages installation failed"
exit 1
fi
else
error "Brewfile_tap installation failed"
exit 1
fi
fi
}
function setup_zsh() {
info "Setting up zsh"
current_dir=$(pwd)
cd ${DOTFILES_REPO}/zsh
if bash config.sh; then
cd $current_dir
success "zsh updated successfully"
else
cd $current_dir
error "zsh update failed"
exit 1
fi
}
function setup_symlinks() {
info "Setting up symlinks"
symlink "karabiner" ${DOTFILES_REPO}/karabiner ~/.config/karabiner
success "Symlinks successfully setup"
}
function symlink() {
application=$1
point_to=$2
destination=$3
destination_dir=$(dirname "$destination")
if test ! -e "$destination_dir"; then
substep "Creating ${destination_dir}"
mkdir -p "$destination_dir"
fi
if rm -rf "$destination" && ln -s "$point_to" "$destination"; then
substep "Symlinking for \"${application}\" done"
else
error "Symlinking for \"${application}\" failed"
exit 1
fi
}
function update_hosts_file() {
info "Updating /etc/hosts"
own_hosts_file_path=${DOTFILES_REPO}/hosts/own_hosts
downloaded_hosts_file_path=/etc/downloaded_hosts_file
if sudo cp "${own_hosts_file_path}" /etc/hosts; then
substep "Copying ${own_hosts_file_path} to /etc/hosts succeeded"
else
error "Copying ${own_hosts_file_path} to /etc/hosts failed"
exit 1
fi
if sudo wget --timeout=5 --tries=1 --quiet --output-document="${downloaded_hosts_file_path}" \
https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts; then
substep "hosts file downloaded successfully"
if cat "${downloaded_hosts_file_path}" | \
sudo tee -a /etc/hosts > /dev/null; then
success "/etc/hosts updated"
else
error "Failed to update /etc/hosts"
exit 1
fi
else
error "Failed to download hosts file"
fi
}
function setup_macOS_defaults() {
info "Updating macOS defaults"
current_dir=$(pwd)
cd ${DOTFILES_REPO}/macOS
if bash defaults.sh; then
cd $current_dir
success "macOS defaults updated successfully"
else
cd $current_dir
error "macOS defaults update failed"
exit 1
fi
}
function configure_vscode() {
info "Configuring VSCode"
current_dir=$(pwd)
cd ${DOTFILES_REPO}/vscode
if bash configure_vscode.sh; then
cd $current_dir
success "VSCode successfully configured"
else
cd $current_dir
error "VSCode configuration failed"
exit 1
fi
}
# Utils
function coloredEcho() {
local exp="$1";
local color="$2";
local arrow="$3";
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput bold;
tput setaf "$color";
echo "$arrow $exp";
tput sgr0;
}
function info() {
coloredEcho "$1" blue "========>"
}
function substep() {
coloredEcho "$1" magenta "===="
}
function success() {
coloredEcho "$1" green "========>"
}
function error() {
coloredEcho "$1" red "========>"
}
main "$@"