From 9ea0f02b76d6ad7283d71a764dabe6dcec49974e Mon Sep 17 00:00:00 2001 From: jwerle Date: Fri, 18 Mar 2022 14:27:22 -0400 Subject: [PATCH] feat(): introduce 'bpkg-run' --- .shellcheckrc | 2 + README.md | 8 +- bpkg-run | 1 + lib/init/{package.json => bpkg.json} | 0 lib/install/install.sh | 14 +++- lib/package/package.sh | 24 ++++-- lib/run/run.sh | 106 ++++++++++++++++++++++++ lib/suggest/{package.json => bpkg.json} | 0 lib/term/{package.json => bpkg.json} | 0 setup.sh | 6 +- 10 files changed, 145 insertions(+), 16 deletions(-) create mode 120000 bpkg-run rename lib/init/{package.json => bpkg.json} (100%) create mode 100755 lib/run/run.sh rename lib/suggest/{package.json => bpkg.json} (100%) rename lib/term/{package.json => bpkg.json} (100%) diff --git a/.shellcheckrc b/.shellcheckrc index 5cd5c56..fc479f7 100644 --- a/.shellcheckrc +++ b/.shellcheckrc @@ -2,3 +2,5 @@ disable=SC2034 ## Allow declare and assign separately to avoid masking return values disable=SC2155 +## Double quote array expansions to avoid re-splitting elements. +disable=SC2068 diff --git a/README.md b/README.md index c2e0603..498ccc5 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,10 @@ You can install a packages dependencies with the `bpkg getdeps` command. These w _Note: There is no protection against circular dependencies, so be careful!_ +### Running packages with `bpkg` + +You can run a package script with `bpkg run` which will install your +package globally and execute it as a command ### Retrieving package info @@ -242,7 +246,6 @@ This is a hash of dependencies. The keys are the package names, and the values a } ``` - ## Packaging best practices These are guidelines that we strongly encourage developers to follow. @@ -295,15 +298,12 @@ Support this project by becoming a sponsor. Your logo will show up here with a l This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)]. - ### Backers Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/bpkg#backer)] - - ## License `bpkg` is released under the **MIT license**. diff --git a/bpkg-run b/bpkg-run new file mode 120000 index 0000000..0cbfcc4 --- /dev/null +++ b/bpkg-run @@ -0,0 +1 @@ +lib/run/run.sh \ No newline at end of file diff --git a/lib/init/package.json b/lib/init/bpkg.json similarity index 100% rename from lib/init/package.json rename to lib/init/bpkg.json diff --git a/lib/install/install.sh b/lib/install/install.sh index ed59aee..f10c3d1 100755 --- a/lib/install/install.sh +++ b/lib/install/install.sh @@ -13,6 +13,8 @@ fi BPKG_USER="${BPKG_USER:-bpkg}" BPKG_DEPS="${BPKG_DEPS:-deps}" +let prevent_prune=0 + ## check parameter consistency validate_parameters () { if [[ ${#BPKG_GIT_REMOTES[@]} -ne ${#BPKG_REMOTES[@]} ]]; then @@ -175,6 +177,10 @@ bpkg_install () { shift needs_global=1 ;; + --no-prune) + shift + prevent_prune=1 + ;; *) if [[ '-' = "${opt:0:1}" ]]; then @@ -410,10 +416,10 @@ bpkg_install_from_remote () { ## go to tmp dir cd "$([[ -n "$TMPDIR" ]] && echo "$TMPDIR" || echo /tmp)" && ## prune existing - rm -rf "$name-$version" && + ( (( 0 == prevent_prune )) && rm -rf "$name-$version" || true) && ## shallow clone - info "Cloning $repo_url to $name-$version" + info "Cloning $repo_url to $(pwd)/$name-$version" git clone "$repo_url" "$name-$version" && ( ## move into directory cd "$name-$version" && ( @@ -432,7 +438,9 @@ bpkg_install_from_remote () { ) ## clean up - rm -rf "$name-$version" + if (( 0 == prevent_prune )); then + rm -rf "$name-$version" + fi )} ## perform local install otherwise else diff --git a/lib/package/package.sh b/lib/package/package.sh index 4fee839..95fdfe5 100755 --- a/lib/package/package.sh +++ b/lib/package/package.sh @@ -1,5 +1,13 @@ #!/bin/bash +BPKG_JSON="$(which bpkg-json)" + +if [ -z "$BPKG_JSON" ]; then + BPKG_JSON="$(realpath "$0/../JSON/JSON.sh")" +else + BPKG_JSON="$(realpath "$BPKG_JSON")" +fi + ## output usage usage () { echo "usage: bpkg-package [-h|--help]" @@ -30,11 +38,11 @@ bpkg_package () { if [ -z "$prop" ]; then ## output all propertyies if property ## is ommited - cat "$pkg" | bpkg-json -b + 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 '"' + cat "$pkg" | "$BPKG_JSON" -b | grep "$prop" | awk '{ $1=""; printf $0 }' | tr -d '"' | sed 's/^ *//;s/ *$//' echo fi @@ -46,9 +54,13 @@ bpkg_package () { return 1 } -if [[ ${BASH_SOURCE[0]} != "$0" ]]; then - export -f bpkg_package +if [ -z "$BPKG_JSON" ]; then + echo 1>&2 "error: Failed to load 'bpkg-json'" else - bpkg_package "${@}" - exit $? + if [[ ${BASH_SOURCE[0]} != "$0" ]]; then + export -f bpkg_package + else + bpkg_package "${@}" + exit $? + fi fi diff --git a/lib/run/run.sh b/lib/run/run.sh new file mode 100755 index 0000000..3ee2f71 --- /dev/null +++ b/lib/run/run.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +if ! type -f bpkg-utils &>/dev/null; then + echo "error: bpkg-utils not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/utils/utils.sh + source "$(which bpkg-utils)" +fi + +if ! type -f bpkg-install &>/dev/null; then + echo "error: bpkg-install not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/install/install.sh + source "$(which bpkg-install)" +fi + +if ! type -f bpkg-package &>/dev/null; then + echo "error: bpkg-package not found, aborting" + exit 1 +else + # shellcheck disable=SC2230 + # shellcheck source=lib/package/package.sh + source "$(which bpkg-package)" +fi + +bpkg_initrc + +## outut usage +usage () { + echo 'usage: bpkg-run [-h|--help]' + echo ' or: bpkg-run [-s|--source] ' + echo ' or: bpkg-run [-s|--source] /' +} + +bpkg_run () { + pushd . >/dev/null || return $? + local ignore_args=0 + local needs_name=0 + local package='' + local name='' + + for opt in "$@"; do + case "$opt" in + -h|--help) + if (( 0 == ignore_args )); then + usage + return 0 + fi + ;; + + -s|--source) + if (( 0 == ignore_args )); then + # shellcheck disable=SC1090 + source "$(which "$name")" + return $? + fi + ;; + + -t|--target|--name) + if (( 0 == ignore_args )); then + shift + needs_name=1 + fi + ;; + + *) + ignore_args=1 + if (( 1 == needs_name )); then + name="$opt" + shift + needs_name=0 + fi + ;; + esac + done + + local dest=$(bpkg_install --no-prune -g "$1" 2>/dev/null | grep 'info: Cloning' | sed 's/.* to //g' | xargs echo) + + if [ -z "$dest" ]; then return $?; fi + + cd "$dest" || return $? + + if [ -z "$name" ]; then + name="$(bpkg_package name)" + fi + + shift + popd >/dev/null || return $? + eval "$(which "$name")" $@ + return $? +} + +## Use as lib or perform install +if [[ ${BASH_SOURCE[0]} != "$0" ]]; then + export -f bpkg_run +elif validate_parameters; then + bpkg_run "$@" + exit $? +else + #param validation failed + exit $? +fi diff --git a/lib/suggest/package.json b/lib/suggest/bpkg.json similarity index 100% rename from lib/suggest/package.json rename to lib/suggest/bpkg.json diff --git a/lib/term/package.json b/lib/term/bpkg.json similarity index 100% rename from lib/term/package.json rename to lib/term/bpkg.json diff --git a/setup.sh b/setup.sh index fb7509a..5fcfb54 100755 --- a/setup.sh +++ b/setup.sh @@ -43,13 +43,13 @@ setup () { ## build { echo - cd "${TMPDIR}" + cd "${TMPDIR}" || exit echo " info: Creating temporary files..." test -d "${DEST}" && { echo " warn: Already exists: '${DEST}'"; } rm -rf "${DEST}" echo " info: Fetching latest 'bpkg'..." git clone --depth=1 "${REMOTE}" "${DEST}" > /dev/null 2>&1 - cd "${DEST}" + cd "${DEST}" || exit echo " info: Installing..." echo make_install @@ -69,7 +69,7 @@ if [ -z "$PREFIX" ]; then fi # All 'bpkg' supported commands -CMDS="json install package term suggest init utils update list show getdeps" +CMDS="json install package term suggest init utils update list show getdeps run" make_install () { local source