-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·267 lines (215 loc) · 5.73 KB
/
run.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
#!/usr/bin/env bash
if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting"
exit 1
fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
# shellcheck source=lib/realpath/realpath.sh
bpkg_exec_or_exit bpkg-realpath &&
source "$(which bpkg-realpath)"
# shellcheck source=lib/install/install.sh
bpkg_exec_or_exit bpkg-install &&
source "$(which bpkg-install)"
# shellcheck source=lib/package/package.sh
bpkg_exec_or_exit bpkg-package &&
source "$(which bpkg-package)"
bpkg_initrc
## output usage
usage () {
echo 'usage: bpkg-run [-h|--help]'
echo ' or: bpkg-run [-l|--list]'
echo ' or: bpkg-run [-s|--source] <package> [command]'
echo ' or: bpkg-run [-s|--source] <user>/<package> [command]'
}
bpkg_list_commands () {
local commands
local col_len
local description
commands="$(bpkg_package 2>/dev/null | grep '\["commands"' | sed 's/\["commands","\([^"]*\).*/\1/')"
col_len="$(wc -L <<< "${commands}")"
if [ "${col_len}" -eq 0 ]; then
bpkg_error "No commands provided in BPKG package file."
return 1
fi
for command in ${commands}; do
description="$(bpkg_package commands-description "${command}")"
if [ -z "${description}" ]; then
description="Runs the ${command} command as defined in BPKG configuration"
fi
printf " "
bpkg_exec_exist bpkg-term &&
bpkg-term color cyan
printf "%-${col_len}s " "${command}"
bpkg_exec_exist bpkg-term && {
bpkg-term reset
bpkg-term bright
}
printf "%s\n" "${description}"
bpkg_exec_exist bpkg-term &&
bpkg-term reset
done
return 0
}
bpkg_runner () {
local cmd="$1"
shift
eval "$cmd"
return $?
}
bpkg_run () {
local should_emit_source=0
local should_source=0
local should_clean=0
local needs_name=0
local dest=''
local name=''
for opt in "$@"; do
case "$opt" in
-h|--help)
usage
return 0
;;
-l|--list)
bpkg_list_commands
return $?
;;
-s|--source)
should_source=1
shift
;;
--emit-source)
should_emit_source=1
shift
;;
-c|--clean)
should_clean=1
shift
;;
-t|--target|--name)
needs_name=1
shift
;;
*)
if (( 1 == needs_name )); then
name="$opt"
shift
fi
break
;;
esac
done
if [ -n "$1" ]; then
local cmd="$(bpkg_package commands "$1" 2>/dev/null)"
if [ -n "$cmd" ]; then
local root="$(dirname "$(bpkg_package --path)")"
cd "$root" || return 1
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
local parts=()
# shellcheck disable=SC2086
read -r -a parts <<< $cmd
local args=()
local prefix="${parts[0]}"
for (( i = 1; i < ${#parts[@]}; i++ )); do
if [[ "${parts[$i]}" =~ \*.\* ]]; then
local found=($(find . -path "${parts[$i]}"))
if (( ${#found[@]} > 0 )); then
args+=("${found[@]}")
else
# shellcheck disable=SC2086
args+=($($(which ls) ${parts[$i]} 2>/dev/null))
fi
else
args+=("${parts[$i]}")
fi
done
shift
bpkg_runner "$prefix ${args[*]}" "$@"
return $?
fi
fi
if which "$name" 2>/dev/null; then
return 0
fi
if ! pushd . >/dev/null; then
bpkg_error "Failed to 'pushd' to current working directory."
return 1
fi
if (( 0 == should_clean )); then
dest=$(bpkg_install --no-prune -g "${name:-$1}" | grep 'Cloning' | sed 's/.* to //g' | xargs echo)
else
dest=$(bpkg_install -g "${name:-$1}" | grep 'Cloning' | sed 's/.* to //g' | xargs echo)
fi
if [ -z "$dest" ]; then
if (( 1 == should_emit_source )); then
bpkg_error "The source '${name:-1}' was not found locally in a 'bpkg.json' or published as a package."
else
bpkg_error "The command '$1' was not found locally in a 'bpkg.json' or published as a package."
fi
return 1
fi
if ! cd "$dest" >/dev/null; then
bpkg_error "Failed to change directory to package: '$dest'."
return 1
fi
local pkg_name="$(bpkg_package name 2>/dev/null)"
if [ -n "$pkg_name" ]; then
name="$pkg_name"
fi
if (( 1 == should_emit_source )); then
if which "$name" 2>/dev/null; then
:
elif test -f "$1"; then
bpkg_realpath "$1"
elif [ -n "$1" ] && which "$1" 2>/dev/null; then
:
fi
else
shift
if (( 1 == should_source )); then
# shellcheck disable=SC1090
source "$(which "$name")"
else
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
cmd="$(bpkg_package commands "$1" 2>/dev/null)"
if [ -n "$cmd" ]; then
local parts=()
# shellcheck disable=SC2086
read -r -a parts <<< $cmd
local args=()
local prefix="${parts[0]}"
for (( i = 1; i < ${#parts[@]}; i++ )); do
if [[ "${parts[$i]}" =~ \*.\* ]]; then
local found=($(find . -path "${parts[$i]}"))
if (( ${#found[@]} > 0 )); then
args+=("${found[@]}")
else
# shellcheck disable=SC2086
args+=($($(which ls) ${parts[$i]} 2>/dev/null))
fi
fi
done
shift
bpkg_runner "$prefix ${args[*]}" "$@"
fi
# shellcheck disable=SC2068
"$(which "$name")" $@
fi
fi
if ! popd >/dev/null; then
bpkg_error "Failed to 'popd' to previous working directory."
return 1
fi
return $?
}
## Use as lib or perform install
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_run
else
bpkg_run "$@"
exit $?
fi