-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.sh
executable file
·221 lines (177 loc) · 5.71 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
#!/bin/sh
# bootstrap.sh -- install the dwiw2015 Vim distribution
# See the Github page (https://github.com/mkropat/vim-dwiw2015) for details
# It should be safe to run -- even on an existing Vim set up. Re-running the
# `bootstrap.sh` script is safe — it simply resumes the installation and checks
# for updates on any plug-ins already installed.
main() {
parse_options "$@" &&
check_for_prerequisites &&
locate_paths &&
ensure_vundle_installed &&
# Create `~/.vim/dwiw-loader.vim` script to load Vundle and then call
# `bundles.vim`. Do not modify the loader file.
ensure_loader_file_exists &&
# Create `~/.vim/bundles.vim` script, which contains a list of Vundle
# plug-ins. Feel free to make local modifications to this file.
ensure_bundles_file_populated &&
# Prepand a one-line hook in `~/.vimrc` to call `dwiw-loader.vim`.
ensure_vimrc_hook_exists &&
install_or_update_bundles &&
msg 'Bootstrap successful! Run `vim` or `gvim` to get started'
}
parse_options() {
quiet=--quiet
while getopts v opt; do
case "$opt" in
v) # be verbose
quiet=
;;
esac
done
shift $(( OPTIND - 1 ))
}
check_for_prerequisites() {
local missing=''
for command in git vim; do
if ! hash "$command" 2>/dev/null; then
missing="$missing $command"
fi
done
if [ -n "$missing" ]; then
die "Error: missing dependencies:$missing"
fi
}
locate_paths() {
myvimrc_path=$(resolve_symlinks ~/.vimrc)
local myvimfiles_path myvimfiles_tlde
myvimfiles_path=~/.vim
myvimfiles_tlde="~/.vim"
loader_file_path=$(resolve_symlinks "$myvimfiles_path/dwiw-loader.vim")
loader_file_tlde="$myvimfiles_tlde/dwiw-loader.vim"
bundles_file_path=$(resolve_symlinks "$myvimfiles_path/bundles.vim")
bundles_file_tlde="$myvimfiles_tlde/bundles.vim"
vundle_path=$(resolve_symlinks "$myvimfiles_path/bundle/vundle")
vundle_tlde="$myvimfiles_tlde/bundle/vundle"
}
ensure_vundle_installed() {
if [ -d "$vundle_path/.git" ]; then
return
fi
msg "Installing Vundle into $vundle_path"
git clone "$quiet" https://github.com/gmarik/vundle.git "$vundle_path" ||
die "Error: unable to clone Vundle repo"
}
ensure_loader_file_exists() {
local loader_version="1.1"
if [ ! -e "$loader_file_path" ]; then
msg "Creating loader script at '$loader_file_path'"
write_loader_script_to "$loader_file_path" "$loader_version" ||
die "Error: unable to create loader script at $loader_file_path"
elif [ "$(get_script_version "$loader_file_path")" != "$loader_version" ]; then
msg "Upgrading loader script at '$loader_file_path' to latest version"
write_loader_script_to "$loader_file_path" "$loader_version" ||
die "Error: unable to create loader script at $loader_file_path"
fi
}
get_script_version() {
perl -ne '/^" Version: (.*)/ && do { print "$1\n"; exit }' "$1" 2>/dev/null
}
write_loader_script_to() {
local script="\" dwiw-loader.vim - Load Vundle and tell it about bundles
\" Version: $2
set nocompatible
filetype off
set rtp+=$vundle_tlde/
call vundle#rc()
source $bundles_file_tlde
filetype plugin indent on
runtime! plugin/sensible.vim
runtime! plugin/dwiw2015.vim"
printf '%s\n' "$script" >|"$1"
}
ensure_bundles_file_populated() {
if [ ! -e "$bundles_file_path" ]; then
msg "Creating bundles file at $bundles_file_path"
fi
# Migrate from old Vundle interface to new interface
perl -pi -e 's/^Bundle /Plugin /' "$bundles_file_path"
ensure_bundle_line 'gmarik/vundle'
ensure_bundle_line 'tpope/vim-sensible'
ensure_bundle_line 'mkropat/vim-dwiw2015'
ensure_bundle_line 'bling/vim-airline'
ensure_bundle_line 'ctrlpvim/ctrlp.vim'
ensure_bundle_line 'mileszs/ack.vim'
ensure_bundle_line 'tpope/vim-commentary'
ensure_bundle_line 'tpope/vim-sleuth'
}
ensure_bundle_line() {
if ! fgrep -qe "$1" "$bundles_file_path" 2>/dev/null; then
printf "Plugin '%s'\n" "$1" >>"$bundles_file_path"
fi
}
ensure_vimrc_hook_exists() {
if ! grep -qe "^source $loader_file_tlde" "$myvimrc_path" 2>/dev/null; then
msg "Adding hook to .vimrc"
prepend_line "source $loader_file_tlde" "$myvimrc_path" ||
die "Error: unable to add loader hook to $myvimrc_path"
fi
}
install_or_update_bundles() {
msg "Calling Vundle's :PluginInstall!"
echo | vim -u "$loader_file_path" +PluginInstall! +qall - ||
die "Error: unable to start vim"
}
msg() {
printf '%s\n' "$*"
}
die() {
printf '%s\n' "$*"
exit 1
}
resolve_symlinks() {
_resolve_symlinks "$1"
}
_resolve_symlinks() {
_assert_no_path_cycles "$@" || return
local dir_context path
path=$(readlink -- "$1")
if [ $? -eq 0 ]; then
dir_context=$(dirname -- "$1")
_resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@"
else
printf '%s\n' "$1"
fi
}
_prepend_dir_context_if_necessary() {
if [ "$1" = . ]; then
printf '%s\n' "$2"
else
_prepend_path_if_relative "$1" "$2"
fi
}
_prepend_path_if_relative() {
case "$2" in
/* ) printf '%s\n' "$2" ;;
* ) printf '%s\n' "$1/$2" ;;
esac
}
_assert_no_path_cycles() {
local target path
target=$1
shift
for path in "$@"; do
if [ "$path" = "$target" ]; then
return 1
fi
done
}
prepend_line() {
local tempfile=$(make_tempfile)
touch "$2"
printf '%s\n' "$1" | cat - "$2" >"$tempfile" && mv "$tempfile" "$2"
}
make_tempfile() {
mktemp "${TMPDIR:-/tmp}/dwiw2015.XXXXX"
}
main "$@"