-
Notifications
You must be signed in to change notification settings - Fork 99
/
zgen.zsh
executable file
·501 lines (409 loc) · 13 KB
/
zgen.zsh
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/bin/zsh
# vim: set ft=zsh fenc=utf-8 noai ts=8 et sts=4 sw=0 tw=80 nowrap :
local ZGEN_SOURCE="$0:A:h"
-zgputs() { printf %s\\n "$@" ;}
-zgpute() { printf %s\\n "-- zgen: $*" >&2 ;}
-zginit() { -zgputs "$*" >> "${ZGEN_INIT}" ;}
if [[ -z "${ZGEN_DIR}" ]]; then
ZGEN_DIR="${HOME}/.zgen"
fi
if [[ -z "${ZGEN_INIT}" ]]; then
ZGEN_INIT="${ZGEN_DIR}/init.zsh"
fi
# The user can explicitly disable Zgen attempting to invoke `compinit`, or it
# will be automatically disabled if `compinit` appears to have already been
# invoked.
if [[ -z "${ZGEN_AUTOLOAD_COMPINIT}" && -z "${(t)_comps}" ]]; then
ZGEN_AUTOLOAD_COMPINIT=1
fi
if [[ -n "${ZGEN_CUSTOM_COMPDUMP}" ]]; then
ZGEN_COMPINIT_DIR_FLAG="-d ${(q)ZGEN_CUSTOM_COMPDUMP}"
ZGEN_COMPINIT_FLAGS="${ZGEN_COMPINIT_DIR_FLAG} ${ZGEN_COMPINIT_FLAGS}"
fi
if [[ -z "${ZGEN_LOADED}" ]]; then
ZGEN_LOADED=()
fi
if [[ -z "${ZGEN_PREZTO_OPTIONS}" ]]; then
ZGEN_PREZTO_OPTIONS=()
fi
if [[ -z "${ZGEN_PREZTO_LOAD}" ]]; then
ZGEN_PREZTO_LOAD=()
fi
if [[ -z "${ZGEN_COMPLETIONS}" ]]; then
ZGEN_COMPLETIONS=()
fi
if [[ -z "${ZGEN_USE_PREZTO}" ]]; then
ZGEN_USE_PREZTO=0
fi
if [[ -z "${ZGEN_PREZTO_LOAD_DEFAULT}" ]]; then
ZGEN_PREZTO_LOAD_DEFAULT=1
fi
if [[ -z "${ZGEN_OH_MY_ZSH_REPO}" ]]; then
ZGEN_OH_MY_ZSH_REPO=robbyrussell
fi
if [[ "${ZGEN_OH_MY_ZSH_REPO}" != */* ]]; then
ZGEN_OH_MY_ZSH_REPO="${ZGEN_OH_MY_ZSH_REPO}/oh-my-zsh"
fi
if [[ -z "${ZGEN_OH_MY_ZSH_BRANCH}" ]]; then
ZGEN_OH_MY_ZSH_BRANCH=master
fi
if [[ -z "${ZGEN_PREZTO_REPO}" ]]; then
ZGEN_PREZTO_REPO=sorin-ionescu
fi
if [[ "${ZGEN_PREZTO_REPO}" != */* ]]; then
ZGEN_PREZTO_REPO="${ZGEN_PREZTO_REPO}/prezto"
fi
if [[ -z "${ZGEN_PREZTO_BRANCH}" ]]; then
ZGEN_PREZTO_BRANCH=master
fi
-zgen-encode-url () {
# Remove characters from a url that don't work well in a filename.
# Inspired by -anti-get-clone-dir() method from antigen.
local url="${1}"
url="${url//\//-SLASH-}"
url="${url//\:/-COLON-}"
url="${url//\|/-PIPE-}"
url="${url//~/-TILDE-}"
-zgputs "$url"
}
-zgen-get-clone-dir() {
local repo="${1}"
local branch="${2:-master}"
if [[ -e "${repo}/.git" ]]; then
-zgputs "${ZGEN_DIR}/local/${repo:t}-${branch}"
else
# Repo directory will be location/reponame
local reponame="${repo:t}"
# Need to encode incase it is a full url with characters that don't
# work well in a filename.
local location="$(-zgen-encode-url ${repo:h})"
repo="${location}/${reponame}"
-zgputs "${ZGEN_DIR}/${repo}-${branch}"
fi
}
-zgen-get-clone-url() {
local repo="${1}"
if [[ -e "${repo}/.git" ]]; then
-zgputs "${repo}"
else
# Sourced from antigen url resolution logic.
# https://github.com/zsh-users/antigen/blob/master/antigen.zsh
# Expand short github url syntax: `username/reponame`.
if [[ $repo != git://* &&
$repo != https://* &&
$repo != http://* &&
$repo != ssh://* &&
$repo != git@*:*/*
]]; then
repo="https://github.com/${repo%.git}.git"
fi
-zgputs "${repo}"
fi
}
zgen-clone() {
local repo="${1}"
local branch="${2:-master}"
local url="$(-zgen-get-clone-url ${repo})"
local dir="$(-zgen-get-clone-dir ${repo} ${branch})"
if [[ ! -d "${dir}" ]]; then
mkdir -p "${dir}"
git clone --depth=1 --recursive -b "${branch}" "${url}" "${dir}"
fi
}
-zgen-add-to-fpath() {
local completion_path="${1}"
# Add the directory to ZGEN_COMPLETIONS array if not present
if [[ ! "${ZGEN_COMPLETIONS[@]}" =~ ${completion_path} ]]; then
ZGEN_COMPLETIONS+=("${completion_path}")
fi
}
-zgen-source() {
local file="${1}"
if [[ ! "${ZGEN_LOADED[@]}" =~ "${file}" ]]; then
ZGEN_LOADED+=("${file}")
source "${file}"
completion_path="${file:h}"
-zgen-add-to-fpath "${completion_path}"
fi
}
-zgen-prezto-option(){
local module=${1}
shift
local option=${1}
shift
local params
params=${@}
if [[ ${module} =~ "^:" ]]; then
module=${module[1,]}
fi
if [[ ! $module =~ "^(\*|module|prezto:module):" ]]; then
module="module:$module"
fi
if [[ ! $module =~ "^(prezto):" ]]; then
module="prezto:$module"
fi
local cmd="zstyle ':${module}' $option ${params}"
# execute in place
eval $cmd
if [[ ! "${ZGEN_PREZTO_OPTIONS[@]}" =~ "${cmd}" ]]; then
ZGEN_PREZTO_OPTIONS+=("${cmd}")
fi
}
-zgen-prezto-load(){
local params="$*"
local cmd="pmodload ${params[@]}"
# execute in place
eval $cmd
if [[ ! "${ZGEN_PREZTO[@]}" =~ "${cmd}" ]]; then
ZGEN_PREZTO_LOAD+=("${params[@]}")
fi
}
zgen-init() {
if [[ -f "${ZGEN_INIT}" ]]; then
source "${ZGEN_INIT}"
fi
}
zgen-reset() {
-zgpute 'Deleting `'"${ZGEN_INIT}"'` ...'
if [[ -f "${ZGEN_INIT}" ]]; then
rm "${ZGEN_INIT}"
fi
if [[ -f "${ZGEN_CUSTOM_COMPDUMP}" ]] || [[ -d "${ZGEN_CUSTOM_COMPDUMP}" ]]; then
-zgpute 'Deleting `'"${ZGEN_CUSTOM_COMPDUMP}"'` ...'
rm -r "${ZGEN_CUSTOM_COMPDUMP}"
fi
}
zgen-update() {
setopt localoptions extended_glob
for repo in "${ZGEN_DIR}"/(^.git)/*; do
-zgpute "Updating '${repo}' ..."
(cd "${repo}" \
&& git pull \
&& git submodule update --init --recursive)
done
zgen-reset
}
zgen-save() {
-zgpute 'Creating `'"${ZGEN_INIT}"'` ...'
-zgputs "# {{{" >! "${ZGEN_INIT}"
-zginit "# Generated by zgen."
-zginit "# This file will be overwritten the next time you run zgen save!"
-zginit ""
-zginit "ZSH=$(-zgen-get-zsh)"
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
-zginit ""
-zginit "# ### Prezto initialization"
for option in "${ZGEN_PREZTO_OPTIONS[@]}"; do
-zginit "${option}"
done
fi
-zginit ""
-zginit "# ### General modules"
for file in "${ZGEN_LOADED[@]}"; do
-zginit 'source "'"${(q)file}"\"
done
# Set up fpath, load completions
# NOTE: This *intentionally* doesn't use ${ZGEN_COMPINIT_FLAGS}; the only
# available flags are meaningless in the presence of `-C`.
-zginit ""
-zginit "# ### Plugins & Completions"
-zginit 'fpath=('"${(@q)ZGEN_COMPLETIONS}"' ${fpath})'
if [[ ${ZGEN_AUTOLOAD_COMPINIT} == 1 ]]; then
-zginit ""
-zginit 'autoload -Uz compinit && \'
-zginit ' compinit -C '"${ZGEN_COMPINIT_DIR_FLAG}"
fi
# Check for file changes
if [[ ! -z "${ZGEN_RESET_ON_CHANGE}" ]]; then
-zginit ""
-zginit "# ### Recompilation triggers"
local ages="$(stat -Lc "%Y" 2>/dev/null $ZGEN_RESET_ON_CHANGE || \
stat -Lf "%m" 2>/dev/null $ZGEN_RESET_ON_CHANGE)"
local shas="$(cksum ${ZGEN_RESET_ON_CHANGE})"
-zginit "read -rd '' ages <<AGES; read -rd '' shas <<SHAS"
-zginit "$ages"
-zginit "AGES"
-zginit "$shas"
-zginit "SHAS"
-zginit 'if [[ -n "$ZGEN_RESET_ON_CHANGE" \'
-zginit ' && "$(stat -Lc "%Y" 2>/dev/null $ZGEN_RESET_ON_CHANGE || \'
-zginit ' stat -Lf "%m" $ZGEN_RESET_ON_CHANGE)" != "$ages" \'
-zginit ' && "$(cksum $ZGEN_RESET_ON_CHANGE)" != "$shas" ]]; then'
-zginit ' printf %s\\n '\''-- zgen: Files in $ZGEN_RESET_ON_CHANGE changed; resetting `init.zsh`...'\'
-zginit ' zgen reset'
-zginit 'fi'
fi
# load prezto modules
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
-zginit ""
-zginit "# ### Prezto modules"
printf %s "pmodload" >> "${ZGEN_INIT}"
for module in "${ZGEN_PREZTO_LOAD[@]}"; do
printf %s " ${module}" >> "${ZGEN_INIT}"
done
fi
-zginit ""
-zginit "# }}}"
zgen-apply
}
zgen-apply() {
fpath=(${(q)ZGEN_COMPLETIONS[@]} ${fpath})
if [[ ${ZGEN_AUTOLOAD_COMPINIT} == 1 ]]; then
-zgpute "Initializing completions ..."
autoload -Uz compinit && \
compinit $ZGEN_COMPINIT_FLAGS
fi
}
-zgen-path-contains() {
setopt localoptions nonomatch nocshnullglob nonullglob;
[ -e "$1"/*"$2"(.,@[1]) ]
}
-zgen-get-zsh(){
if [[ ${ZGEN_USE_PREZTO} == 1 ]]; then
-zgputs "$(-zgen-get-clone-dir "$ZGEN_PREZTO_REPO" "$ZGEN_PREZTO_BRANCH")"
else
-zgputs "$(-zgen-get-clone-dir "$ZGEN_OH_MY_ZSH_REPO" "$ZGEN_OH_MY_ZSH_BRANCH")"
fi
}
zgen-load() {
if [[ "$#" == 0 ]]; then
-zgpute '`load` requires at least one parameter:'
-zgpute '`zgen load <repo> [location] [branch]`'
elif [[ "$#" == 1 && ("${1[1]}" == '/' || "${1[1]}" == '.' ) ]]; then
local location="${1}"
else
local repo="${1}"
local file="${2}"
local branch="${3:-master}"
local dir="$(-zgen-get-clone-dir ${repo} ${branch})"
local location="${dir}/${file}"
location=${location%/}
# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi
fi
# source the file
if [[ -f "${location}" ]]; then
-zgen-source "${location}"
# Prezto modules have init.zsh files
elif [[ -f "${location}/init.zsh" ]]; then
-zgen-source "${location}/init.zsh"
elif [[ -f "${location}.zsh-theme" ]]; then
-zgen-source "${location}.zsh-theme"
elif [[ -f "${location}.theme.zsh" ]]; then
-zgen-source "${location}.theme.zsh"
elif [[ -f "${location}.zshplugin" ]]; then
-zgen-source "${location}.zshplugin"
elif [[ -f "${location}.zsh.plugin" ]]; then
-zgen-source "${location}.zsh.plugin"
# Classic oh-my-zsh plugins have foo.plugin.zsh
elif -zgen-path-contains "${location}" ".plugin.zsh" ; then
for script (${location}/*\.plugin\.zsh(N)) -zgen-source "${script}"
elif -zgen-path-contains "${location}" ".zsh" ; then
for script (${location}/*\.zsh(N)) -zgen-source "${script}"
elif -zgen-path-contains "${location}" ".sh" ; then
for script (${location}/*\.sh(N)) -zgen-source "${script}"
# Completions
elif [[ -d "${location}" ]]; then
-zgen-add-to-fpath "${location}"
else
if [[ -d ${dir:-$location} ]]; then
-zgpute "Failed to load ${dir:-$location} -- ${file}"
else
-zgpute "Failed to load ${dir:-$location}"
fi
fi
}
zgen-loadall() {
# shameless copy from antigen
# Bulk add many bundles at one go. Empty lines and lines starting with a `#`
# are ignored. Everything else is given to `zgen-load` as is, no
# quoting rules applied.
local line
grep '^[[:space:]]*[^[:space:]#]' | while read line; do
# Using `eval` so that we can use the shell-style quoting in each line
# piped to `antigen-bundles`.
eval "zgen-load $line"
done
}
zgen-saved() {
[[ -f "${ZGEN_INIT}" ]] && return 0 || return 1
}
zgen-list() {
if [[ -f "${ZGEN_INIT}" ]]; then
cat "${ZGEN_INIT}"
else
-zgpute '`init.zsh` missing, please use `zgen save` and then restart your shell.'
return 1
fi
}
zgen-selfupdate() {
if [[ -e "${ZGEN_SOURCE}/.git" ]]; then
(cd "${ZGEN_SOURCE}" \
&& git pull) \
&& zgen reset
else
-zgpute "Not running from a git repository; cannot automatically update."
return 1
fi
}
zgen-oh-my-zsh() {
local repo="$ZGEN_OH_MY_ZSH_REPO"
local file="${1:-oh-my-zsh.sh}"
zgen-load "${repo}" "${file}"
}
zgen-prezto() {
local repo="$ZGEN_PREZTO_REPO"
local file="${1:-init.zsh}"
# load prezto itself
if [[ $# == 0 ]]; then
ZGEN_USE_PREZTO=1
zgen-load "${repo}" "${file}"
if [[ ${ZGEN_PREZTO_LOAD_DEFAULT} != 0 ]]; then
-zgen-prezto-load "'environment' 'terminal' 'editor' 'history' 'directory' 'spectrum' 'utility' 'completion' 'prompt'"
fi
# this is a prezto module
elif [[ $# == 1 ]]; then
local module=${file}
if [[ -z ${file} ]]; then
-zgpute 'Please specify which module to load using `zgen prezto <name of module>`'
return 1
fi
-zgen-prezto-load "'$module'"
# this is a prezto option
else
shift
-zgen-prezto-option ${file} ${(qq)@}
fi
}
zgen-pmodule() {
local repo="${1}"
local branch="${2:-master}"
local dir="$(-zgen-get-clone-dir ${repo} ${branch})"
# clone repo if not present
if [[ ! -d "${dir}" ]]; then
zgen-clone "${repo}" "${branch}"
fi
local module="${repo:t}"
-zgen-prezto-load "'${module}'"
}
zgen() {
local cmd="${1}"
if [[ -z "${cmd}" ]]; then
-zgputs 'usage: `zgen [command | instruction] [options]`'
-zgputs " commands: list, saved, reset, clone, update, selfupdate"
-zgputs " instructions: load, oh-my-zsh, pmodule, prezto, save, apply"
return 1
fi
shift
if functions "zgen-${cmd}" > /dev/null ; then
"zgen-${cmd}" "${@}"
else
-zgpute 'Command not found: `'"${cmd}"\`
return 1
fi
}
ZSH=$(-zgen-get-zsh)
fpath=($ZGEN_SOURCE $fpath)
zgen-init