forked from tarjoilija/zgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zgen.zsh
171 lines (134 loc) · 3.58 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
autoload -U compinit
compinit
local ZGEN_SOURCE=$(dirname $0)
if [[ -z "${ZGEN_DIR}" ]]; then
ZGEN_DIR="${HOME}/.zgen"
fi
if [[ -z "${ZGEN_INIT}" ]]; then
ZGEN_INIT="${ZGEN_DIR}/init.zsh"
fi
if [[ -z "${ZGEN_LOADED}" ]]; then
ZGEN_LOADED=()
fi
-zgen-get-clone-dir() {
local repo=${1}
if [ -d "${repo}/.git" ]; then
echo "${ZGEN_DIR}/local/$(basename ${repo})"
else
echo "${ZGEN_DIR}/${repo}"
fi
}
-zgen-get-clone-url() {
local repo=${1}
if [ -d "${repo}/.git" ]; then
echo ${repo}
else
echo "https://github.com/${repo}.git"
fi
}
-zgen-clone() {
local repo=${1}
local dir=${2}
local url=$(-zgen-get-clone-url "${repo}")
mkdir -p "${dir}"
git clone "${url}" "${dir}"
echo
}
-zgen-source() {
local file="${1}"
source "${file}"
# add to the array if not loaded already
if [[ ! "${ZGEN_LOADED[@]}" =~ ${file} ]]; then
ZGEN_LOADED+="${file}"
fi
}
zgen-update() {
find "${ZGEN_DIR}" -maxdepth 2 -mindepth 2 -type d -exec \
git --git-dir={}/.git --work-tree={} pull origin master \;
if [[ -f "${ZGEN_INIT}" ]]; then
rm "${ZGEN_INIT}"
fi
}
zgen-save() {
if [[ -f "${ZGEN_INIT}" ]]; then
rm "${ZGEN_INIT}"
fi
for file in "${ZGEN_LOADED[@]}"; do
echo "-zgen-source \"$file\"" >> "${ZGEN_INIT}"
done
}
zgen-load() {
local repo=${1}
local file=${2}
local dir=$(-zgen-get-clone-dir "${repo}")
local location=${dir}/${file}
# clone repo if not present
if [[ ! -d "${dir}" ]]; then
-zgen-clone "${repo}" "${dir}"
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}.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 ls "${location}" | grep -l "\.plugin\.zsh" &> /dev/null; then
for script (${location}/*\.plugin\.zsh(N)) -zgen-source "${script}"
elif ls "${location}" | grep -l "\.zsh" &> /dev/null; then
for script (${location}/*\.zsh(N)) -zgen-source "${script}"
elif ls "${location}" | grep -l "\.sh" &> /dev/null; then
for script (${location}/*\.sh(N)) -zgen-source "${script}"
else
echo "zgen: failed to load ${dir}"
fi
}
zgen-saved() {
[[ -f "${ZGEN_INIT}" ]] && return 0 || return 1
}
zgen-selfupdate() {
if [ -d ${ZGEN_SOURCE}/.git ]; then
pushd ${ZGEN_SOURCE}
git pull
popd
else
echo "zgen is not running from a git repository, so it is not possible to selfupdate"
fi
}
zgen-oh-my-zsh() {
local repo="robbyrussell/oh-my-zsh"
local file="${1:-oh-my-zsh.sh}"
zgen-load "${repo}" "${file}"
}
zgen() {
local cmd="${1}"
if [[ -z "${cmd}" ]]; then
echo "usage: zgen [load|oh-my-zsh|save|selfupdate|update]"
return 1
fi
shift
if functions "zgen-${cmd}" > /dev/null ; then
"zgen-${cmd}" "${@}"
else
echo "zgen: command not found: ${cmd}"
fi
}
_zgen() {
compadd \
load \
oh-my-zsh \
save \
selfupdate \
update
}
ZSH=$(-zgen-get-clone-dir "robbyrussell/oh-my-zsh")
if [[ -f "${ZGEN_INIT}" ]]; then
source "${ZGEN_INIT}"
fi
compdef _zgen zgen