refactor(): support 'bpkg.json'

pull/143/head
jwerle 2 years ago committed by Joseph Werle
parent 7e6b03e8ea
commit de503e955e

@ -0,0 +1,4 @@
## Allow unsued variables
disable=SC2034
## Allow declare and assign separately to avoid masking return values
disable=SC2155

@ -158,7 +158,7 @@ Here we lay down some info on the structure of a package.
## bpkg.json
Every package must have a file called `bpkg.json` (for backward-compatibility
Every package must have a file called `bpkg.json` (for backward-compatibility
`package.json` can also be used); it specifies package metadata on the [JSON format][json].
Here's an example of a well-formed `bpkg.json`:

@ -9,15 +9,8 @@ usage () {
## Read a package property
bpkg_getdeps () {
local cwd pkg
cwd="$(pwd)"
pkg="${cwd}/bpkg.json"
if ! test -f "${pkg}"; then
pkg="${cwd}/package.json"
fi
local cwd="$(pwd)"
local pkg="${cwd}/bpkg.json"
## parse flags
case "$1" in
@ -29,8 +22,11 @@ bpkg_getdeps () {
## ensure there is a package to read
if ! test -f "${pkg}"; then
echo 2>&1 "error: Unable to find 'bpkg.json' or 'package.json' in $(pwd)"
return 1
pkg="${cwd}/package.json"
if ! test -f "${pkg}"; then
echo 2>&1 "error: Unable to find \`bpkg.json' or \`package.json' in $cwd"
return 1
fi
fi
# shellcheck disable=SC2002

@ -1,7 +1,7 @@
#!/bin/bash
## sets optional variable from environment
opt () { eval "if [ -z \"\${$1}\" ]; then ${1}='${2}'; fi"; }
opt () { eval "if [ -z "\${"$1"}" ]; then $1='$2'; fi"; }
## output usage
usage () {
@ -22,8 +22,8 @@ prompt () {
value="${value//\"/\'}";
} 2>&1
if [ -n "${value}" ]; then
eval "${var}=\"${value}\""
if [ -n "$value" ]; then
eval "$var=\"$value\""
fi
}
@ -84,7 +84,7 @@ intro () {
echo "defaults."
echo
echo "See github.com/bpkg/bpkg for more information on defining the bpkg"
echo "\`bpkg.json' file."
echo "'bpkg.json' file."
echo
echo "You can press ^C anytime to quit this prompt. The 'bpkg.json' file"
echo "will only be written upon completion."
@ -138,9 +138,8 @@ csv () {
SCRIPTS="${SCRIPTS//,/ }"
SCRIPTS="${SCRIPTS//\"/}"
SCRIPTS="${SCRIPTS//\'/}"
# shellcheck disable=SC2086
SCRIPTS=($(wrap ${SCRIPTS}))
(( len=${#SCRIPTS[@]} ))
SCRIPTS=($(wrap "${SCRIPTS}"))
let len=${#SCRIPTS[@]}
for (( i = 0; i < len; i++ )); do
word=${SCRIPTS[$i]}
if (( i + 1 != len )); then
@ -275,7 +274,6 @@ create_repo () {
fi
}
## main
bpkg_init () {
local cwd
@ -341,4 +339,3 @@ else
bpkg_init "${@}"
exit $?
fi

@ -31,11 +31,11 @@ usage () {
## format and output message
message () {
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term color "${1}"
bpkg-term color "$1"
fi
shift
echo -n " ${1}"
echo -n " $1"
shift
if type -f bpkg-term > /dev/null 2>&1; then
@ -49,7 +49,7 @@ message () {
bpkg-term bright
fi
printf "%s\n" "${@}"
printf "%s\n" "$@"
if type -f bpkg-term > /dev/null 2>&1; then
bpkg-term reset
@ -58,88 +58,96 @@ message () {
## output error
error () {
{
message 'red' 'error' "${@}"
} >&2
message 'red' 'error' "$@" >&2
return 0
}
## output warning
warn () {
{
message 'yellow' 'warn' "${@}"
} >&2
message 'yellow' 'warn' "$@" >&2
return 0
}
## output info
info () {
local title='info'
if (( "${#}" > 1 )); then
title="${1}"
if (( "$#" > 1 )); then
title="$1"
shift
fi
message 'cyan' "${title}" "${@}"
message 'cyan' "$title" "$@"
return 0
}
save_remote_file () {
local auth_param dirname path url
url="${1}"
path="${2}"
url="$1"
path="$2"
auth_param="${3:-}"
dirname="$(dirname "${path}")"
dirname="$(dirname "$path")"
# Make sure directory exists
if [[ ! -d "${dirname}" ]];then
mkdir -p "${dirname}"
if [[ ! -d "$dirname" ]];then
mkdir -p "$dirname"
fi
if [[ "${auth_param}" ]];then
curl --silent -L -o "${path}" -u "${auth_param}" "${url}"
if [[ "$auth_param" ]];then
curl --silent -L -o "$path" -u "$auth_param" "$url"
else
curl --silent -L -o "${path}" "${url}"
curl --silent -L -o "$path" "$url"
fi
return $?
}
url_exists () {
local auth_param exists status url
local auth_param exists status url
url="${1}"
auth_param="${2:-}"
url="$1"
auth_param="${2:-}"
exists=0
exists=0
if [[ "${auth_param}" ]];then
status=$(curl --silent -L -w '%{http_code}' -o '/dev/null' -u "${auth_param}" "${url}")
result="$?"
else
status=$(curl --silent -L -w '%{http_code}' -o '/dev/null' "${url}")
result="$?"
fi
if [[ "$auth_param" ]];then
status=$(curl --silent -L -w '%{http_code}' -o '/dev/null' -u "$auth_param" "$url")
result="$?"
else
status=$(curl --silent -L -w '%{http_code}' -o '/dev/null' "$url")
result="$?"
fi
# In some rare cases, curl will return CURLE_WRITE_ERROR (23) when writing
# to `/dev/null`. In such a case we do not care that such an error occured.
# We are only interested in the status, which *will* be available regardless.
if [[ '0' != "${result}" && '23' != "${result}" ]] || (( status >= 400 )); then
exists=1
fi
# In some rare cases, curl will return CURLE_WRITE_ERROR (23) when writing
# to `/dev/null`. In such a case we do not care that such an error occured.
# We are only interested in the status, which *will* be available regardless.
if [[ '0' != "$result" && '23' != "$result" ]] || (( status >= 400 )); then
exists=1
fi
return "${exists}"
return "$exists"
}
read_package_json () {
fetch () {
local auth_param url
url="${1}"
url="$1"
auth_param="${2:-}"
if [[ "${auth_param}" ]];then
curl --silent -L -u "${auth_param}" "${url}"
if [[ "$auth_param" ]];then
curl --silent -L -u "$auth_param" "$url"
else
curl --silent -L "${url}"
curl --silent -L "$url"
fi
return $?
}
## Install a bash package
@ -147,16 +155,16 @@ bpkg_install () {
local pkg=''
local let needs_global=0
for opt in "${@}"; do
for opt in "$@"; do
if [[ '-' = "${opt:0:1}" ]]; then
continue
fi
pkg="${opt}"
pkg="$opt"
break
done
for opt in "${@}"; do
case "${opt}" in
for opt in "$@"; do
case "$opt" in
-h|--help)
usage
return 0
@ -169,7 +177,7 @@ bpkg_install () {
*)
if [[ '-' = "${opt:0:1}" ]]; then
echo 2>&1 "error: Unknown argument \`${1}'"
echo 2>&1 "error: Unknown argument \`$1'"
usage
return 1
fi
@ -178,7 +186,7 @@ bpkg_install () {
done
## ensure there is a package to install
if [[ -z "${pkg}" ]]; then
if [[ -z "$pkg" ]]; then
usage
return 1
fi
@ -224,22 +232,19 @@ bpkg_install_from_remote () {
local name=''
local version=''
local auth_param=''
# shellcheck disable=SC2034
local let has_pkg_json=0
local files pkg_parts remote_parts scripts
declare -a pkg_parts=()
declare -a remote_parts=()
declare -a scripts=()
declare -a files=()
local package_file=''
declare -a local pkg_parts=()
declare -a local remote_parts=()
declare -a local scripts=()
declare -a local files=()
## get version if available
{
OLDIFS="${IFS}"
OLDIFS="$IFS"
IFS="@"
# shellcheck disable=SC2206
pkg_parts=(${pkg})
IFS="${OLDIFS}"
pkg_parts=($pkg)
IFS="$OLDIFS"
}
if [[ ${#pkg_parts[@]} -eq 1 ]]; then
@ -249,21 +254,20 @@ bpkg_install_from_remote () {
name="${pkg_parts[0]}"
version="${pkg_parts[1]}"
else
error 'Error parsing package version'
error 'Error parsing package version'
return 1
fi
## split by user name and repo
{
OLDIFS="${IFS}"
OLDIFS="$IFS"
IFS='/'
# shellcheck disable=SC2206
pkg_parts=(${pkg})
IFS="${OLDIFS}"
pkg_parts=($pkg)
IFS="$OLDIFS"
}
if [[ ${#pkg_parts[@]} -eq 1 ]]; then
user="${BPKG_USER}"
user="$BPKG_USER"
name="${pkg_parts[0]}"
elif [[ ${#pkg_parts[@]} -eq 2 ]]; then
user="${pkg_parts[0]}"
@ -278,26 +282,24 @@ bpkg_install_from_remote () {
name=${name/@*//}
name=${name////}
## check to see if remote is raw with oauth (GHE)
if [[ "${remote:0:10}" == "raw-oauth|" ]]; then
info 'Using OAUTH basic with content requests'
OLDIFS="${IFS}"
OLDIFS="$IFS"
IFS="'|'"
# shellcheck disable=SC2206
local remote_parts=($remote)
IFS="${OLDIFS}"
local remote_parts=("$remote")
IFS="$OLDIFS"
local token=${remote_parts[1]}
remote=${remote_parts[2]}
auth_param="$token:x-oauth-basic"
uri="/${user}/${name}/raw/${version}"
uri="/$user/$name/raw/$version"
## If git remote is a URL, and doesn't contain token information, we
## inject it into the <user>@host field
if [[ "$git_remote" == https://* ]] && [[ "$git_remote" != *x-oauth-basic* ]] && [[ "$git_remote" != *${token}* ]]; then
if [[ "$git_remote" == https://* ]] && [[ "$git_remote" != *x-oauth-basic* ]] && [[ "$git_remote" != *$token* ]]; then
git_remote=${git_remote/https:\/\//https:\/\/$token:x-oauth-basic@}
fi
else
uri="/${user}/${name}/${version}"
uri="/$user/$name/$version"
fi
## clean up extra slashes in uri
@ -309,47 +311,41 @@ bpkg_install_from_remote () {
## error since the user may have intended to install the package
## from the broken remote.
{
if ! url_exists "${remote}" "${auth_param}"; then
error "Remote unreachable: ${remote}"
if ! url_exists "$remote" "$auth_param"; then
error "Remote unreachable: $remote"
return 2
fi
}
## build url
url="${remote}${uri}"
repo_url="${git_remote}/${user}/${name}.git"
url="$remote$uri"
repo_url="$git_remote/$user/$name.git"
local nonce="$(date +%s)"
if url_exists "$url/bpkg.json?$nonce" "$auth_param"; then
## read 'bpkg.json'
json=$(fetch "$url/bpkg.json?$nonce" "$auth_param")
package_file='bpkg.json'
has_pkg_json=1
elif url_exists "$url/package.json?$nonce" "$auth_param"; then
## read 'package.json'
json=$(fetch "$url/package.json?$nonce" "$auth_param")
package_file='package.json'
has_pkg_json=1
fi
## determine if `bpkg.json` or 'package.json' exists at url
{
if url_exists "${url}/bpkg.json?$(date +%s)" "${auth_param}"; then
has_pkg_json=2
else
warn 'bpkg.json doesn`t exist'
if url_exists "${url}/package.json?$(date +%s)" "${auth_param}"; then
has_pkg_json=1
else
warn 'package.json doesn`t exist'
has_pkg_json=0
# check to see if there's a Makefile. If not, this is not a valid package
if ! url_exists "${url}/Makefile?$(date +%s)" "${auth_param}"; then
warn "Makefile not found, skipping remote: $url"
return 1
fi
fi
if (( 0 == has_pkg_json )); then
## check to see if there's a Makefile. If not, this is not a valid package
if ! url_exists "$url/Makefile?$nonce" "$auth_param"; then
warn "Makefile not found, skipping remote: $url"
return 1
fi
}
## read bpkg.json or package.json
if (( 2 == has_pkg_json )); then
json=$(read_package_json "${url}/bpkg.json?$(date +%s)" "${auth_param}")
elif (( 1 == has_pkg_json )); then
json=$(read_package_json "${url}/package.json?$(date +%s)" "${auth_param}")
fi
if (( has_pkg_json > 0 )); then
if (( 1 == has_pkg_json )); then
## get package name from 'bpkg.json' or 'package.json'
name="$(
echo -n "${json}" |
echo -n "$json" |
bpkg-json -b |
grep -m 1 '"name"' |
awk '{ $1=""; print $0 }' |
@ -358,45 +354,43 @@ bpkg_install_from_remote () {
)"
## check if forced global
if [[ "$(echo -n "${json}" | bpkg-json -b | grep '\["global"\]' | awk '{ print $2 }' | tr -d '"')" == 'true' ]]; then
if [[ "$(echo -n "$json" | bpkg-json -b | grep '\["global"\]' | awk '{ print $2 }' | tr -d '"')" == 'true' ]]; then
needs_global=1
fi
## construct scripts array
{
scripts=$(echo -n "${json}" | bpkg-json -b | grep '\["scripts' | awk '{ print $2 }' | tr -d '"')
scripts=$(echo -n "$json" | bpkg-json -b | grep '\["scripts' | awk '{ print $2 }' | tr -d '"')
## create array by splitting on newline
OLDIFS="${IFS}"
OLDIFS="$IFS"
IFS=$'\n'
# shellcheck disable=SC2206
scripts=(${scripts[@]})
IFS="${OLDIFS}"
IFS="$OLDIFS"
}
## construct files array
{
files=$(echo -n "${json}" | bpkg-json -b | grep '\["files' | awk '{ print $2 }' | tr -d '"')
files=($(echo -n "$json" | bpkg-json -b | grep '\["files' | awk '{ print $2 }' | tr -d '"'))
## create array by splitting on newline
OLDIFS="${IFS}"
OLDIFS="$IFS"
IFS=$'\n'
# shellcheck disable=SC2206
files=(${files[@]})
IFS="${OLDIFS}"
files=("${files[@]}")
IFS="$OLDIFS"
}
fi
## build global if needed
if (( 1 == needs_global )); then
if (( has_pkg_json > 0 )); then
## install bin if needed
build="$(echo -n "${json}" | bpkg-json -b | grep '\["install"\]' | awk '{$1=""; print $0 }' | tr -d '\"')"
build="$(echo -n "${build}" | sed -e 's/^ *//' -e 's/ *$//')"
build="$(echo -n "$json" | bpkg-json -b | grep '\["install"\]' | awk '{$1=""; print $0 }' | tr -d '\"')"
build="$(echo -n "$build" | sed -e 's/^ *//' -e 's/ *$//')"
fi
if [[ -z "${build}" ]]; then
if [[ -z "$build" ]]; then
warn 'Missing build script'
warn 'Trying "make install"...'
build='make install'
@ -411,66 +405,63 @@ bpkg_install_from_remote () {
build="env PREFIX=$PREFIX $build"
fi
{ (
{(
## go to tmp dir
cd "$( [[ -n "${TMPDIR}" ]] && echo "${TMPDIR}" || echo /tmp)" &&
## prune existing
rm -rf "${name}-${version}" &&
## shallow clone
info "Cloning ${repo_url} to ${name}-${version}"
git clone "${repo_url}" "${name}-${version}" &&
(
## move into directory
cd "${name}-${version}" &&
(
## checkout to branch version
git checkout ${version} ||
## checkout into branch 'main' just in case 'master' was renamed
[ "${version}" = "master" ] && git checkout main
) &&
## build
info "Performing install: \`${build}'"
build_output=$(eval "${build}")
echo "${build_output}"
) &&
## clean up
rm -rf "${name}-${version}"
) }
cd "$([[ -n "$TMPDIR" ]] && echo "$TMPDIR" || echo /tmp)" &&
## prune existing
rm -rf "$name-$version" &&
## shallow clone
info "Cloning $repo_url to $name-$version"
git clone "$repo_url" "$name-$version" && (
## move into directory
cd "$name-$version" && (
## checkout to branch version or checkout into
## branch 'main' just in case 'master' was renamed
git checkout "$version" ||
([ "$version" = "master" ] && git checkout main 2>/dev/null) ||
(git checkout master)
)
## build
info "Performing install: \`$build'"
mkdir -p "$PREFIX"/{bin,lib}
build_output=$(eval "$build")
echo "$build_output"
)
## clean up
rm -rf "$name-$version"
)}
## perform local install otherwise
else
## copy bpkg.json or package.json over
if (( 2 == has_pkg_json )); then
save_remote_file "${url}/bpkg.json" "${cwd}/deps/${name}/bpkg.json" "${auth_param}"
elif (( 1 == has_pkg_json )); then
save_remote_file "${url}/package.json" "${cwd}/deps/${name}/package.json" "${auth_param}"
fi
## copy 'bpkg.json' or 'package.json' over
save_remote_file "$url/$package_file" "$cwd/deps/$name/$package_file" "$auth_param"
## make 'deps/' directory if possible
mkdir -p "${cwd}/deps/${name}"
mkdir -p "$cwd/deps/$name"
## make 'deps/bin' directory if possible
mkdir -p "${cwd}/deps/bin"
mkdir -p "$cwd/deps/bin"
# install package dependencies
info "Install dependencies for ${name}"
(cd "${cwd}/deps/${name}" && bpkg getdeps)
info "Install dependencies for $name"
(cd "$cwd/deps/$name" && bpkg getdeps)
## grab each script and place in deps directory
for script in "${scripts[@]}"; do
(
local scriptname
if [[ "${script}" ]];then
scriptname="$(echo "${script}" | xargs basename )"
if [[ "$script" ]];then
local scriptname="$(echo "$script" | xargs basename )"
info "fetch" "${url}/${script}"
info "write" "${cwd}/deps/${name}/${script}"
save_remote_file "${url}/${script}" "${cwd}/deps/${name}/${script}" "${auth_param}"
info "fetch" "$url/$script"
info "write" "$cwd/deps/$name/$script"
save_remote_file "$url/$script" "$cwd/deps/$name/$script" "$auth_param"
scriptname="${scriptname%.*}"
info "${scriptname} to PATH" "${cwd}/deps/bin/${scriptname}"
ln -si "${cwd}/deps/${name}/${script}" "${cwd}/deps/bin/${scriptname}"
chmod u+x "${cwd}/deps/bin/${scriptname}"
info "$scriptname to PATH" "$cwd/deps/bin/$scriptname"
ln -si "$cwd/deps/$name/$script" "$cwd/deps/bin/$scriptname"
chmod u+x "$cwd/deps/bin/$scriptname"
fi
)
done
@ -479,10 +470,10 @@ bpkg_install_from_remote () {
## grab each file and place in correct directory
for file in "${files[@]}"; do
(
if [[ "${file}" ]];then
info "fetch" "${url}/${file}"
info "write" "${cwd}/deps/${name}/${file}"
save_remote_file "${url}/${file}" "${cwd}/deps/${name}/${file}" "${auth_param}"
if [[ "$file" ]];then
info "fetch" "$url/$file"
info "write" "$cwd/deps/$name/$file"
save_remote_file "$url/$file" "$cwd/deps/$name/$file" "$auth_param"
fi
)
done
@ -495,7 +486,7 @@ bpkg_install_from_remote () {
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_install
elif validate_parameters; then
bpkg_install "${@}"
bpkg_install "$@"
exit $?
else
#param validation failed

@ -1,6 +1,6 @@
#! /usr/bin/env bash
cd ${0%/*}
cd "${0%/*}" || exit
#set -e
fail=0
@ -10,14 +10,14 @@ tests=0
for test in test/*.sh ;
do
tests=$((tests+1))
echo TEST: $test
./$test
echo TEST: "$test"
./"$test"
ret=$?
if [ $ret -eq 0 ] ; then
echo OK: ---- $test
echo OK: ---- "$test"
passed=$((passed+1))
else
echo FAIL: $test $fail
echo FAIL: "$test" $fail
fail=$((fail+ret))
fi
done

@ -1,12 +1,12 @@
#! /usr/bin/env bash
cd ${0%/*}
cd "${0%/*}" || exit
# make test output TAP compatible
# http://en.wikipedia.org/wiki/Test_Anything_Protocol
fails=0
tests=`ls invalid/* -1 | wc -l`
tests=$(ls invalid/* -1 | wc -l)
echo "1..${tests##* }"
for input in invalid/*
@ -22,7 +22,7 @@ do
fails=$((fails+1))
else
echo "ok $i - $input was rejected"
echo "#" `cat /tmp/JSON.sh_errlog`
echo "#" $(cat /tmp/JSON.sh_errlog)
fi
done
echo "$fails test(s) failed"

@ -1,6 +1,6 @@
#! /usr/bin/env bash
cd ${0%/*}
cd "${0%/*}" || exit
. ../JSON.sh
@ -14,7 +14,7 @@ echo "1..4"
for input in '"oooo" ' '[true, 1, [0, {}]] ' '{"true": 1}'
do
i=$((i+1))
if echo "$input" | ptest
if echo "$input" | ptest
then
echo "ok $i - $input"
else
@ -23,12 +23,12 @@ do
fi
done
if ! ptest < ../package.json
if ! ptest < ../bpkg.json
then
echo "not ok 4 - Parsing package.json failed!"
echo "not ok 4 - Parsing bpkg.json failed!"
fails=$((fails+1))
else
echo "ok $i - package.json"
echo "ok $i - bpkg.json"
fi
echo "$fails test(s) failed"

@ -1,6 +1,6 @@
#! /usr/bin/env bash
cd ${0%/*}
cd "${0%/*}" || exit
. ../JSON.sh
i=0
@ -44,10 +44,10 @@ ttest '[ null , -110e10, "null" ]' \
ttest '{"e": false}' '{' '"e"' ':' 'false' '}'
ttest '{"e": "string"}' '{' '"e"' ':' '"string"' '}'
if ! cat ../package.json | tokenize >/dev/null
if ! cat ../bpkg.json | tokenize >/dev/null
then
fails=$((fails+1))
echo "Tokenizing package.json failed!"
echo "Tokenizing bpkg.json failed!"
fi
echo "$fails test(s) failed"

@ -1,9 +1,9 @@
#! /usr/bin/env bash
cd ${0%/*}
cd "${0%/*}" || exit
fails=0
i=0
tests=`ls valid/*.json -1l | wc -l`
tests=$(ls valid/*.json -1l | wc -l)
echo "1..$tests"
for input in valid/*.json
do

@ -27,7 +27,7 @@ usage () {
bpkg_list () {
local verbose=0
for opt in "${@}"; do
case "$opt" in
case "$opt" in
-V|--version)
echo "${VERSION}"
return 0
@ -51,19 +51,21 @@ bpkg_list () {
for remote in "${BPKG_REMOTES[@]}"; do
local git_remote="${BPKG_GIT_REMOTES[$i]}"
bpkg_select_remote "$remote" "$git_remote"
if [ ! -f "$BPKG_REMOTE_INDEX_FILE" ]; then
bpkg_warn "no index file found for remote: ${remote}"
bpkg_warn "You should run \`bpkg update' before running this command."
continue
fi
OLDIFS="$IFS"
IFS=$'\n'
local line
while read -r line; do
local desc name
name=$(echo "$line" | cut -d\| -f1 | tr -d ' ')
desc=$(echo "$line" | cut -d\| -f2)
for line in $(cat "$BPKG_REMOTE_INDEX_FILE"); do
local name=$(echo "$line" | cut -d\| -f1 | tr -d ' ')
local desc=$(echo "$line" | cut -d\| -f2)
local host=$BPKG_REMOTE_HOST
if [ "$verbose" == "1" ]; then
echo "$name [$host] - $desc"
else
@ -76,7 +78,6 @@ bpkg_list () {
done
}
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_list
elif bpkg_validate; then

@ -9,15 +9,10 @@ usage () {
## Read a package property
bpkg_package () {
local cwd pkg prop
prop="${1}"
cwd="$(pwd)"
pkg="${cwd}/package.json"
if ! test -f "${pkg}"; then
pkg="${cwd}/package.json"
fi
local cwd="$(pwd)"
local prop="${1}"
local pkgs=("$cwd/bpkg.json" "${cwd}/package.json")
local npkgs="${#pkgs[@]}"
## parse flags
case "${prop}" in
@ -27,30 +22,28 @@ bpkg_package () {
;;
esac
## ensure there is a package to read
if ! test -f "${pkg}"; then
echo 2>&1 "error: Unable to find 'bpkg.json' or 'package.json' in $(pwd)"
return 1
fi
if [ -z "${prop}" ]; then
## output all propertyies if property
## is ommited
{
# shellcheck disable=SC2002
cat "${pkg}" | bpkg-json -b
}
else
## show value for a specific property
## in 'bpkg.json' or 'package.json'
{
# shellcheck disable=SC2002
cat "${pkg}" | bpkg-json -b | grep "${prop}" | awk '{ $1=""; printf $0 }'
echo
}
fi
return 0
## attempt to find JSON manifest and query it
for (( i = 0; i < npkgs; i++ )); do
local pkg="${pkgs[$i]}"
if test -f "$pkg"; then
if [ -z "$prop" ]; then
## output all propertyies if property
## is ommited
cat "$pkg" | bpkg-json -b
else
## show value for a specific property
## in 'bpkg.json' or 'package.json'
cat "$pkg" | bpkg-json -b | grep "$prop" | awk '{ $1=""; printf $0 }' | tr -d '"'
echo
fi
return 0
fi
done
echo 2>&1 "error: Unable to find \`bpkg.json' or \`package.json' in $cwd"
return 1
}
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then

@ -43,6 +43,7 @@ show_package () {
local show_sources=$4
local remote=$BPKG_REMOTE
local git_remote=$BPKG_GIT_REMOTE
local nonce="$(date +%s)"
local auth=""
local json=""
local readme=""
@ -58,11 +59,16 @@ show_package () {
uri=$BPKG_REMOTE/$pkg/raw/master
fi
json=$(eval "curl $auth -sL '$uri/bpkg.json?$(date +%s)'")
json=$(eval "curl $auth -sL '$uri/bpkg.json?$nonce'" 2>/dev/null)
if [ "${json}" = '404: Not Found' ];then
json=$(eval "curl $auth -sL '$uri/package.json?$(date +%s)'")
json=$(eval "curl $auth -sL '$uri/package.json?$nonce'" 2>/dev/null)
fi
readme=$(eval "curl $auth -sL '$uri/README.md?$(date +%s)'")
if [ -z "$json" ]; then
echo 1>&2 "error: Failed to load package JSON"
fi
readme=$(eval "curl $auth -sL '$uri/README.md?$nonce'")
local author install_sh pkg_desc readme_len sources version
@ -174,6 +180,7 @@ bpkg_show () {
OLDIFS="$IFS"
IFS=$'\n'
local line
while read -r line; do
local desc name

Loading…
Cancel
Save