-
Notifications
You must be signed in to change notification settings - Fork 2
/
update
executable file
·292 lines (244 loc) · 9.38 KB
/
update
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
289
290
291
292
#!/usr/bin/env bash
#
# Darkcloud Vim Config Update Tool
# https://github.com/prurigro/darkcloud-vimconfig
#
# Written by Kevin MacMartin ([email protected])
# Released under the MIT license
#
#
# VARIABLES
#
error_log=update-errors.log
script_name="${0//*\/}"
script_home="${0%"$script_name"}"
if [[ -z "$script_home" ]]; then
script_home="$PWD"
else
pushd "$script_home" >/dev/null || exit
script_home="$PWD"
popd >/dev/null || exit
fi
if [[ -t 1 ]]; then
cbg_blue=$'\e[44m'
cbg_red_bold=$'\e[1;41m'
cbg_black=$'\e[40m'
cbg_yellow=$'\e[43m'
cfg_green_bold=$'\e[1;32m'
cfg_red_bold=$'\e[1;31m'
cfg_white_bold=$'\e[1;37m'
c_reset=$'\e[0m'
else
cbg_blue='#'
fi
#
# FUNCTIONS
#
# error: output and log error
function error {
printf '%s\n' "$cbg_blue $c_reset$cbg_red_bold ! ERROR: $c_reset$cfg_red_bold $2 " >&2
printf '%s\n' "$cbg_blue $c_reset$cbg_red_bold ! COMMAND: $c_reset ${cfg_white_bold}=> $1$c_reset" >&2
[[ -n "$3" ]] && printf '%s\n' "$cbg_blue $c_reset$cbg_red_bold ! OUTPUT: $c_reset$cfg_white_bold $3" >&2
printf '%s\n%s\n%s\n' "DATE: @ $(date)" "ERROR: $2" "COMMAND: $1" >> "$script_home/$error_log"
[[ -n "$3" ]] && printf '%s\n' "OUTPUT: $3" >> "$script_home/$error_log"
printf '\n' >> "$script_home/$error_log"
}
# show_version: displays version information
function show_version {
repo_version="$(printf "%s.r%s" "$(git show -s --format=%ci master | sed 's/\ .*//g;s/-//g')" "$(git rev-list --count HEAD)")"
printf '%s\n' "$script_name: darkcloud-vimconfig update tool (version: $repo_version)"
}
# show_help: this function displays help output
function show_help {
printf '\n%s\n' 'USAGE'
printf ' %s\n\n' "./$script_name [OPTION]"
printf '%s\n' 'OPTIONS'
printf ' %s\t%s\n' '-v, --version' '| output version information and exit'
printf ' %s\t%s\n\n' '-h, --help' '| display this help and exit'
printf '%s\n' 'Run with no arguments to update darkcloud-vimconfig'
}
#
# SETUP
#
if type -P timeout >/dev/null; then
timeout_command=timeout
elif type -P gtimeout >/dev/null; then
timeout_command=gtimeout
else
error 'type -P timeout' 'The timeout command could not be found (install coreutils)'
exit 1
fi
cd "$script_home" || exit
# delete old error log if it exists
[[ -f "$error_log" ]] && rm "$script_home/$error_log"
# parse for arguments (then handle them below)
[[ -n "$1" ]] && {
case "$1" in
-h|--help)
show_version
show_help
exit 0
;;
-v|--version)
show_version
exit 0
;;
*)
error "$ERROR" "$1 is not a valid option"
show_help
exit 1
;;
esac
}
# display script title
printf '\n%s\n' "$cbg_black ~~~ DarkCloud Vimconfig Update Tool ~~~ $c_reset"
# create vim/bundle.user if it doesn't exist
[[ -d 'vim/bundle.user' ]] || {
printf '\n%s' "$cbg_blue >> Creating user plugin directory:$c_reset"
process_status="$(install -d 'vim/bundle.user' 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'install -d vim/bundle.user' "User plugin directory couldn't be created" "$process_status"
fi
}
# create vim/vimrc.user if it doesn't exist
[[ -e 'vim/vimrc.user' ]] || {
printf '\n%s' "$cbg_blue >> Creating user config file:$c_reset"
process_status="$(touch 'vim/vimrc.user' 2>&1)"
if (( ! $? )); then
{
printf '%s\n%s\n\n' '"Autocheck syntax checking: (1:start toggled on | *0:start toggled off)' '"let g:autostartchecker = 0'
printf '%s\n%s\n\n' '"Autoload Tagbar: (1:start open | *0:start closed)' '"let g:autostarttagbar = 0'
printf '%s\n%s\n\n' '"Disable Completion: (1:disable completion | *0:enable completion if requirements are met)' '"let g:disablecompletion = 0'
printf '%s\n%s\n\n' '"Disable automatic tag generation and highlighting: (1:force disabled tag generation and highlighting | *0:enable automatic tag generation and highlighting)' '"let g:disableautotags = 0'
printf '%s\n%s\n\n' '"Disable automatic linebreaking: (1:force disabled globally | *0:let the filetype decide)' '"let g:disablelinebreaks = 0'
printf '%s\n%s\n\n' '"Enable Powerline fonts: (1:expect powerline font | *0:expect regular font)' '"let g:powerlinefonts = 0 "(set powerline font for gvim and terminal when enabled)'
printf '%s\n%s\n' '"GVim font selection: (Escaping spaces and use powerline if appropriate)' '"set guifont=Monospace\ 12'
} >> vim/vimrc.user
if [[ -e 'vim/vimrc.user' ]]; then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
fi
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error "touch vim/vimrc.user" "User config couldn't be created" "$process_status"
fi
}
#
# REPO UPDATE
#
printf '\n%s' "$cbg_blue >> Updating darkcloud-vimconfig:$c_reset"
process_status="$(git pull origin master 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'git pull origin master' 'Git failed to sync the repo' "$process_status"
exit 1
fi
#
# SUBMODULE UPDATE
#
printf '\n%s\n' "$cbg_blue >> Updating plugin submodules >> $c_reset"
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Updating plugin URLs:$c_reset"
process_status="$(git submodule sync 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'git submodule sync' 'Git failed to sync the submodules' "$process_status"
fi
# update each submodule to the new head and run 'git fetch --all'
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Fetching updates:$c_reset"
git submodule foreach git reset --hard >/dev/null 2>&1
process_status="$(git submodule foreach git fetch --all 2>&1)"
if (( ! $? )); then
process_status=$(git submodule update --init --recursive 2>&1)
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'git submodule update --init --recursive' 'Git failed to update the submodules' "$process_status"
fi
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'git submodule foreach git fetch --all' "Git failed to fetch the submodules from their respective remotes" "$process_status"
fi
# clean plugin directories and remove plugins no longer in the repo
printf '\n%s\n' "$cbg_blue >> Clean plugin directories >> $c_reset"
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Remove untracked files:$c_reset"
process_status="$(git submodule foreach git clean -dxf 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error 'git submodule foreach git clean -dxf' 'Git failed to remove untracked files' "$process_status"
fi
[[ -f '.gitmodules' ]] && {
for plugin in vim/bundle/*; do
[[ -f "$plugin/.git" ]] && {
plugin_dirname="${plugin/*\/}"
grep 'path = ' .gitmodules | grep -oe '[^\/]*$' | grep -qe "^$plugin_dirname$" || {
[[ -z "$first_found" ]] && {
first_found=1
printf '%s\n' "$cbg_blue $c_reset$cbg_yellow + Removing old plugins: $c_reset"
}
process_status="$(rm -rf "${plugin:?}")"
if (( ! $? )); then
printf '%s\n' "$cbg_blue $c_reset$cbg_yellow = $cfg_white_bold$plugin$c_reset"
else
error "rm -rf $plugin" "Folder couldn't be deleted" "$process_status"
exit 1
fi
}
}
done
printf '\n'
}
#
# USER PLUGIN UPDATE
#
[[ -d vim/bundle.user ]] && find vim/bundle.user -mindepth 3 -maxdepth 3 -name config | grep -q '.git/config' && {
printf '%s\n' "$cbg_blue >> Updating user plugins >> $c_reset"
pushd 'vim/bundle.user' >/dev/null || exit
for plugin in *; do
[[ -d "$plugin/.git" ]] && {
pushd "$plugin" >/dev/null || exit
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Updating 'vim/bundle.user/$plugin':$c_reset"
process_status="$(git pull 2>&1)"
if (( ! $? )); then
if ! grep -q "Already up-to-date" <<< "$process_status"; then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_white_bold UP TO DATE $c_reset"
fi
else
error 'git pull' "Failed pulling changes for $plugin" "$process_status"
fi
popd >/dev/null || exit
}
done
popd >/dev/null || exit
printf '\n'
}
#
# GENERATE PLUGIN HELP
#
type -P vim >/dev/null && {
printf '%s' "$cbg_blue >> Generating plugin help:$c_reset"
$timeout_command --preserve-status --foreground 1m vim -u ./vimrc -c 'Helptags|qa!'
if (( ! $? )); then
printf '%s\n' "$cfg_green_bold SUCCESS! $c_reset"
else
reset -I
printf '%s\n' "$cfg_red_bold FAIL! $c_reset"
error "vim -u './vimrc' -c 'Helptags|qa!'" 'Generating helpdocs for the submodules failed'
fi
}
#
# FINISH
#
printf '\n%s\n\n' "$cbg_black ~~~ Update Complete ~~~ $c_reset"