Skip to content

Commit

Permalink
(bpkg-env): introduce 'bpkg-env
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Mar 28, 2022
1 parent da38fcd commit c5ecb73
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 81 deletions.
1 change: 1 addition & 0 deletions bpkg-env
3 changes: 2 additions & 1 deletion bpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"repo": "bpkg/bpkg",
"install": "bash setup.sh",
"commands": {
"lint": "shellcheck $BPKG_SCRIPT_SOURCES"
"lint": "command shellcheck **/*.sh",
"list-sources": "echo ${BPKG_SCRIPT_SOURCES[@]} | sed 's/ /\n/g'"
}
}
203 changes: 203 additions & 0 deletions lib/env/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#!/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-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

# Include config rc file if found
BPKG_USER_CONFIG_FILE="$HOME/.bpkgrc"

# Include config rc file if found
BPKG_LOCAL_CONFIG_FILE="$(pwd)/.bpkgrc"

## meta
export BPKG_DATE="$(date)"
export BPKG_HOME="${BPKG_HOME:-$HOME}"
export BPKG_INDEX="$BPKG_INDEX"

## os
export BPKG_OS="$(uname)"
export BPKG_CWD="$(pwd)"
export BPKG_BIN="$(which bpkg)"
export BPKG_USER="$(uname)"

## package
export BPKG_PACKAGE_USER
export BPKG_PACKAGE_NAME="$(bpkg_package name 2>/dev/null)"
export BPKG_PACKAGE_REPO="$(bpkg_package repo 2>/dev/null)"
export BPKG_PACKAGE_DEPS="${BPKG_PACKAGE_DEPS:-deps}"
export BPKG_PACKAGE_VERSION="$(bpkg_package version 2>/dev/null)"
export BPKG_PACKAGE_DESCRIPTION="$(bpkg_package description 2>/dev/null)"

## remote
export BPKG_REMOTE="$BPKG_REMOTE"
# shellcheck disable=SC2178
export BPKG_REMOTES="${BPKG_REMOTE[*]}"
export BPKG_REMOTE_RAW_PATH="$BPKG_REMOTE_RAW_PATH"

if test -f bpkg.json || test -f package.json; then
declare -a BPKG_SCRIPT_SOURCES=()
BPKG_SCRIPT_SOURCES+=($(find . -name '*.sh'))
BPKG_SCRIPT_SOURCES+=($(find . -name '*.zsh'))
BPKG_SCRIPT_SOURCES+=($(find . -name '*.bash'))
export BPKG_SCRIPT_SOURCES
fi

## output usage
usage () {
echo 'usage: bpkg-env [-h|--help]'
echo ' or: bpkg-env <key|pattern> [--value]'
echo
echo 'example:'
echo '$ bpkg-env BPKG_PACKAGE*'
echo 'BPKG_PACKAGE_DEPS="deps"'
echo 'BPKG_PACKAGE_DESCRIPTION="Lightweight bash package manager"'
echo 'BPKG_PACKAGE_NAME="bpkg"'
echo 'BPKG_PACKAGE_REPO="bpkg/bpkg"'
echo 'BPKG_PACKAGE_USER="Linux"'
echo 'BPKG_PACKAGE_VERSION="1.0.7"'
}

bpkg_env () {
local key=""
local should_emit_value=0

for opt in "$@"; do
case "$opt" in
-h|--help)
usage
return 0
;;

--value)
should_emit_value=1
shift
;;
-*)
bpkg_error "Unknown option: \`$opt'"
return 2
;;

*)
key="$1"
shift
;;
esac
done

