-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.sh
287 lines (241 loc) · 6.5 KB
/
path.sh
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
#-------------------------------------------------------------------------------
#
# Interactive bash path manipulation library
#
# Source this file in bash or your `.bashrc` to add a `path` shell function for
# manipulating colon-delimited lookup paths in environment variables, such as
# `PATH`, `LD_LIBRARY_PATH`, and `PYTHONPATH`.
#
# Invoke `path --help` for usage information. Examples:
#
# path PATH # Print how my command path.
# path PATH - 0 # Remove the first element from it.
# path PYTHONPATH ++ $HOME/python # Prepend ~/python to my Python path.
#
# Also supports short aliases for path environment variable names. Override
# this array variable to customize:
#
# PATH_VARNAME_ALIASES=(
# [CP]=CLASSPATH
# [JS]=NODE_PATH
# [LD]=LD_LIBRARY_PATH
# [MAN]=MANPATH
# [PY]=PYTHONPATH
# [P]=PATH
# )
#
#-------------------------------------------------------------------------------
abspath() {
(
unset CDPATH;
cd "$(dirname "$1")" && echo $PWD/$(basename "$1")
)
}
_path-join() {
local IFS=":"
echo "$*"
}
# _path-join-set VARNAME ITEM ...
#
# Sets path $VARNAME to the path obtained by joining ITEMs.
#
_path-join-set() {
local varname="$1"; shift
eval "$varname"="$(_path-join "$@")"
export $varname
}
_path-show() {
local varname="$1"
local parts
IFS=":" read -r -a parts <<< "${!varname}"
echo "$varname="
local part
local i=0
for part in "${parts[@]}"; do
printf "%3d: %s\n" $i "$part"
i=$(( i + 1 ))
done
}
_path-remove() {
local varname="$1"
local item="$2"
local parts
IFS=":" read -r -a parts <<< "${!varname}"
if [[ $item =~ ^[0-9]+$ ]]; then
unset parts[$item]
else
item="$(abspath "$item")"
local i
for (( i = ${#parts[@]} - 1; i >= 0; i-- )); do
local part="${parts[$i]}"
if [[ "$part" == "$item" ]]; then
unset parts[$i]
fi
done
fi
_path-join-set "$varname" "${parts[@]}"
}
_path-in() {
local varname="$1"
local item="$2"
item="$(abspath "$item")"
local parts
IFS=":" read -r -a parts <<< "${!varname}"
local part
for part in "${parts[@]}"; do
if [[ "$part" == "$item" ]]; then
return 0
fi
done
return 1
}
_path-prepend() {
local varname="$1"
local item="$2"
item="$(abspath "$item")"
_path-remove "$varname" "$item"
local parts
IFS=":" read -r -a parts <<< "${!varname}"
local new_parts=("$item")
new_parts+=("${parts[@]}")
_path-join-set "$varname" "${new_parts[@]}"
}
_path-add() {
local varname="$1"
local item="$2"
item="$(abspath "$item")"
local parts
IFS=":" read -r -a parts <<< "${!varname}"
local found
for part in "${parts[@]}"; do
if [[ "$part" == "$item" ]]; then
found=yes
break
fi
done
if [[ ! $found ]]; then
parts[${#parts[@]}]="$item"
fi
_path-join-set "$varname" "${parts[@]}"
}
_path-move() {
local varname="$1"
local i0="$2"
local i1="${3:-0}"
local item
local i
local parts
IFS=":" read -r -a parts <<< "${!varname}"
item="${parts[$i0]}"
if [[ $i0 -lt $i1 ]]; then
# Shift items up.
i=$i0
while [[ $i -lt $i1 ]]; do
parts[$i]="${parts[$(($i + 1))]}"
i=$(($i + 1))
done
elif [[ $i0 -gt $i1 ]]; then
# Shift items down.
i=$i0
while [[ $i -gt $i1 ]]; do
parts[$i]="${parts[$(($i - 1))]}"
i=$(($i - 1))
done
fi
parts[$i1]="$item"
_path-join-set "$varname" "${parts[@]}"
}
_path-clean() {
local varname="$1"
local real
if [[ "$1" == --real ]]; then
real=yes
shift
fi
local parts
IFS=":" read -r -a parts <<< "${!varname}"
local clean=()
local part part1
for part in "${parts[@]}"; do
local found=
for part1 in "${clean[@]}"; do
if [[ "$part" == "$part1"
|| ( $real && "$part" -ef "$part1" ) ]]; then
found=yes
break
fi
done
if [[ ! $found ]]; then
clean[${#clean[@]}]="$part"
fi
done
_path-join-set "$varname" "${clean[@]}"
}
_path-clear() {
local varname="$1"
_path-join-set "$varname"
}
_path-help() {
echo 'Usage: path VARNAME [ COMMAND ... ]
path VARNAME [ show ]
Prints path components of $VARNAME.
path VARNAME ( remove | rm | - ) ITEM
If ITEM is a component of $VARNAME, removes it.
If ITEM is a number, returns the ITEMth component of $VARNAME.
path VARNAME in ITEM
Returns true if ITEM is a component of $VARNAME.
path VARNAME ( prepend | pre | ++ ) ITEM
Prepends ITEM to the path $VARNAME. If it is already a component,
replaces the existing occurrence.
path VARNAME ( add | + ) ITEM
Appends ITEM it to the path $VARNAME if it is not a component.
path VARNAME ( move | mv ) SRC [ DST ]
Moves item at position SRC of $VARNAME to position DST.
If DST is omitted, moves item at position SRC to the front of $VARNAME.
path VARNAME clear
Removes all path components.
path VARNAME clean [ --real ]
Removes the second and subsequent occurence of each component of $VARNAME.
With --real, components are compared based on actual file system identity,
so that redundant paths to the same file are removed.
'
}
declare -A PATH_VARNAME_ALIASES
PATH_VARNAME_ALIASES=(
[CP]=CLASSPATH
[JS]=NODE_PATH
[LD]=LD_LIBRARY_PATH
[MAN]=MANPATH
[PY]=PYTHONPATH
[P]=PATH
)
path() {
local varname="$1"; shift
if [[ $varname == "-h" || $varname == "--help" ]]; then
_path-help
return 0
fi
if [[ -z "$varname" ]]; then
echo "missing VARNAME" >&2
return 1
fi
local expansion="${PATH_VARNAME_ALIASES[$varname]}"
if [[ -n "$expansion" ]]; then
varname=$expansion
fi
local command="${1:-show}"; shift
case "$command" in
# Command aliases.
pre|++) command=prepend;;
rm|-) command=remove;;
+) command=add;;
mv) command=move;;
# Valid commands.
add|clean|clear|in|move|prepend|remove|show);;
# Anything else is invalid.
*)
echo "invalid command '$command'" >&2
return 1;;
esac
_path-$command "$varname" "$@"
}