-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhab.plugin.zsh
294 lines (243 loc) · 5.5 KB
/
hab.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
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
# Hab zsh plugin
#
# A Hab is a set of exported OS environment variables. This plugin loads the OS
# environment variables set in the file `.envrc` automatically every time you
# `cd` to a folder or open shell.
#
# It's possible to change the name of the default file base name setting the
# variable `$HAB_BASE` in your .zshrc
# shellcheck disable=SC1090
# shellcheck disable=SC2012
# shellcheck disable=SC2148
# shellcheck disable=SC2155
# shellcheck disable=SC2207
##################
# Global variables
export HAB_CURRENT=""
export HAB_MODIFICATION_DATE=""
[ -z "$HAB_BASE" ] && export HAB_BASE=".envrc"
###################
# Private functions
# Prints an error
function __hab_error() {
local message="$1"
(>&2 echo -e "\033[1;91m[ERROR] $message\033[0;0m")
}
# Prints a warning
function __hab_warn() {
local message="$1"
(>&2 echo -e "\033[1;93m[WARN] $message\033[0;0m")
}
# Prints a success message
function __hab_success() {
local message="$1"
(echo -e "\033[1;92m[SUCCESS] $message\033[0;0m")
}
# Gets the name of the env file
function __hab_get_file() {
local hab_type="$1"
local current_path=""
local hab_file=""
current_path="$(pwd)"
if [ -n "$hab_type" ] && [ -r "$current_path/$HAB_BASE.$hab_type" ]
then
hab_file="$current_path/$HAB_BASE.$hab_type"
elif [ -n "$hab_type" ] && [ -r "$current_path/$HAB_BASE" ]
then
__hab_error "File not found [$current_path/$HAB_BASE.$hab_type]"
__hab_warn "Using default instead [$current_path/$HAB_BASE]"
hab_file="$current_path/$HAB_BASE"
elif [ -r "$current_path/$HAB_BASE" ]
then
hab_file="$current_path/$HAB_BASE"
elif [ -n "$HAB_CURRENT" ] &&
[ -r "$HAB_CURRENT" ] &&
grep -q "# INHERIT: true" < "$HAB_CURRENT" &&
echo "$current_path" | grep -q "$(dirname "$HAB_CURRENT")"
then
hab_file="$HAB_CURRENT"
fi
echo "$hab_file"
}
# Unloads variables
function __hab_unload_variables() {
local current_hab="$1"
local unload=""
unload=$(
grep '^export .*$' < "$current_hab" |
sed -e 's/^export \([0-9a-zA-Z\_]*\)=.*$/\1/'
)
while read -r line
do
if [ -n "$line" ]
then
unset "$line"
__hab_warn "Unloaded variable $line"
fi
done <<<"$unload"
}
# Unloads functions
function __hab_unload_functions() {
local current_hab="$1"
local unload=""
unload=$(
grep '^function .*$' < "$current_hab" |
sed -e 's/^function \([0-9a-zA-Z\_]*\).*$/\1/'
)
while read -r line
do
if [ -n "$line" ]
then
unset -f "$line"
__hab_warn "Unloaded function $line"
fi
done <<<"$unload"
}
# Unloads current hab
function __hab_unload_current() {
local unload=""
if [ -n "$HAB_CURRENT" ] && [ -r "$HAB_CURRENT" ]
then
__hab_unload_variables "$HAB_CURRENT"
__hab_unload_functions "$HAB_CURRENT"
fi
export HAB_CURRENT=""
export HAB_MODIFICATION_DATE=""
}
# Loads new hab
function __hab_load_new() {
export HAB_CURRENT="$1"
export HAB_MODIFICATION_DATE="$(date -r "$HAB_CURRENT")"
source "$HAB_CURRENT"
__hab_success "Loaded hab [$HAB_CURRENT] (Last modified $HAB_MODIFICATION_DATE)"
}
# Updates hab plugin
function __hab_update() {
if [ -d "$ZSH_CUSTOM/plugins/hab/.git" ]
then
(
cd "$ZSH_CUSTOM/plugins/hab" &&
git pull origin master
)
fi
}
# Unloads current hab
function __hab_unload() {
__hab_unload_current
}
# Loads a new hab
function __hab_load() {
local hab_type="$1"
local hab_file=""
hab_file=$(__hab_get_file "$hab_type")
__hab_unload_current
if [ -n "$hab_file" ]
then
__hab_load_new "$hab_file"
fi
}
# Reloads a hab
function __hab_reload() {
local hab_type="$(basename "$HAB_CURRENT")"
hab_type="${hab_type##*.}"
if [ ".$hab_type" = "$HAB_BASE" ]
then
hab_type=""
fi
if [ -n "$HAB_CURRENT" ] &&
[ "$(date -r "$HAB_CURRENT")" != "$HAB_MODIFICATION_DATE" ]
then
__hab_load "$hab_type"
fi
}
# Hab usage
function __hab_usage() {
echo "\033[1;1mUsage:\033[0;0m
~ $ hab [load [environment] | reload | unload | update | help]
"
}
# Hab environment list
function __hab_env_list() {
local envs=""
envs=$(
ls -a .envrc.* 2> /dev/null |
sed 's/'$HAB_BASE'\.\(.*\)/\1/g' |
tr '\n' ' '
)
echo "$envs"
}
# Hab completions
function __hab() {
local current=""
local previous=""
local cmd=""
local cmds="load reload unload update help"
local envs=""
COMPREPLY=()
envs=$(__hab_env_list)
current="${COMP_WORDS[COMP_CWORD]}"
previous="${COMP_WORDS[COMP_CWORD - 1]}"
if [ "$COMP_CWORD" -eq 1 ]
then
case "$current" in
reload | unload | update | help)
;;
load)
COMPREPLY=($(compgen -W "$envs" -- "$current"))
;;
*)
COMPREPLY=($(compgen -W "$cmds $envs" -- "$current"))
;;
esac
elif [ "$COMP_CWORD" -eq 2 ]
then
case "$previous" in
load)
COMPREPLY=($(compgen -W "$envs" -- "$current"))
;;
*)
;;
esac
fi
return 0
}
##################
# Public functions
# Main hab function.
function hab() {
local cmd="$1"
local env="$2"
if [ -z "$cmd" ]
then
cmd="load"
fi
case "$cmd" in
load)
__hab_load "$env"
;;
reload)
__hab_reload
;;
unload)
__hab_unload
;;
update)
__hab_update
;;
help)
__hab_usage
;;
*)
__hab_load "$cmd"
;;
esac
}
##############
# Installation
# Loads hab on `cd`
add-zsh-hook chpwd __hab_load
add-zsh-hook precmd __hab_reload
# Completions
complete -F __hab hab
# Loads hab when starting zsh
__hab_load