{
printenv
echo BPKG_SCRIPT_SOURCES=\""${BPKG_SCRIPT_SOURCES[*]}"\"
} | \
grep '^\s*BPKG_' | \
sed 's/^\s*//g' | \
sort | \
{
did_emit=0

while read -r line; do
local kv=("" "")
local j=0

for (( i = 0; i < ${#line}; ++i )); do
if [ "${line:$i:1}" = "=" ] && (( j == 0 )); then
(( j++ ))
continue
fi

kv[$j]+=${line:$i:1}
done

if [ -n "$key" ]; then
regex="^${key/'*'/'.*'}$"
if ! [[ "${kv[0]}" =~ $regex ]]; then
continue
fi
fi

if [ -z "${kv[1]}" ]; then
continue
fi

if [ "${kv[1]:0:1}" = "$" ] || [ "${kv[1]:1:1}" = "$" ]; then
continue
fi

if [ "${kv[1]:0:1}" = ";" ]; then
continue
fi

if [ "${kv[1]}" = '"";' ]; then
continue
fi

if (( should_emit_value == 0 )); then
printf '%s=' "${kv[0]}"
fi

if [ "${kv[1]:0:1}" != '"' ]; then
printf '"'
fi

printf '%s' "${kv[1]}"

l=${#kv[1]}
lm1=$(( l - 1 ))
lm2=$(( l - 2 ))

if [ "${kv[1]:$lm1:1}" != '"' ] && [ "${kv[1]:$lm2:1}" != '"' ] && [ "${kv[1]:$lm1:1}" != ';' ] ; then
printf '"'
fi

# shellcheck disable=SC2030,SC2031
did_emit=1
echo
done

if (( did_emit == 1 )); then
return 0
fi

return 1
}

return $?
}

if [[ -f "$BPKG_USER_CONFIG_FILE" ]] && [ -z "$BPKG_USER_CONFIG_FILE_LOADED" ]; then
export BPKG_USER_CONFIG_FILE
export BPKG_USER_CONFIG_FILE_LOADED="1"
# shellcheck disable=SC1090
source "$BPKG_USER_CONFIG_FILE_LOADED"
fi

if [[ -f "$BPKG_LOCAL_CONFIG_FILE" ]] && [ -z "$BPKG_LOCAL_CONFIG_FILE_LOADED" ]; then
export BPKG_LOCAL_CONFIG_FILE
export BPKG_LOCAL_CONFIG_FILE_LOADED="1"
# shellcheck disable=SC1090
source "$BPKG_LOCAL_CONFIG_FILE"
fi

## Use as lib or perform install
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f bpkg_env
else
bpkg_env "$@"
exit $?
fi
47 changes: 21 additions & 26 deletions lib/install/install.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#!/bin/bash

# Include config rc file if found
CONFIG_FILE="$HOME/.bpkgrc"
# shellcheck disable=SC1090
[[ -f "$CONFIG_FILE" ]] && source "$CONFIG_FILE"

## set defaults
if [[ ${#BPKG_REMOTES[@]} -eq 0 ]]; then
BPKG_REMOTES[0]=${BPKG_REMOTE-https://raw.githubusercontent.com}
BPKG_GIT_REMOTES[0]=${BPKG_GIT_REMOTE-https://github.com}
if ! type -f bpkg-env &>/dev/null; then
echo "error: bpkg-env not found, aborting"
exit 1
else
# shellcheck disable=SC2230
# shellcheck source=lib/env/env.sh
source "$(which bpkg-env)"
fi
BPKG_USER="${BPKG_USER:-bpkg}"
BPKG_DEPS="${BPKG_DEPS:-deps}"

let prevent_prune=0

Expand Down Expand Up @@ -278,7 +273,7 @@ bpkg_install_from_remote () {
}

if [[ ${#pkg_parts[@]} -eq 1 ]]; then
user="$BPKG_USER"
user="$BPKG_PACKAGE_DEFAULT_USER"
name="${pkg_parts[0]}"
elif [[ ${#pkg_parts[@]} -eq 2 ]]; then
user="${pkg_parts[0]}"
Expand Down Expand Up @@ -464,17 +459,17 @@ bpkg_install_from_remote () {
## perform local install otherwise
else
## copy 'bpkg.json' or 'package.json' over
save_remote_file "$url/$package_file" "$cwd/$BPKG_DEPS/$name/$package_file" "$auth_param"
save_remote_file "$url/$package_file" "$cwd/$BPKG_PACKAGE_DEPS/$name/$package_file" "$auth_param"

## make '$BPKG_DEPS/' directory if possible
mkdir -p "$cwd/$BPKG_DEPS/$name"
## make '$BPKG_PACKAGE_DEPS/' directory if possible
mkdir -p "$cwd/$BPKG_PACKAGE_DEPS/$name"

## make '$BPKG_DEPS/bin' directory if possible
mkdir -p "$cwd/$BPKG_DEPS/bin"
## make '$BPKG_PACKAGE_DEPS/bin' directory if possible
mkdir -p "$cwd/$BPKG_PACKAGE_DEPS/bin"

# install package dependencies
info "Install dependencies for $name"
(cd "$cwd/$BPKG_DEPS/$name" && bpkg getdeps)
(cd "$cwd/$BPKG_PACKAGE_DEPS/$name" && bpkg getdeps)

## grab each script and place in deps directory
for script in "${scripts[@]}"; do
Expand All @@ -483,13 +478,13 @@ bpkg_install_from_remote () {
local scriptname="$(echo "$script" | xargs basename )"

info "fetch" "$url/$script"
info "write" "$cwd/$BPKG_DEPS/$name/$script"
save_remote_file "$url/$script" "$cwd/$BPKG_DEPS/$name/$script" "$auth_param"
info "write" "$cwd/$BPKG_PACKAGE_DEPS/$name/$script"
save_remote_file "$url/$script" "$cwd/$BPKG_PACKAGE_DEPS/$name/$script" "$auth_param"

scriptname="${scriptname%.*}"
info "$scriptname to PATH" "$cwd/$BPKG_DEPS/bin/$scriptname"
ln -si "$cwd/$BPKG_DEPS/$name/$script" "$cwd/$BPKG_DEPS/bin/$scriptname"
chmod u+x "$cwd/$BPKG_DEPS/bin/$scriptname"
info "$scriptname to PATH" "$cwd/$BPKG_PACKAGE_DEPS/bin/$scriptname"
ln -si "$cwd/$BPKG_PACKAGE_DEPS/$name/$script" "$cwd/$BPKG_PACKAGE_DEPS/bin/$scriptname"
chmod u+x "$cwd/$BPKG_PACKAGE_DEPS/bin/$scriptname"
fi
)
done
Expand All @@ -500,8 +495,8 @@ bpkg_install_from_remote () {
(
if [[ "$file" ]];then
info "fetch" "$url/$file"
info "write" "$cwd/$BPKG_DEPS/$name/$file"
save_remote_file "$url/$file" "$cwd/$BPKG_DEPS/$name/$file" "$auth_param"
info "write" "$cwd/$BPKG_PACKAGE_DEPS/$name/$file"
save_remote_file "$url/$file" "$cwd/$BPKG_PACKAGE_DEPS/$name/$file" "$auth_param"
fi
)
done
Expand Down
Loading

0 comments on commit c5ecb73

Please sign in to comment.