You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bpkg/lib/install/install.sh

272 lines
5.4 KiB
Bash

10 years ago
#!/bin/bash
10 years ago
BPKG_GIT_REMOTE="${BPKG_GIT_REMOTE:-"https://github.com"}"
10 years ago
BPKG_REMOTE="${BPKG_REMOTE:-"https://raw.githubusercontent.com"}"
BPKG_USER="${BPKG_USER:-"bpkg"}"
## outut usage
usage () {
echo "usage: bpkg-install [-h|--help]"
10 years ago
echo " or: bpkg-install [-g|--global] <package>"
echo " or: bpkg-install [-g|--global] <user>/<package>"
10 years ago
}
message () {
if type -f bpkg-term > /dev/null 2>&1; then
term color "${1}"
fi
shift
printf " ${1}"
shift
if type -f bpkg-term > /dev/null 2>&1; then
term reset
fi
printf ": "
if type -f bpkg-term > /dev/null 2>&1; then
term reset
term bright
fi
printf "%s\n" "${@}"
if type -f bpkg-term > /dev/null 2>&1; then
term reset
fi
}
## output error
error () {
{
message "red" "error" "${@}"
} >&2
}
## output warning
warn () {
{
message "yellow" "warn" "${@}"
} >&2
}
## output info
info () {
local title="info"
if (( "${#}" > 1 )); then
title="${1}"
shift
fi
message "cyan" "${title}" "${@}"
}
10 years ago
## Install a bash package
bpkg_install () {
local pkg=""
10 years ago
local cwd="`pwd`"
local user=""
local name=""
local url=""
local uri=""
local version=""
local status=""
local json=""
10 years ago
local let needs_global=0
10 years ago
declare -a local parts=()
declare -a local scripts=()
declare -a args=( "${@}" )
10 years ago
for opt in "${@}"; do
if [ "-" = "${opt:0:1}" ]; then
continue
fi
pkg="${opt}"
break
done
for opt in "${@}"; do
case "${opt}" in
-h|--help)
usage
return 0
;;
-g|--global)
shift
needs_global=1
;;
*)
if [ "-" = "${opt:0:1}" ]; then
echo 2>&1 "error: Unknwon argument \`${1}'"
usage
return 1
fi
;;
esac
done
10 years ago
## ensure there is a package to install
if [ -z "${pkg}" ]; then
10 years ago
usage
10 years ago
return 1
fi
echo
10 years ago
## ensure remote is reachable
{
curl -s "${BPKG_REMOTE}"
if [ "0" != "$?" ]; then
error "Remote unreachable"
10 years ago
return 1
fi
}
## get version if available
{
OLDIFS="${IFS}"
IFS="@"
parts=(${pkg})
IFS="${OLDIFS}"
}
if [ "1" = "${#parts[@]}" ]; then
version="master"
info "Using latest (master)"
10 years ago
elif [ "2" = "${#parts[@]}" ]; then
name="${parts[0]}"
version="${parts[1]}"
else
error "Error parsing package version"
10 years ago
return 1
fi
## split by user name and repo
{
OLDIFS="${IFS}"
IFS='/'
parts=(${pkg})
IFS="${OLDIFS}"
}
if [ "1" = "${#parts[@]}" ]; then
user="${BPKG_USER}"
name="${parts[0]}"
elif [ "2" = "${#parts[@]}" ]; then
user="${parts[0]}"
name="${parts[1]}"
else
error "Unable to determine package name"
10 years ago
return 1
fi
10 years ago
## clean up name of weird trailing
## versions and slashes
10 years ago
name=${name/@*//}
10 years ago
name=${name////}
10 years ago
## build uri portion
uri="/${user}/${name}/${version}"
## clean up extra slashes in uri
uri=${uri/\/\///}
## build url
url="${BPKG_REMOTE}${uri}"
## determine if `package.json' exists at url
{
status=$(curl -sL "${url}/package.json?`date +%s`" -w '%{http_code}' -o /dev/null)
10 years ago
if [ "0" != "$?" ] || (( status >= 400 )); then
error "Package doesn't exist"
10 years ago
return 1
fi
}
## read package.json
json=$(curl -sL "${url}/package.json?`date +%s`")
10 years ago
## check if forced global
if [ ! -z $(echo -n $json | bpkg-json -b | grep 'global' | awk '{ print $2 }' | tr -d '"') ]; then
needs_global=1
fi
10 years ago
## construct scripts array
{
10 years ago
scripts=$(echo -n $json | bpkg-json -b | grep 'scripts' | awk '{$1=""; print $0 }' | tr -d '"')
10 years ago
OLDIFS="${IFS}"
10 years ago
## comma to space
10 years ago
IFS=','
10 years ago
scripts=($(echo ${scripts[@]}))
10 years ago
IFS="${OLDIFS}"
10 years ago
## account for existing space
scripts=($(echo ${scripts[@]}))
10 years ago
}
## build global if needed
10 years ago
if [ "1" = "${needs_global}" ]; 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/ *$//')"
if [ ! -z "${build}" ]; then
info "install: \`${build}'"
10 years ago
{(
## go to tmp dir
10 years ago
cd $( [ ! -z $TMPDIR ] && echo $TMPDIR || echo /tmp) &&
## prune existing
rm -rf ${name}-${version} &&
## shallow clone
10 years ago
git clone --depth=1 ${BPKG_GIT_REMOTE}/${user}/${name}.git ${name}-${version} > /dev/null 2>&1 &&
(
## move into directory
cd ${name}-${version} &&
## build
eval "${build}"
) &&
## clean up
rm -rf ${name}-${version}
10 years ago
)}
else
warn "Mssing build script"
fi
10 years ago
elif [ "${#scripts[@]}" -gt "0" ]; then
## get package name from `package.json'
10 years ago
name="$(
echo -n ${json} |
bpkg-json -b |
grep 'name' |
awk '{ $1=""; print $0 }' |
tr -d '\"' |
tr -d ' '
)"
10 years ago
## make `deps/' directory if possible
mkdir -p "${cwd}/deps/${name}"
10 years ago
## copy package.json over
curl -sL "${url}/package.json" -o "${cwd}/deps/${name}/package.json"
10 years ago
## grab each script and place in deps directory
for (( i = 0; i < ${#scripts[@]} ; ++i )); do
(
local script=${scripts[$i]}
curl -sL "${url}/${script}" -o "${cwd}/deps/${name}/${script}"
)
done
fi
return 0
}
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f bpkg_install
else
bpkg_install "${@}"
exit $?
fi