-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·228 lines (194 loc) · 4.73 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
#!/bin/bash
set -e
cd "$(dirname "${BASH_SOURCE[0]}")"
if [ -z "$DOTFILES_REPO" ]; then
export DOTFILES_REPO="$(pwd)"
fi
source $DOTFILES_REPO/script/helpers.bash
run_install() {
local backup_dir
if [ -n "$BACKUP" ]; then
backup_dir="$HOME/dotfiles-backup-$(date +%s)"
mkdir $backup_dir
fi
local exclude=0
if [ -e "$HOME/.bootstrap.exclusions" ]; then
declare -a exclusions
readarray -t exclusions < "$HOME/.bootstrap.exclusions"
exclude=1
fi
# Find the installers and run them
find . -mindepth 2 -name install.sh | sort | while read installer; do
if [ $exclude -eq 0 ]; then
sh -c "${installer} $backup_dir"
else
local name="$(echo "$installer" | cut -d'/' -f 2)"
if [ ! "$(array_contains "$name" "$(declare -p exclusions)")" ]; then
sh -c "${installer} $backup_dir"
else
warn "Skipping $name installation"
fi
fi
done
unset exclusions
if [ -r $HOME/bin ]; then
if [ -n "$BACKUP" ]; then
mkdir $backup_dir/bin
cp -r $HOME/bin/* $backup_dir/bin/
fi
if [ -h "$HOME/bin" ]; then
unlink $HOME/bin
elif [ -d "$HOME/bin" ]; then
rm -rf $HOME/bin
fi
fi
ln -s $DOTFILES_REPO/bin $HOME/bin
}
array_contains() {
local search="$1"
eval "declare -a array="${2#*=}
local found=
for name in "${array[@]}"; do
if [[ $name == $search ]]; then
echo 1
found=1
return
fi
done
}
check_installed() {
if [ -z "$(type -P $1)" ]; then
echo 0
else
echo 1
fi
}
install_dependencies() {
e_header "Installing/upgrading dependencies..."
if [[ "$OSTYPE" =~ ^linux-gnu ]]; then
install_dependencies_debian
elif [[ "$OSTYPE" =~ ^darwin ]]; then
install_dependencies_osx
else
fail "Current platform is not supported for installing dependencies."
fi
e_success "Dependencies installed/upgraded"
}
install_dependencies_debian() {
e_arrow "Updating package index files from their sources..."
sudo apt-get update &> /dev/null
e_success "Package index files updated"
if [ -z "$UPGRADE" ]; then
local install_cmd="sudo apt-get -y -q install"
else
local install_cmd="sudo apt-get -y -q upgrade"
fi
install_dependencies_list "$install_cmd" "check_installed_debian" "packages-debian"
}
install_dependencies_osx() {
if [ ! "$(check_installed gcc)" ]; then
e_error "XCode or the Command Line Tools for XCode must be installed first"
exit 1
fi
if [ ! "$(check_installed brew)" ]; then
e_arrow "Installing Homebrew (OSX package manager)"
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
e_success "Homebrew installed"
fi
e_arrow "Updating homebrew and homebrew formulae..."
brew update &> /dev/null
e_success "Homebrew and formulae updated"
if [ -z "$UPGRADE" ]; then
local install_cmd="brew install"
else
local install_cmd="brew upgrade"
fi
install_dependencies_list "$install_cmd" "check_installed_osx" packages-osx
}
check_installed_debian() {
dpkg-query -l $1 &> /dev/null && echo 0 || echo 1
}
check_installed_osx() {
if [ "$(brew ls --versions $1)" ]; then
echo 0
else
echo 1
fi
}
# Argument 3 must be the name of a function that takes the name
# of a package and returns 0 if it is installed and 1 otherwise
install_dependencies_list() {
local install_cmd="$1"
local check="$2"
local packages_file="$3"
if [ -z "$UPGRADE" ]; then
local verb1="Installing"
local verb2="installed"
else
local verb1="Upgrading"
local verb2="upgraded"
fi
while IFS= read -r pkgname; do
if [ -n "$UPGRADE" ] || [ ! "$($check $pkgname)" ]; then
e_arrow "$verb1 $pkgname..."
$install_cmd $pkgname &> /dev/null
e_success "$pkgname $verb2"
fi
done < "$packages_file"
}
bootstrap() {
[ -z "$DEP" ] && install_dependencies
run_install
if [ -n "$BACKUP" ]; then
echo "A backup of your previous config files has been made in $backup_dir"
fi
echo "Run \`source ~/.bashrc' to reload your bashrc file."
echo "Run \`vim +PlugInstall +qall' to install vim plugins."
echo Some changes may require a terminal restart to take effect.
}
usage() {
cat << EOF
Usage: $0 [OPTION]...
Install configurations for bash, git, vim and python.
-b create a backup of current config files in home directory
-d install without checking dependencies
-f run install without prompting
-h display this help and exit
-u upgrade package manager and packages (ignored if '-d' is used)
EOF
exit 0
}
# Parse command line arguments
while getopts "bdfhu" o; do
case "${o}" in
b)
BACKUP=1
;;
d)
DEP=1
;;
f)
FORCE=1
;;
h)
usage
;;
u)
UPGRADE=1
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -n "$FORCE" ]; then
bootstrap
else
echo This script may overwrite files in your home directory.
read -r -p "Would you like to proceed? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then
bootstrap
fi
fi
exit 0