-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-envrc.rc
379 lines (353 loc) · 16.3 KB
/
bash-envrc.rc
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
##
## bash-envrc.rc -- Improve GNU Bash with Environment Variable Run-Commands
## Copyright (c) 2019-2023 Dr. Ralf S. Engelschall <http://engelschall.com>
##
## Permission is hereby granted, free of charge, to any person obtaining
## a copy of this software and associated documentation files (the
## "Software"), to deal in the Software without restriction, including
## without limitation the rights to use, copy, modify, merge, publish,
## distribute, sublicense, and/or sell copies of the Software, and to
## permit persons to whom the Software is furnished to do so, subject to
## the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
## IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
## CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
## TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
## SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##
# we enhance bash(1) only for interactive sessions...
if [[ $- =~ .*i.* ]]; then
# the dedicated filenames
__envmanifest_file=".bash_envrc_manifest"
__envrc_file=".bash_envrc"
# provide the envrc(1) command
envrc () {
# sanity check usage
if [[ $# -lt 1 ]]; then
echo "-bash: envrc: ERROR: invalid number of arguments" 2>&1
echo "-bash: envrc: USAGE: envrc list" 2>&1
echo "-bash: envrc: USAGE: envrc add <dir>" 2>&1
echo "-bash: envrc: USAGE: envrc rm <dir>" 2>&1
echo "-bash: envrc: USAGE: envrc switch [-i] <new-dir> [<old-dir>]" 2>&1
return 1
fi
# dispatch commands
cmd="$1"
shift
if [[ $cmd == "list" ]]; then
##
## list all directories in whitelist
##
if [[ -f "${HOME}/${__envmanifest_file}" ]]; then
while read -r line; do
echo "envrc: whitelisted directory: \"${line}\""
done < "${HOME}/${__envmanifest_file}"
fi
elif [[ $cmd == "add" ]]; then
##
## add directory to whitelist
##
if [[ $# -ne 1 ]]; then
echo "-bash: envrc: ERROR: invalid number of arguments" 2>&1
echo "-bash: envrc: USAGE: envrc add <dir>" 2>&1
return 1
fi
local dir="$1"; shift
if [[ ! -d ${dir} ]]; then
echo "-bash: envrc: ERROR: no such directory \"${dir}\"" 1>&2
return 1
fi
dir=`builtin cd "$dir" && pwd -P`
exists=0
if [[ -f "${HOME}/${__envmanifest_file}" ]]; then
while read -r line; do
if [[ $line == $dir ]]; then
exists=1
break
fi
done < "${HOME}/${__envmanifest_file}"
fi
if [[ $exists -eq 0 ]]; then
echo "$dir" >> "${HOME}/${__envmanifest_file}"
echo "envrc: added \"${dir}\" to whitelist \"${HOME}/${__envmanifest_file}\""
else
echo "envrc: \"${dir}\" already on whitelist (nothing changed)"
fi
elif [[ $cmd == "rm" ]]; then
##
## remove directory from whitelist
##
if [[ $# -ne 1 ]]; then
echo "-bash: envrc: ERROR: invalid number of arguments" 2>&1
echo "-bash: envrc: USAGE: envrc rm <dir>" 2>&1
return 1
fi
local dir="$1"; shift
if [[ ! -d ${dir} ]]; then
echo "-bash: envrc: ERROR: no such directory \"${dir}\"" 1>&2
return 1
fi
dir=`builtin cd "$dir" && pwd -P`
removed=0
if [[ -f "${HOME}/${__envmanifest_file}" ]]; then
while read -r line; do
if [[ $line == $dir ]]; then
removed=1
continue
fi
echo "$line"
done < "${HOME}/${__envmanifest_file}" > "${HOME}/${__envmanifest_file}.new"
if [[ $removed -eq 1 ]]; then
echo "envrc: removed \"${dir}\" from whitelist \"${HOME}/${__envmanifest_file}\""
cp "${HOME}/${__envmanifest_file}.new" "${HOME}/${__envmanifest_file}"
rm -f "${HOME}/${__envmanifest_file}.new"
else
echo "envrc: \"${dir}\" not on whitelist \"${HOME}/${__envmanifest_file}\" (ignoring removal)"
rm -f "${HOME}/${__envmanifest_file}.new"
fi
else
echo "envrc: still no such whitelist \"${HOME}/${__envmanifest_file}\" (ignoring removal)"
fi
elif [[ $cmd == "switch" ]]; then
##
## switch directory and activate/deactivate environments
##
# determine mode
local init=0
local verbose=0
while [[ $# -ge 1 ]]; do
case "$1" in
-i ) init=1; shift ;;
-v ) verbose=$(($verbose + 1)); shift ;;
* ) break ;;
esac
done
# sanity check usage
if [[ ($init -eq 0 && $# -ne 2) || ($init -eq 1 && $# -ne 1) ]]; then
echo "-bash: envrc: ERROR: invalid number of arguments" 2>&1
echo "-bash: envrc: USAGE: envrc switch [-v] [-i] <new-dir> [<old-dir>]" 2>&1
return 1
fi
# determine absolute path of new/destination directory
local dst="$1"; shift
if [[ ! -d $dst ]]; then
echo "-bash: envrc: ERROR: no such new/destination directory \"${dst}\"" 2>&1
return 1
fi
dst=`builtin cd "$dst" && pwd -P`
# determine absolute path of old/source directory
local src
if [[ $init -eq 0 ]]; then
src="$1"; shift
if [[ ! -d $dst ]]; then
echo "-bash: envrc: ERROR: no such old/source directory \"${dst}\"" 2>&1
return 1
fi
src=`builtin cd "$src" && pwd -P`
fi
# walk the source filesystem path upwards until a common
# prefix of source and destination directory is found
local dir="$src"
local walk_up=()
if [[ $init -eq 0 ]]; then
walk_up+=($dir)
while [[ ${dst#$dir} == $dst ]]; do
if [[ $dir =~ ^(.+)/[^/]+$ ]]; then
dir=${BASH_REMATCH[1]}
walk_up+=($dir)
else
dir=""
break
fi
done
if [[ $dir == "" && ${walk_up[${#walk_up[@]} - 1]} != "/" ]]; then
walk_up+=("/")
fi
fi
# walk the destination filesystem path downwards until a common
# prefix of source and destination directory is found
local dir="$dst"
local walk_dn=($dir)
while [[ ${src#$dir} == $src ]]; do
if [[ $dir =~ ^(.+)/[^/]+$ ]]; then
dir=${BASH_REMATCH[1]}
walk_dn=($dir "${walk_dn[@]}")
else
dir=""
break
fi
done
if [[ $dir == "" && ${walk_dn[0]} != "/" ]]; then
walk_dn=("/" "${walk_dn[@]}")
fi
# ensure that we do not walk up and then just down the exactly same directory
if [[ $init -eq 0 ]]; then
local i=${#walk_up[*]}
local j=${#walk_dn[*]}
if [[ $i -gt 0 && $j -gt 0 ]]; then
i=$(($i - 1))
if [[ ${walk_up[i]} == ${walk_dn[0]} ]]; then
walk_up=("${walk_up[@]:0:${#walk_up[@]}-1}")
walk_dn=("${walk_dn[@]:1}")
fi
fi
fi
# parse run-command file
processfile () {
local mode="$1"
local dir="$2"
local file="$3"
while read -r line; do
if [[ ! ($line =~ ^#.* || $line == "") ]]; then
if [[ $line =~ ^\ *([^ !]+)(!?)\ *([:^$]=)\ *(\"([^\"\"]*)\"|[^ ]+)\ *$ ]]; then
local name=${BASH_REMATCH[1]}
local exp=${BASH_REMATCH[2]}
local op=${BASH_REMATCH[3]}
local value=${BASH_REMATCH[4]}
local value2=${BASH_REMATCH[5]}
if [[ $value2 != "" ]]; then
value=$value2
fi
# always expand old value
local value_old
eval "value_old=\${${name}}"
eval "value=\${value/\\\\\${${name}}/\${value_old}}"
eval "value=\${value/\\\\\$${name}/\${value_old}}"
# always expand some standard variables
value=${value/\\${PWD}/${dir}}
value=${value/\\$PWD/${dir}}
value=${value/\\${HOME}/${HOME}}
value=${value/\\$HOME/${HOME}}
value=${value/\\${LOGNAME}/${LOGNAME}}
value=${value/\\$LOGNAME/${LOGNAME}}
value=${value/\\${TERM}/${TERM}}
value=${value/\\$TERM/${TERM}}
# dispatch into activation or deactivation
if [[ $mode == "activate" ]]; then
if [[ ${verbose} -ge 2 ]]; then
echo "envrc: ACTIVATE: environment directive: ${name}${exp} ${op} ${value}" 1>&2
fi
local cmd
if [[ $op == ":=" ]]; then
cmd="[[ -v ${name} ]] && __envrc_${name}+=(\${${name}});"
cmd="$cmd ${name}='${value}'"
elif [[ $op == "^=" ]]; then
cmd="${name}='${value}'\${${name}-''}"
elif [[ $op == "\$=" ]]; then
cmd="${name}=\${${name}-''}'${value}'"
fi
if [[ $exp == "!" ]]; then
cmd="$cmd; export ${name}"
fi
if [[ ${verbose} -ge 3 ]]; then
echo "envrc: ACTIVATE: execute: ${cmd}" 1>&2
fi
eval "${cmd}"
elif [[ $mode == "deactivate" ]]; then
if [[ ${verbose} -ge 2 ]]; then
echo "envrc: DEACTIVATE: environment directive: ${name}${exp} ${op} ${value}" 1>&2
fi
local cmd
if [[ $op == ":=" ]]; then
cmd="if [[ \${#__envrc_${name}[@]} -gt 1 ]];"
cmd="$cmd then ${name}=\${__envrc_${name}[\${#__envrc_${name}[@]}-1]};"
cmd="$cmd __envrc_${name}=(\"\${__envrc_${name}[@]:0:\${#__envrc_${name}[@]}-1}\");"
cmd="$cmd elif [[ \${#__envrc_${name}[@]} -eq 1 ]];"
cmd="$cmd then ${name}=\${__envrc_${name}[0]}; unset __envrc_${name};"
cmd="$cmd else unset ${name};"
cmd="$cmd fi"
elif [[ $op == "^=" ]]; then
cmd="${name}=\${${name}#${value}}; [[ -z \${${name}} ]] && unset ${name}"
elif [[ $op == "\$=" ]]; then
cmd="${name}=\${${name}%${value}}; [[ -z \${${name}} ]] && unset ${name}"
fi
if [[ ${verbose} -ge 3 ]]; then
echo "envrc: DEACTIVATE: execute: ${cmd}" 1>&2
fi
eval "${cmd}"
fi
elif [[ $line =~ ^\ *(->|<-)\ +(.+)$ ]]; then
local on=${BASH_REMATCH[1]}
local cmd=${BASH_REMATCH[2]}
if [[ $mode == "activate" && ${on} == "->" ]]; then
if [[ ${verbose} -ge 2 ]]; then
echo "envrc: ACTIVATE: environment command: ${cmd}" 1>&2
fi
eval "${cmd}"
elif [[ $mode == "deactivate" && ${on} == "<-" ]]; then
if [[ ${verbose} -ge 2 ]]; then
echo "envrc: DEACTIVATE: environment command: ${cmd}" 1>&2
fi
eval "${cmd}"
fi
else
echo "-bash: envrc: WARNING: invalid directive \"$line\" in \"${dir}/${file}\"" 1>&2
fi
fi
done < "${dir}/${file}"
}
# load whitelist
local whitelist=()
if [[ -f "${HOME}/${__envmanifest_file}" ]]; then
whitelist=($(< "${HOME}/${__envmanifest_file}" ))
fi
whitelisted () {
local dir="$1"
for entry in "${whitelist[@]}"; do
if [[ ${entry} == ${dir} ]]; then
return 0
fi
done
return 1
}
# walk filesystem up and deactivate all entries of run-command files
for dir in "${walk_up[@]}"; do
if [[ -f "${dir}/${__envrc_file}" ]]; then
if whitelisted "${dir}"; then
if [[ ${verbose} -ge 1 ]]; then
echo "envrc: DEACTIVATE: environment configuration \"${dir}/${__envrc_file}\""
fi
processfile deactivate "${dir}" "${__envrc_file}"
fi
fi
done
# walk filesystem down and activate all entries of run-command files
for dir in "${walk_dn[@]}"; do
if [[ -f "${dir}/${__envrc_file}" ]]; then
if whitelisted "${dir}"; then
if [[ ${verbose} -ge 1 ]]; then
echo "envrc: ACTIVATE: environment configuration \"${dir}/${__envrc_file}\""
fi
processfile activate "${dir}" "${__envrc_file}"
fi
fi
done
else
echo "-bash: envrc: ERROR: invalid command \"${cmd}\"" 1>&2
return 1
fi
}
# optionally automatically integrate into FZF-enhanced cd(1) command
if [[ -v __fzf_version ]]; then
__envrc_cmd="envrc switch \${ENVRC_SWITCH_OPTS} \"\${PWD}\" \"\${OLDPWD}\""
__envrc_hooked=0
for __envrc_hook in "${__fzf_cd_hooks[@]}"; do
if [[ ${__envrc_hook} == ${__envrc_cmd} ]]; then
__envrc_hooked=1
break
fi
done
if [[ ${__envrc_hooked} -eq 0 ]]; then
__fzf_cd_hooks+=("${__envrc_cmd}")
fi
unset __envrc_cmd
unset __envrc_hooked
unset __envrc_hook
fi
fi