-
-
Save hugot/e078b21cf0b21f9b568a99d8c128634f to your computer and use it in GitHub Desktop.
Little script to build software using basher as dependency manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
# Build software with basher. | |
read -rd '' HELP <<'EOF' | |
tree - Build software from custom build scripts, into a directory tree of your choosing. | |
USAGE: tree COMMAND [ AUTHOR/REPO ] [ OPTIONS ] | |
COMMANDS: | |
new : Initialize a new tree project. Respects --dir argument if given. | |
COMMAND AUTHOR/REPO : Pass "COMMAND" to INSTALL script of specified package. | |
OPTIONS: | |
--ssh : Use ssh when fetching. | |
-d, --dir : Directory in which tree should be executed, defaults to CWD. | |
-f, --fetch : If a build package is not yet installed, install it into the tree build directory with basher. | |
-h, --help : Show this prompt. | |
EOF | |
parseArguments() { | |
CONFIG[$ACTION]="$1" | |
shift | |
declare arg="$1" | |
while shift; do | |
case "$arg" in | |
'-d' | '--dir') | |
CONFIG[$DIRECTORY]="$1" | |
shift | |
;; | |
'-f' | '--fetch') | |
CONFIG[$FETCH]='true' | |
;; | |
'--ssh') | |
CONFIG[$SSH]='true' | |
BASHER_OPTIONS[${#BASHER_OPTIONS[@]}]='--ssh' | |
;; | |
'-h' | '--help') | |
echo "$HELP" >&2 | |
exit 0 | |
;; | |
-*) | |
echo "Unrecognized option: \"$arg\"" >&2 | |
echo "$HELP" >&2 | |
return 1 | |
;; | |
*) | |
if [[ -n ${CONFIG[$PACKAGE]} ]]; then | |
echo "Unexpected argument: \"$arg\"" >&2 | |
echo "$HELP" >&2 | |
return 1 | |
fi | |
CONFIG[$PACKAGE]="$arg" | |
;; | |
esac | |
arg="$1" | |
done | |
} | |
newTree() { | |
declare dir="${CONFIG[$DIRECTORY]}" | |
[[ -z $dir ]] && dir='.' | |
echo "Initializing a new tree in \"$dir\"" | |
[[ -d "$dir" ]] || mkdir -p "$dir" | |
[[ -a "$dir/INSTALL" ]] && mv -T --backup=numbered "$dir/INSTALL" "$dir/INSTALL.old" | |
[[ -a "$dir/package.sh" ]] && mv -T --backup=numbered "$dir/package.sh" "$dir/package.sh.old" | |
cat <<'EOF' > "$dir/"INSTALL | |
#!/bin/bash | |
## | |
# Tree build file. | |
set -xe | |
execute() { | |
case "$1" in | |
*) | |
printf 'This script does not implement command "%s"' "$1" | |
;; | |
esac | |
} | |
execute "$@" | |
EOF | |
cat <<'EOF' > "$dir/"package.sh | |
## | |
# Basher package file. | |
DEPS='' | |
EOF | |
} | |
interact() { | |
if ! basher list | grep "${CONFIG[$PACKAGE]}"; then | |
if [[ "${CONFIG[$FETCH]}" == true ]]; then | |
basher install "${BASHER_OPTIONS[@]}" "${CONFIG[$PACKAGE]}" || return $? | |
else | |
echo "Package \"${CONFIG[$PACKAGE]}\" is not installed. Can't build. Exiting." >&2 | |
return 1 | |
fi | |
fi | |
( | |
cd "$(basher package-path "${CONFIG[$PACKAGE]}")" || return $? | |
if ! [[ -e INSTALL ]]; then | |
echo "${CONFIG[$PACKAGE]} Does not contain an executable INSTALL file, exiting." >&2 | |
return 1 | |
fi | |
./INSTALL "${CONFIG[$ACTION]}" | |
) | |
} | |
execute() { | |
declare -a CONFIG=() | |
declare -a BASHER_OPTIONS=() | |
declare -ri PACKAGE=0 | |
declare -ri FETCH=1 | |
declare -ri SSH=2 | |
declare -ri ACTION=3 | |
declare -ri DIRECTORY=4 | |
parseArguments "$@" || return $? | |
if [[ -z "$TREE_DIR" ]]; then | |
echo "TREE_DIR is not set. exiting." >&2 | |
return 1 | |
elif [[ -z "$TREE_BUILD_DIR" ]]; then | |
echo "TREE_BUILD_DIR is not set. exiting." >&2 | |
return 1 | |
fi | |
export BASHER_PREFIX="$TREE_BUILD_DIR" | |
[[ -d "$TREE_DIR" ]] || mkdir -p "$TREE_DIR" | |
[[ -d "$TREE_BUILD_DIR" ]] || mkdir -p "$TREE_BUILD_DIR" | |
[[ -z ${CONFIG[$DIRECTORY]} ]] && CONFIG[$DIRECTORY]="$(pwd)" | |
case "${CONFIG[$ACTION]}" in | |
'-h'|'help'|'--help') | |
echo "$HELP" >&2 | |
exit 0 | |
;; | |
new) | |
newTree | |
;; | |
update) | |
basher upgrade "${CONFIG[$PACKAGE]}" | |
interact | |
;; | |
*) | |
interact | |
esac | |
} | |
if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then | |
set -e | |
shopt -s -o pipefail | |
execute "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment