This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathomz.plugin.zsh
executable file
·259 lines (210 loc) · 6.13 KB
/
omz.plugin.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
#!/usr/bin/env zsh
# a zsh shell script to manage plugins for oh-my-zsh
# some initial variables
local DEFAULT_PLUGIN_URL="${DEFAULT_PLUGIN_URL:-"https://github.com"}"
local PLUGIN_BASE_DIR="${ZSH_CUSTOM:-"$HOME/.oh-my-zsh/custom"}/plugins/"
# help command
function usage () {
cat << 'eof'
Usage: omz [-h/--help] <command> [<plugin>]
Commands:
help: show this help message
add: add a plugin (git clone)
update: update a plugin (update all if no plugins are specified)
remove: remove a plugin
Options:
-h, --help show this help message
eof
}
# function to check if git is installed
function has_git () {
command -v git >/dev/null
}
# error logging function
function log_err () {
echo "error: $*"
}
# cd to directory, run a command, and cd back
function run_in_dir () {
# make sure at least two arguments were passed
if test -z "$1"; then
log_err "you need to specify a directory"
return 1
elif test -z "$2"; then
log_err "you need to specify a command"
return 1
fi
# make sure the destination directory and command exist
if ! test -d "$1"; then
log_err "that isn't a directory"
return 1
fi
if ! command -v "$2" >/dev/null; then
log_err "that isn't a valid command"
return 1
fi
# save the current directory to cd back to
local CURRENT_DIR="$(pwd)"
# cd into the destination
cd "$1"
# run the command
"$@[2,-1]"
# save the return status
local RETURN_CODE="$?"
# change back to the original directory
cd "$CURRENT_DIR"
return "$RETURN_CODE"
}
# create github url
function create_gh_url () {
echo "$DEFAULT_PLUGIN_URL/$1.git"
}
# get plugin name
function get_plugin_name () {
echo "$(sed 's/.*\///' <<< "$1")"
}
# check if a git repo exists
function repo_exists () {
# if there wasn't a param passed return 1
if test -z "$1"; then
return 1
fi
# save the url
local REPO_URL="$(create_gh_url "$1")"
# save the status
local STATUS_CODE="$(curl -s -o /dev/null -w "%{http_code}" -L "$1")"
# if the status is not 200, return 1
if test "$STATUS_CODE" -eq 200; then
return 0
else
return 1
fi
}
# clone a plugin
function clone_plugin () {
# gather some info on the plugin
local REPO_URL="$(create_gh_url "$1")"
local PLUGIN_NAME="$(get_plugin_name "$1")"
local PLUGIN_DIR="$PLUGIN_BASE_DIR/$PLUGIN_NAME"
# if the directory exists, do nothing
if test -d "$PLUGIN_DIR"; then
log_err "you already have that plugin"
return 1
fi
# make sure the repo exists
if ! repo_exists; then;
log_err "that repo doesn't exist"
return 1
fi
echo "Cloning $PLUGIN_NAME ($REPO_URL)..."
# clone the plugin
run_in_dir "$PLUGIN_BASE_DIR" git clone "$REPO_URL"
echo "$PLUGIN_NAME cloned!"
return "$?"
}
# update a plugin
function update_plugin () {
# gather some info on the plugin
local PLUGIN_NAME="$(get_plugin_name "$1")"
local PLUGIN_DIR="$PLUGIN_BASE_DIR/$PLUGIN_NAME"
# if the directory doesn't exist, throw an error
if ! test -d "$PLUGIN_DIR"; then
log_err "you can't update something you don't have"
return 1
fi
echo "Updating $PLUGIN_NAME ($PLUGIN_DIR)..."
# pull the plugin
run_in_dir "$PLUGIN_DIR" git pull
echo "Updated $PLUGIN_NAME!"
return "$?"
}
# update all plugins
function update_all () {
# for each folder in the plugin directory, if it's a git repo pull it
for PLUGIN_DIR in "$PLUGIN_BASE_DIR/"*/; do
# check for git folder
if test -d "$PLUGIN_DIR/.git"; then
echo "Updating $PLUGIN_DIR..."
# pull the plugin
run_in_dir "$PLUGIN_DIR" "git pull"
echo "Updated $PLUGIN_DIR!"
fi
done
}
# remove a plugin
function remove_plugin () {
# gather some info on the plugin
local PLUGIN_NAME="$(get_plugin_name "$1")"
local PLUGIN_DIR="$PLUGIN_BASE_DIR/$PLUGIN_NAME"
echo "Removing $PLUGIN_NAME ($PLUGIN_DIR)..."
# if the directory exists, remove it
if test -d "$PLUGIN_DIR"; then
rm -rf "$PLUGIN_DIR"
echo "Removed $PLUGIN_NAME!"
return "$?"
else
log_err "you can't remove something you don't have"
return 1
fi
}
# load a plugin
function load_plugin () {
# gather some info on the plugin
local PLUGIN_NAME="$(get_plugin_name "$1")"
local PLUGIN_DIR="$PLUGIN_BASE_DIR/$PLUGIN_NAME"
# if the directory doesn't exist, throw an error
if ! test -d "$PLUGIN_DIR"; then
log_err "you can't load something you don't have"
return 1
fi
if test -f $PLUGIN_BASE_DIR/$PLUGIN_NAME/$PLUGIN_NAME.plugin.zsh -o -f $PLUGIN_BASE_DIR/$PLUGIN_NAME/_$PLUGIN_NAME; then
fpath=($PLUGIN_BASE_DIR/$PLUGIN_NAME $fpath)
return "$?"
elif test -f $ZSH/plugins/$PLUGIN_NAME; then
fpath=($ZSH/plugins/$PLUGIN_NAME $fpath)
return "$?"
else
log_err "you can't load something you don't have"
return 1
fi
}
# main omz function
function omz () {
# make sure the action arg was passed
if test -z "$1"; then
log_err "you need to specify a command"
return 1
fi
# make sure a second arg was passed unless updating all
if test -z "$2"; then
if test "$1" = "update"; then
update_all
return "$?"
elif test "help" = "$1" -o "$1" = "-h" -o "$1" = "--help"; then
usage
return 0
else
log_err "you need to specify a plugin"
return 1
fi
fi
# run the proper function
case $1 in
add)
clone_plugin "$2"
;;
update)
update_plugin "$2"
;;
remove)
remove_plugin "$2"
;;
load)
load_plugin "$2"
;;
*)
log_err "that wasn't a real command"
;;
esac
}
# unfunction has_git log_err run_in_dir create_gh_url get_plugin_name clone_plugin update_plugin update_all remove_plugin load_plugin