-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.sh
executable file
·195 lines (162 loc) · 4.3 KB
/
package.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
#!/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)"
BPKG_JSON="$(which bpkg-json)"
if [ -z "$BPKG_JSON" ]; then
BPKG_JSON="$(bpkg_realpath "$0/../JSON/JSON.sh")"
else
BPKG_JSON="$(bpkg_realpath "$BPKG_JSON")"
fi
## output usage
usage () {
echo "usage: bpkg-package [-h|--help]"
echo " or: bpkg-package [-p|--path]"
echo " or: bpkg-package <prop>"
echo " or: bpkg-package"
}
expand_grep_args () {
local strict_ending=$(echo "$@" | grep '\-\-strict')
if [ -n "$strict_ending" ]; then
shift
fi
local n=$#
if (( n > 0 )); then
printf '\['
fi
while (( $# > 0 )); do
if [[ "$1" =~ ^[0-9]+$ ]]; then
printf '%s' "$1"
else
printf '"%s"' "$1"
fi
shift
if (( $# > 0)); then
printf ','
fi
done
if (( n > 0 )); then
if [ -n "$strict_ending" ]; then
printf '\]'
else
printf '\]?'
fi
fi
return 0
}
find_file () {
local path="$(pwd)"
local file="$1"
## check if file exists at given path
if test -f "$file"; then
realpath "$file"
return 0
fi
## check if file exists joined with currrent path (cwd)
if test -f "$path/$file"; then
realpath "$path/$file"
return 0
fi
## check if file exists in paths stopping at $HOME and '/'
while [[ "$path" != "$HOME" && "$path" != "/" && "$path" != "" ]]; do
if test -f "$path/$file"; then
realpath "$path/$file"
return 0
fi
path="$(dirname "$path")"
done
return 1
}
bpkg_package_path () {
local cwd="$(pwd)"
## search up for 'bpkg.json', but only in CWD for 'package.json'
local pkgs=("bpkg.json" "$cwd/package.json")
for (( i = 0; i < ${#pkgs[@]}; i++ )); do
if find_file "${pkgs[$i]}"; then
return 0
fi
done
return 1
}
## Read a package property
bpkg_package () {
local cwd="$(pwd)"
## search up for 'bpkg.json', but only in CWD for 'package.json'
local pkgs=("bpkg.json" "$cwd/package.json")
local npkgs="${#pkgs[@]}"
local rc=1
## parse flags
case "$1" in
-h|--help)
usage
return 0
;;
-p|--path)
bpkg_package_path
return $?
;;
esac
## attempt to find JSON manifest and query it
for (( i = 0; i < npkgs; i++ )); do
local pkg="$(find_file "${pkgs[$i]}")"
if test -f "$pkg"; then
if [ -z "$1" ]; then
## output all propertyies if property
## is ommited
cat < "$pkg" | "$BPKG_JSON" -b || return $?
rc=0
else
## show value for a specific property
## in 'bpkg.json' or 'package.json'
declare -a results
declare -a peek
# shellcheck disable=SC2068
IFS=$'\n' read -r -d '' -a peek <<< "$(cat < "$pkg" | "$BPKG_JSON" -b | grep -E "$(expand_grep_args --strict "$@")")"
# shellcheck disable=SC2068
IFS=$'\n' read -r -d '' -a results <<< "$(cat < "$pkg" | "$BPKG_JSON" -b | grep -E "$(expand_grep_args $@)")"
if (( ${#results[@]} == 1 )); then
rc=0
if (( ${#peek[@]} > 0 )); then
{
echo "${results[@]}" |
awk '{ $1=""; print $0 }' | ## print value
sed 's/^\s*//g' | ## remove leading whitespace
sed 's/\s*$//g' | ## remove trailing whitespace
sed 's/^"//g' | ## remove leading quote from JSON value
sed 's/"$//g' | ## remove trailing quote from JSON value
sed 's/^ *//;s/ *$//'
} || return $?
else
echo -e "${results[@]}"
fi
elif (( ${#results[@]} > 0 )); then
rc=0
for (( j = 0; j < ${#results[@]}; j++ )); do
echo -e "${results[j]}"
done
fi
shift
fi
return $rc
fi
done
echo 1>&2 "error: Unable to find \`bpkg.json' or \`package.json' in $cwd"
return 1
}
if [ -z "$BPKG_JSON" ]; then
echo 1>&2 "error: Failed to load 'bpkg-json'"
else
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_package
export -f bpkg_package_path
else
bpkg_package "${@}"
exit $?
fi
fi