refactor(): support 'commands' for 'bpkg run' and 'BPKG_SCRIPT_SOURCES' env var

pull/143/head
jwerle 2 years ago committed by Joseph Werle
parent f0b5ee6ba7
commit edaf0cab93

@ -1,20 +1,16 @@
# shellcheck disable=SC2034
## Allow: To read lines rather than words, pipe/redirect to a 'while read' loop
disable=SC2013
## Allow: Unsued variables
disable=SC2034
## Allow: Double quote array expansions to avoid re-splitting elements
disable=SC2068
## Allow: Declare and assign separately to avoid masking return values
disable=SC2155
## Allow: Quote to prevent word splitting/globbing, or split robustly with mapfile or read -a.
disable=SC2206
## Allow: Prefer mapfile or read -a to split command output (or quote to avoid splitting)
disable=SC2207
## Allow: Instead of 'let expr', prefer (( expr ))
disable=SC2219
## Allow: Not following source
disable=SC1091

@ -4,5 +4,8 @@
"description": "Lightweight bash package manager",
"global": true,
"repo": "bpkg/bpkg",
"install": "bash setup.sh"
"install": "bash setup.sh",
"commands": {
"lint": "shellcheck $BPKG_SCRIPT_SOURCES"
}
}

@ -73,7 +73,7 @@ bpkg () {
echo "Here are some commands available in your path:"
echo
local cmds=($(commands))
for cmd in ${cmds[@]}; do
for cmd in "${cmds[@]}"; do
echo " ${cmd}"
done
return 0
@ -94,7 +94,7 @@ bpkg () {
local res
declare -a res=($(commands))
if [ -n "${res}" ]; then
if [ -n "${res[*]}" ]; then
echo
echo >&2 "Did you mean one of these?"
found=0

@ -35,6 +35,7 @@ prompt_if () {
y|Y|yes|YES|Yes)
shift
shift
# shellcheck disable=SC2068
$func $@
return 0
esac

@ -156,7 +156,7 @@ fetch () {
## Install a bash package
bpkg_install () {
local pkg=''
local let needs_global=0
local needs_global=0
for opt in "$@"; do
if [[ '-' = "${opt:0:1}" ]]; then
@ -201,7 +201,7 @@ bpkg_install () {
echo
## Check each remote in order
local let i=0
local i=0
for remote in "${BPKG_REMOTES[@]}"; do
local git_remote=${BPKG_GIT_REMOTES[$i]}
if bpkg_install_from_remote "$pkg" "$remote" "$git_remote" $needs_global; then
@ -227,9 +227,8 @@ bpkg_install_from_remote () {
local pkg=$1
local remote=$2
local git_remote=$3
local let needs_global=$4
local needs_global=$4
cwd=$(pwd)
local url=''
local uri=''
local version=''
@ -238,17 +237,21 @@ bpkg_install_from_remote () {
local name=''
local version=''
local auth_param=''
local let has_pkg_json=0
local has_pkg_json=0
local package_file=''
declare -a local pkg_parts=()
declare -a local remote_parts=()
declare -a local scripts=()
declare -a local files=()
declare -a pkg_parts=()
declare -a remote_parts=()
declare -a scripts=()
declare -a files=()
cwd=$(pwd)
## get version if available
{
OLDIFS="$IFS"
IFS="@"
# shellcheck disable=SC2206
pkg_parts=($pkg)
IFS="$OLDIFS"
}
@ -268,6 +271,7 @@ bpkg_install_from_remote () {
{
OLDIFS="$IFS"
IFS='/'
# shellcheck disable=SC2206
pkg_parts=($pkg)
IFS="$OLDIFS"
}

@ -1,2 +0,0 @@
test/errlog
test/outlog

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

@ -1,14 +0,0 @@
{ "name": "JSON.sh"
, "version": "0.1.8"
, "description": "JSON parser written in bash"
, "homepage": "http://github.com/dominictarr/JSON.sh"
, "repository":
{ "type": "git"
, "url": "https://github.com/dominictarr/JSON.sh.git" }
, "bin": {
"JSON.sh": "./JSON.sh"
}
, "dependencies": {}
, "devDependencies": {}
, "author": "Dominic Tarr <dominic.tarr@gmail.com> (http://bit.ly/dominictarr)"
, "scripts": { "test": "./all-tests.sh" } }

@ -15,15 +15,40 @@ usage () {
echo " or: bpkg-package"
}
expand_grep_args () {
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
printf '\]?'
fi
return 0
}
## Read a package property
bpkg_package () {
local cwd="$(pwd)"
local prop="${1}"
local pkgs=("$cwd/bpkg.json" "${cwd}/package.json")
local npkgs="${#pkgs[@]}"
## parse flags
case "${prop}" in
case "$1" in
-h|--help)
usage
return 0
@ -35,15 +60,29 @@ bpkg_package () {
local pkg="${pkgs[$i]}"
if test -f "$pkg"; then
if [ -z "$prop" ]; then
if [ -z "$1" ]; 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 '"' | sed 's/^ *//;s/ *$//'
echo
declare -a results
# shellcheck disable=SC2068
IFS=$'\n' read -r -d '' -a results <<< "$(cat < "$pkg" | "$BPKG_JSON" -b | grep -E "$(expand_grep_args $@)")"
if (( ${#results[@]} == 1 )); then
if (( $# > 1 )); then
echo "${results[@]}" | awk '{ $1=""; printf $0 }' | tr -d '"' | sed 's/^ *//;s/ *$//'
else
echo "${results[@]}"
fi
else
for (( j = 0; j < ${#results[@]}; j++ )); do
echo "${results[j]}"
done
fi
shift
fi
return 0

@ -1,5 +1,7 @@
#!/bin/bash
shopt -s extglob
if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting"
exit 1
@ -32,19 +34,17 @@ bpkg_initrc
## outut usage
usage () {
echo 'usage: bpkg-run [-h|--help]'
echo ' or: bpkg-run [-s|--source] <package>'
echo ' or: bpkg-run [-s|--source] <user>/<package>'
echo ' or: bpkg-run [-h|--help] [command]'
echo ' or: bpkg-run [-s|--source] <package> [command]'
echo ' or: bpkg-run [-s|--source] <user>/<package> [command]'
}
bpkg_run () {
pushd . >/dev/null || return $?
local should_emit_source=0
local should_source=0
local should_clean=0
local ignore_args=0
local needs_name=0
local package=''
local dest=''
local name=''
@ -96,13 +96,28 @@ bpkg_run () {
esac
done
local cmd="$(bpkg_package commands "$1")"
BPKG_SCRIPT_SOURCES=$(find . -name '*.sh')
export BPKG_SCRIPT_SOURCES
if [ -n "$cmd" ]; then
shift
# shellcheck disable=SC2068
eval "$cmd" $@
return $?
fi
pushd . >/dev/null || return $?
if (( 0 == should_clean )); then
dest=$(bpkg_install --no-prune -g "$1" 2>/dev/null | grep 'Cloning' | sed 's/.* to //g' | xargs echo)
else
dest=$(bpkg_install -g "$1" 2>/dev/null | grep 'Cloning' | sed 's/.* to //g' | xargs echo)
fi
if [ -z "$dest" ]; then return $?; fi
if [ -z "$dest" ]; then
return $?
fi
cd "$dest" || return $?
@ -110,7 +125,10 @@ bpkg_run () {
name="$(bpkg_package name)"
fi
cmd="$(bpkg_package commands "$1")"
shift
BPKG_SCRIPT_SOURCES=$(find . -name '*.sh')
export BPKG_SCRIPT_SOURCES
popd >/dev/null || return $?
if (( 1 == should_emit_source )); then
@ -120,6 +138,14 @@ bpkg_run () {
# shellcheck disable=SC1090
source "$(which "$name")"
else
if [ -n "$cmd" ]; then
shift
# shellcheck disable=SC2068
eval "$cmd" $@
return $?
fi
# shellcheck disable=SC2068
eval "$(which "$name")" $@
fi
fi

@ -10,6 +10,7 @@ else
fi
bpkg_source () {
# shellcheck disable=SC2068
bpkg_run --emit-source $@
return $?
}

@ -74,14 +74,28 @@ CMDS="json install package term suggest init utils update list show getdeps run
make_install () {
local source
## do 'make uninstall'
make_uninstall
echo " info: Installing $PREFIX/bin/$BIN..."
install -d "$PREFIX/bin"
source=$(<$BIN)
[ -f "$source" ] && install "$source" "$PREFIX/bin/$BIN" || install "$BIN" "$PREFIX/bin"
if [ -f "$source" ]; then
install "$source" "$PREFIX/bin/$BIN"
else
install "$BIN" "$PREFIX/bin"
fi
for cmd in $CMDS; do
source=$(<"$BIN-$cmd")
[ -f "$source" ] && install "$source" "$PREFIX/bin/$BIN-$cmd" || install "$BIN-$cmd" "$PREFIX/bin"
if [ -f "$source" ]; then
install "$source" "$PREFIX/bin/$BIN-$cmd"
else
install "$BIN-$cmd" "$PREFIX/bin"
fi
done
return $?
}
@ -109,6 +123,11 @@ make_unlink () {
make_uninstall
}
## go
[ $# -eq 0 ] && setup || "make_$1"
## do setup or `make_{install|uninstall|link|unlink}` command
if [ $# -eq 0 ]; then
setup
else
"make_$1"
fi
exit $?

Loading…
Cancel
Save